0
我曾多次嘗試在VBA中正確使用評估函數,但是我沒有成功。我想要在數據表上運行多個條件匹配。我在這裏提供了一個樣本數據表來進行測試。我無法獲得功能Evaluate(evalStr)
傳遞匹配行。我反覆收到一條錯誤消息。我在下面提供了一組樣本代碼,期望的結果是第7行,其中item1 =「Orange」和item2 =「Feb」。你能幫忙找出問題嗎?Excel VBA:多個條件匹配 - 評估函數
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
每一個這樣的例子都沒有在匹配前留下「=」號......我也嘗試過逃避引號,只是不與=號相結合。你的回答是正確的。謝謝 – Citanaf
@Citanaf請按照[旅遊]中的描述標記答案。 – teylyn
@teylyn我在等待最低限度的時間才能過期。謝謝 – Citanaf