Excel 请问如何用VB实现vlookup功能

2024-12-04 12:30:13
推荐回答(2个)
回答1:

Function mylookup(ByVal lookup_value, table_array As Range, col_index_num%, Optional ByVal range_lookup As Boolean = True)
    Dim i%
    With table_array
        If range_lookup Then
            i = .Rows.Count
            Do While .Cells(i, 1) > lookup_value
                i = i - 1
                If i = 0 Then Exit Do
            Loop
            If i = 0 Then
                mylookup = "nothing is found"
            Else
                mylookup = .Cells(i, col_index_num)
            End If
        Else
            For i = 1 To .Rows.Count
                If .Cells(i, 1) = lookup_value Then
                    mylookup = .Cells(i, col_index_num)
                    Exit Function
                End If
            Next
            mylookup = "nothing is found"
        End If
    End With
End Function

回答2:

for 循环匹配 匹配到后 记录当前行数,然后引用需要的行列号