创建向下拖动时有效的 UDF

Create UDF that works when dragged down

我想我忽略了一些简单的事情...

我的想法是在 Excel 中创建一个比以下函数更容易设置的函数: =INDEX($A:$A,AGGREGATE(15,6,ROW($B:$B)/($B:$B=1),ROW(1:1)))
(请参阅 ScottCraner's comment in this answer 以了解该功能的实际应用)

我创建了以下 UDF:

Public Function findUnique(ByVal indexRange As Range, matchRange As Range, matchVal As Long)
' Trying to create a dynamic function of:
' =INDEX($A:$A,AGGREGATE(15,6,ROW($B:$B)/($B:$B=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

而且 几乎 有效。除了,当我从第一行向下拖动时,数据不会更新。我必须单击单元格以 "retrigger" 函数来显示正确的数据:

(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

然后输入:=findUnique($A:$A,$B:$B,1,ROW()) 但它只是returns一个#VALUE错误

(另外,我如何避免 ActiveCell.Row,因为我已经将它深入我的脑海以避免使用 Active 任何东西...)

感谢任何想法或建议!

从此SO answer,尝试以下...

 With Application.Caller
    CallerRow = .Row
 End With
 findUnique = Evaluate("=Index(" & indexRange.Address & ",AGGREGATE(15,6,Row(" & matchRange.Address & _
    ")/(" & matchRange.Address & "=" & matchVal & "),Row(" & CallerRow & ":" & CallerRow & ")))")