Excel VBA:多个标准匹配 - 评估功能

时间:2017-07-27 04:24:20

标签: excel vba excel-vba

我曾多次尝试在VBA中正确使用evaluate函数,但我没有成功。我想在数据表上运行多个条件匹配。我在这里提供了一个样本数据表供测试。我无法让函数Sub testEval() Dim dataTable As ListObject Dim evalStr As Variant Dim item1 As String Dim item2 As String Dim ws As Worksheet item1 = "Orange" item2 = "Feb" Set ws = ActiveSheet Set dataTable = ws.ListObjects(1) With ws 'V1 evalStr = .Evaluate("MATCH(" & item1 & "&" & item2 & "," & .Columns(1).Address & "&" & .Columns(2).Address & "),0)") 'V2? 'evalStr = .Evaluate("MATCH(" & item1 & "&" & item2 & "," & Application.WorksheetFunction.Substitute(.Columns(1).Address & "&" & .Columns(2).Address, "$", "") & "),0)" 'With dataTable.DataBodyRange ''V3? 'evalStr = ws.Evaluate("MATCH(" & item1 & "&" & item2 & "," & .Columns(1).Address & "&" & .Columns(2).Address & "),0)") ''V4? 'evalStr = ws.Evaluate("MATCH(" & item1 & "&" & item2 & "," & Application.WorksheetFunction.Substitute(.Columns(1).Address & "&" & .Columns(2).Address, "$", "") & "),0)") 'End With End With End Sub 传递匹配行。我反复收到错误信息。我在下面提供了一组示例代码,所需的结果是第7行,其中item1 =" Orange"和item2 =" 2月"。你能帮忙找出问题吗?

enter image description here

{{1}}

1 个答案:

答案 0 :(得分:1)

evalStr应该是:

evalStr = .Evaluate("=MATCH(""" & item1 & """&""" & item2 & """," & .Columns(1).Address & "&" & .Columns(2).Address & ",0)")
公式中缺少

=item1item2应该用双引号括起来,因此我添加了""并且)之前有,0 { {1}}