我想通过VBA使用它 -
=MATCH("PlanA",A:A,0)
EVALUATE
。
Sub Test()
Dim SectionStartRow As Integer
Dim planname As String
planname = "PlanA"
SectionStartRow = [MATCH(planname,A:A,0)] 'Error 2029 /// Type mismatch '13
End Sub
我已经尝试过了:
SectionStartRow = Evaluate("MATCH(planname,A:A,0)") 'Error 2029 /// Type mismatch '13
和
SectionStartRow = Evaluate("MATCH(" & planname & ",A:A,0)")
但似乎没有任何效果。请注意,planname
变量被一长串函数拒绝。
答案 0 :(得分:4)
问题是planname
需要围绕字符串引号。
excel等效项为=MATCH("PlanA",A:A,0)
,传递给Evaluate
的字符串应与之相同:
SectionStartRow = Evaluate("=MATCH( " & Chr(34) & planname & Chr(34) & ",A:A,0)")
正常工作(使用Excel 2010测试)
Chr(34)
代表ASCII编号中的"
符号。
另一种方法是定义
planname = Chr(34) & "PlanA" & Chr(34)
然后呢
SectionStartRow = Evaluate("=MATCH( " & planname & ",A:A,0)")
顺便说一句,我会将SectionStartRow
定义为Long
而不是Integer
,因为如果匹配的行在32,767之后,Integer
会抛出错误。
另请注意,Evaluate
的简写形式(即方括号[]
)在这种情况下不起作用。它只适用于everything inside the brackets is a constant, not a variable。
我希望这有帮助!
答案 1 :(得分:0)
I stumbled upon this useful question and struggled with connecting not only the planname
but also resolve it with a different sheet.
I was confused with the double quotation mark and & symbol from the answer by Ioannis.
planname = Chr(34) & "PlanA" & Chr(34)
In my example a version of the below line correctly resolved the first parameter. So only one "& variable &"
worked for me.
SectionStartRow = Evaluate("=MATCH('Sheet1'!C " & variablename & ",A:A,0)")