这是一个用户定义的函数,它将执行 2 列查找。把它当作一个vlookup
.
LookupPair
是一对细胞。(C2:D2) 在你的例子中。除了一对并排的单元格之外的任何其他内容都会导致错误。
LookupRange
是同时包含匹配对列和返回列的列。在您的示例中类似于 (I1:K101) 。LookupRange
必须至少包含两列,否则会产生错误。
ReturnCol
LookupRange
是包含要返回的值的列号。Col_index_num
(与a相同vlookup
)。
该函数仅进行完全匹配。
Function DoubleColMatch(LookupPair As Range, LookupRange As Range, ReturnCol As Integer) As Variant
Dim ReturnVal As Variant
Dim Col1Val As Variant
Dim Col2Val As Variant
Dim x As Long
If LookupPair.Rows.Count > 1 _
Or LookupPair.Columns.Count <> 2 _
Or LookupRange.Columns.Count < 2 _
Or ReturnCol < 1 _
Or ReturnCol > LookupRange.Columns.Count _
Then
ReturnVal = CVErr(xlErrRef)
Else
Col1Val = LookupPair.Cells(1, 1)
Col2Val = LookupPair.Cells(1, 2)
ReturnVal = CVErr(xlErrNA)
For x = 1 To LookupRange.Rows.Count
If LookupRange.Cells(x, 1) = Col1Val _
And LookupRange.Cells(x, 2) = Col2Val Then
ReturnVal = LookupRange.Cells(x, ReturnCol).Value
Exit For
End If
Next x
End If
DoubleColMatch = ReturnVal
End Function