简体   繁体   中英

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. 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. 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". 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 = .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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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