Excel Vba中的工作表函数具有连接的值
我有以下公式:
dim functionString as String functionString = "IFERROR(AND(MATCH(" & FirstName.Value & ",C2:C1048576, 0)>0, (MATCH(" & LastName.Value & ",B2:B1048576, 0)>0)), MAX(A2:A1048576)+1)"
我想能够做的就是从VBA代码中调用它,所以它看起来像。
application.WorksheetFunction(functionString)
我知道,我可以把它放在工作表上的一些永远不会被使用的单元格上:IE:
Activesheet.range("ZZ1000").formula = "="& functionString
然后引用该单元格,而不用担心该程序是否会无意中崩溃; 但有没有办法从VBA直接做这样的公式?
基本上我正在查看是否FirstName.Value和LastName.Value(在代码中定义的其他地方)在B列和C列的工作表中。当我写这个,我意识到我需要确保他们都在同一行,而不是在不同的行。
您可以尝试Application.Evaluate(functionString)
但根据复杂性,最好使用VBA函数而不是WorksheetFunctions。
如果在范围/数组中找不到该值,则Application.Match
函数将返回一个错误types,所以我们Dim first, last
作为变体types来允许(不发生错误)。
Dim first, last ' find the row where FirstName.Value appears in column C first = Application.Match(FirstName.Value, Columns(3), False)) ' find the row where LastName.Value appears in column B last = Application.Match(LastName.Value, Columns(2), False)) If Not IsError(first) And Not IsError(last) Then If first = last Then ' match was found in both columns and on same row ' do something else... End If End If