然后试试这个:
Function FLOOKUP(lookup_value, table_array As Range, col_index_num As Long, _
range_lookup As Boolean, Optional ref_value, Optional criteria) As Variant
Dim FoundCell As Range
Dim LastCell As Range
Dim FirstAddr, find_value As String
Dim my_range As Range
Dim row_count, col_count As Long
Dim check As Boolean
col_count = table_array.Columns.Count
find_value = lookup_value
If col_index_num >= 0 Then
Set my_range = table_array.Resize(, 1)
Else
Set my_range = table_array.Resize(, 1).Offset(0, col_count - 1)
End If
With my_range
row_count = .Cells.Count
If row_count = 1048576 Then row_count = .Cells(.Cells.Count).End(xlUp).Row
End With
Set my_range = my_range.Resize(row_count)
Set LastCell = my_range.Cells(my_range.Cells.Count)
If range_lookup Then
Set FoundCell = my_range.Find(what:=find_value, after:=LastCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
Else
Set FoundCell = my_range.Find(what:=find_value, after:=LastCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
End If
If Not FoundCell Is Nothing Then
FirstAddr = FoundCell.Address
If IsNumeric(col_index_num) And Abs(col_index_num) <= col_count Then
Select Case col_index_num
Case Is > 0
If IsMissing(ref_value) Then
FLOOKUP = FoundCell.Offset(0, col_index_num - 1).Value
Else
If ref_value = criteria Then
FLOOKUP = FoundCell.Offset(0, col_index_num - 1).Value
Else
FLOOKUP = CVErr(xlErrNA)
Exit Function
End If
End If
Case Is < 0
If IsMissing(ref_value) Then
FLOOKUP = FoundCell.Offset(0, col_index_num + 1).Value
Else
If ref_value = criteria Then
FLOOKUP = FoundCell.Offset(0, col_index_num + 1).Value
Else
FLOOKUP = CVErr(xlErrNA)
Exit Function
End If
End If
End Select
Exit Function
Else
FLOOKUP = CVErr(xlErrRef)
Exit Function
End If
Else
FLOOKUP = CVErr(xlErrNA)
Exit Function
End If
End Function
仍然需要改进,但我是如何开始的。
语法:
FLOOKUP (lookup_value, table_array, col_index_num, range_lookup, [ref_value], [criteria])
前四个参数与Vlookup 相同,但range_lookup 不是可选的。
其余两个 (2) 是可选的。
ref_value 是您希望比较的值(在您的情况下,B 列中的值)。
criteria 是测试标准。 (在你的情况下为 0)
屏幕截图如下: