创建在拖动时有效的UDF

时间:2017-01-19 22:06:10

标签: excel-vba excel-formula excel-udf vba excel

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

我的想法是在Excel中创建一个比以下设置更容易的功能: =INDEX($A$1:$A$5,AGGREGATE(15,6,ROW($B$1:$B$5)/($B$1:$B$5=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$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

几乎有效。除此之外,当我从第一行向下拖动时,数据不会更新。我必须点击进入单元格才能重新触发"重新触发"获取正确数据的功能:

enter image description here

(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$1:$A$5,$B$1:$B$5,1,ROW()) 但它只返回#VALUE错误

(另外,我如何避免使用ActiveCell.Row,因为我已经在头脑中钻了以避免使用Active任何内容......)

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

1 个答案:

答案 0 :(得分:2)

SO answer开始,尝试以下内容......

let mail = new SendGridMail(
      new SendGridEmail('from@example.com'),
      'Sending with SendGrid is Fun',
      new SendGridEmail('to@example.com'),
      new SendGridContent('text/plain', 'Email sent to to@example.com'));


return this.sendgridService.send(mail);