1

我想我忽略了一些简单的东西......

我的想法是在 Excel 中创建一个比以下更容易设置的函数:( =INDEX($A$1:$A$5,AGGREGATE(15,6,ROW($B$1:$B$5)/($B$1:$B$5=1),ROW(1:1)))
请参阅ScottCraner 在此答案中对实践中该函数的评论)

我创建了以下 UDF:

Public Function findUnique(ByVal indexRange As Range, matchRange As Range, matchVal As Long)
' Trying to create a dynamic function of:
' =INDEX($A$1:$A$5,AGGREGATE(15,6,ROW($B$1:$B$5)/($B$1:$B$5=1),ROW(1:1)))

 findUnique = Evaluate("=Index(" & indexRange.Address & ",AGGREGATE(15,6,Row(" & matchRange.Address & _
    ")/(" & matchRange.Address & "=" & matchVal & "),Row(" & ActiveCell.Row & ":" & ActiveCell.Row & ")))")

End Function

几乎可以工作。除了,当我从第一行向下拖动时,数据不会更新。我必须单击单元格以“重新触发”该功能以获取要显示的正确数据:

在此处输入图像描述

(D列是那个公式,输入正确)。

但是,如何让公式自动更新,而无需重新输入单元格?

我还尝试添加第四个变量:

Public Function findUnique(ByVal indexRange As Range, matchRange As Range, matchVal As Long, curRow as Long)

findUnique = Evaluate("=Index(" & indexRange.address & ",AGGREGATE(15,6,Row(" & matchRange.address & _
    ")/(" & matchRange.address & "=" & matchVal & "),Row(" & curRow & ":" & curRow & ")))")

End Function

并输入 like:=findUnique($A$1:$A$5,$B$1:$B$5,1,ROW()) 但它只是返回一个#VALUE错误

(另外,我该如何避免ActiveCell.Row,因为我已经把它钻到我的头上以避免使用Active任何东西......)

感谢您的任何想法或建议!

4

1 回答 1

2

From this SO answer, try the following ...

 With Application.Caller
    CallerRow = .Row
 End With
 findUnique = Evaluate("=Index(" & indexRange.Address & ",AGGREGATE(15,6,Row(" & matchRange.Address & _
    ")/(" & matchRange.Address & "=" & matchVal & "),Row(" & CallerRow & ":" & CallerRow & ")))")
于 2017-01-19T22:33:31.867 回答