简体   繁体   English

Excel VBA:多个条件匹配-评估功能

[英]Excel VBA: Multiple Criteria Match - Evaluate Function

I have tried on several occasions to correctly use the evaluate function in VBA, but I have not been successful. 我曾多次尝试在VBA中正确使用评估功能,但未成功。 I want to run a multiple criteria match on a data table. 我想在数据表上运行多个条件匹配。 I have provided a sample data table here for testing. 我在这里提供了样本数据表进行测试。 I cannot get the function Evaluate(evalStr) to pass a match row. 我无法获得函数Evaluate(evalStr)传递匹配行。 I repeatedly get an error message. 我反复收到一条错误消息。 I have provided a sample set of code below, the desired result is row 7 where item1 = "Orange" and item2 = "Feb". 我在下面提供了一个示例代码集,所需的结果是第7行,其中item1 =“ Orange”和item2 =“ Feb”。 Can you please help in identifying the issue? 您能帮忙找出问题吗?

在此处输入图片说明

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

evalStr should be: evalStr应该是:

evalStr = .Evaluate("=MATCH(""" & item1 & """&""" & item2 & """," & .Columns(1).Address & "&" & .Columns(2).Address & ",0)")

= is missing from formula, item1 and item2 should be enclosed in double quotes hence I've added "" and there's extra ) before ,0 =在公式中丢失, item1item2应该用双引号引起来,因此我在"" ,0 ""之前添加了""并且有多余的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM