在VBA excel中修改了VLOOKUP

时间:2013-12-19 07:59:50

标签: excel vba vlookup

我正在尝试修改VBA中的VLOOKUP功能,但因为我第一次在VBA工作时不知道如何做某些事情。我想申请vlookup例如一刻就有200个细胞在一列中。我发现它可以用于循环,但它对我没用。可以说我们有三列。首先,有查找值,第二个是有一些值,第三个是查找值。可以说,我只想在第二列中的值为零的行中查找值。重要的是重要的是,我想通过只在一个单元格中输入公式来实现它。有谁能够帮我?图片链接

image

1 个答案:

答案 0 :(得分:0)

然后试试这个:

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)

以下是截图: sample image