WorksheetFunction.Match 为您获取单个列范围或单个行范围中的值的相对位置。如果您的项目在列表中排名第三,则返回 3。
如果您想要表中的值,请使用 WorksheetFunction.VLookup。您正在表格或范围的第一列中查找您的项目。您指定要从中获取值的列,它将返回匹配行中的单元格值。
或者将 HLookup 用于转置表。
如果你真的想使用 Match,试试这个:
dim KeyValue as string
KeyValue = "my item"
Dim rowNum as Variant
If Not VBA.IsError(Application.Match(KeyValue, temp.Range("A1:A200"), 0)) Then
rowNum = Application.Match(KeyValue, temp.Range("A1:A200"), 0)
End If
如果你想要一个 vlookup 试试这个:
Dim RetVal As Variant
Dim KeyValue As String 'or integer or date as appropriate
Dim LookupTable As Range
Dim ColumnNumber As Integer
KeyValue = "myItem"
LookupTable = Range("A1:A200")
ColumnNumber = 2
RetVal = WorksheetFunction.VLookup(KeyValue, LookupTable, ColumnNumber, False)