2017-07-27 114 views
0

我曾多次嘗試在VBA中正確使用評估函數,但是我沒有成功。我想要在數據表上運行多個條件匹配。我在這裏提供了一個樣本數據表來進行測試。我無法獲得功能Evaluate(evalStr)傳遞匹配行。我反覆收到一條錯誤消息。我在下面提供了一組樣本代碼,期望的結果是第7行,其中item1 =「Orange」和item2 =「Feb」。你能幫忙找出問題嗎?Excel VBA:多個條件匹配 - 評估函數

enter image description here

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 

回答

1

evalStr應該是:

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

=從公式,item1失蹤,item2應該用雙引號括起來,因此我添加""),0之前有額外的

+0

每一個這樣的例子都沒有在匹配前留下「=」號......我也嘗試過逃避引號,只是不與=號相結合。你的回答是正確的。謝謝 – Citanaf

+1

@Citanaf請按照[旅遊]中的描述標記答案。 – teylyn

+0

@teylyn我在等待最低限度的時間才能過期。謝謝 – Citanaf