通过用户定义的函数VBA缩短excel公式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过用户定义的函数VBA缩短excel公式相关的知识,希望对你有一定的参考价值。
我的最终目标是使用VBA中的用户定义函数缩短此公式,以下为我的参数A1:C10,A1:A10,E1,B1:B10,F1(这会有所不同,这只是一个纯粹的例子)
Excel公式:
=index(A1:C10,match(1,(A1:A10=E1)*(B1:B10=F1),0),G1)
使用用户定义的函数,它将是这样的:
=pick(A1:C10, A1:A10, E1, B1:B10, F1, G1)
我的VBA代码是这样的,但它不起作用。请高手,帮帮我。非常感谢。
Function pick(range As range, con1 As range, con1a As range, con2 As range, con2a As range, col As range) As Variant
pick = WorksheetFunction.Index(range, Match(1, (con1 = con1a) * (con2 = con2a), 0), col)
End Function
答案
使用Evaluate方法:
Function pick(range As range, con1 As range, con1a As range, con2 As range, con2a As range, col As range) As Variant
pick = Application.Caller.Parent.Evaluate("Index(" & range.Address(1,1,,1) & ",Match(1, (" & con1.Address(1,1,,1) & " = " & con1a.Address(1,1,,1) & ") * (" & con2.Address(1,1,,1) & " = " & con2a.Address(1,1,,1) & "), 0), " & col.Address(1,1,,1) & ")")
End Function
以上是关于通过用户定义的函数VBA缩短excel公式的主要内容,如果未能解决你的问题,请参考以下文章
使用 Excel DNA 将用 F# 编写的用户定义函数导入 VBA 子例程