Excel VBA worksheetfunction 与数组的索引匹配
Excel VBA worksheetfunction Index Match with an Array
有没有办法让 VBA 在使用 .worksheet 函数时排除带有 INDEX MATCH 的数组公式?
我的第一个公式有效,因为我认为它不是数组?
此代码有效
Dim VType As string
VType = Application.WorksheetFunction.Index(Sheets(sheetname).Range("$B:$B"), Application.WorksheetFunction.Match("*" & VendorCode & "*", Sheets(sheetname).Range("$A:$A"), 0), 1)
但是当我添加第二个匹配项时,我得到一个错误:类型不匹配
Dim RetORWaste As String
RetORWaste = Application.WorksheetFunction.Index(Sheets(wsMaster.Name).Range("$F:$F"), Application.WorksheetFunction.Match(("*" & VendorCode & "*") & ("*" & VRegion & "*"), (Sheets(wsMaster.Name).Range("$B:$B")) & (Sheets(wsMaster.Name).Range("$C:$C")), 0), 1)
工作表名称和wsMaster.name都是字符串。
wsMaster.Name 也获得了正确的工作表名称。所以一定是数组?
如果我理解正确,你想像这里那样做 "two column match":https://www.excel-easy.com/examples/two-column-lookup.html
Sub tester()
Dim RetORWaste As String, wsMaster As Worksheet, m, f
Dim VendorCode, VRegion
Set wsMaster = ActiveSheet
VendorCode = "A"
VRegion = "B"
f = "=MATCH(""*{vend}*""&""*{region}*"",B:B&C:C,0)"
f = Replace(f, "{vend}", VendorCode)
f = Replace(f, "{region}", VRegion)
m = wsMaster.Evaluate(f) '<<do not use Application.Evaluate here, or the
' formula will evaluate in the context of the
' active sheet, which might not be wsMaster
If Not IsError(m) Then
'got a match so get the value from col F
RetORWaste = wsMaster.Cells(m, "F")
Debug.Print RetORWaste
End If
End Sub
有没有办法让 VBA 在使用 .worksheet 函数时排除带有 INDEX MATCH 的数组公式?
我的第一个公式有效,因为我认为它不是数组?
此代码有效
Dim VType As string
VType = Application.WorksheetFunction.Index(Sheets(sheetname).Range("$B:$B"), Application.WorksheetFunction.Match("*" & VendorCode & "*", Sheets(sheetname).Range("$A:$A"), 0), 1)
但是当我添加第二个匹配项时,我得到一个错误:类型不匹配
Dim RetORWaste As String
RetORWaste = Application.WorksheetFunction.Index(Sheets(wsMaster.Name).Range("$F:$F"), Application.WorksheetFunction.Match(("*" & VendorCode & "*") & ("*" & VRegion & "*"), (Sheets(wsMaster.Name).Range("$B:$B")) & (Sheets(wsMaster.Name).Range("$C:$C")), 0), 1)
工作表名称和wsMaster.name都是字符串。 wsMaster.Name 也获得了正确的工作表名称。所以一定是数组?
如果我理解正确,你想像这里那样做 "two column match":https://www.excel-easy.com/examples/two-column-lookup.html
Sub tester()
Dim RetORWaste As String, wsMaster As Worksheet, m, f
Dim VendorCode, VRegion
Set wsMaster = ActiveSheet
VendorCode = "A"
VRegion = "B"
f = "=MATCH(""*{vend}*""&""*{region}*"",B:B&C:C,0)"
f = Replace(f, "{vend}", VendorCode)
f = Replace(f, "{region}", VRegion)
m = wsMaster.Evaluate(f) '<<do not use Application.Evaluate here, or the
' formula will evaluate in the context of the
' active sheet, which might not be wsMaster
If Not IsError(m) Then
'got a match so get the value from col F
RetORWaste = wsMaster.Cells(m, "F")
Debug.Print RetORWaste
End If
End Sub