【问题标题】:modified VLOOKUP in VBA excel在 VBA excel 中修改 VLOOKUP
【发布时间】:2013-12-19 07:59:50
【问题描述】:

我正在尝试修改 VBA 中的 VLOOKUP 函数,但因为我是第一次在 VBA 中工作,所以不知道如何做某些事情。我想为例如应用 vlookup一列中有 200 个单元格。我发现它可以使用循环来完成,但它对我不起作用。假设我们有三列。首先,有查找值,第二个有一些值,第三个应该有查找值。可以说,我只想在第二列中的值为零的行中查找值。重要的是要重复一遍,我希望它只在一个单元格中输入公式。有谁能够帮我?图片链接

【问题讨论】:

  • 您想要一个类似于countifsvlookup?我说的对吗?
  • 我看不到任何重要的东西有什么我想要的与 countif 共同的功能。但有可能我只是不明白你。
  • countifs 在 excel 2007 及更高版本中有多个条件。据我了解,您想要具有多个条件的vlookup 版本吗?
  • 我添加了图片以便更好地了解我的需求,我相信从中可以理解。
  • 怎么样:=IF(B1=0,VLOOKUP(A1,I1:J20,2,FALSE),"")。这仅在 B 列中有 0 时执行查找

标签: excel vba vlookup


【解决方案1】:

然后试试这个:

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)

屏幕截图如下:

【讨论】:

  • 顺便说一句,如果您的 criteria 是字符串,请加上引号。喜欢这个=FLOOKUP(A1,I:J,2,0,B1,"your_string")
  • 它对我不起作用,我不知道为什么,我使用的参数和你一样。当我插入一些错误文本时,只会发生在评估单元格中有这个错误文本之后,我放公式的地方,其他单元格是空的。我仍在试图弄清楚为什么它对我不起作用。
  • 我更新了代码。你把代码放在哪里了?应该在一个模块中。将其放入模块后,键入屏幕截图中的公式,如 C 列所示。
  • 啊,在每个FLOOKUP = something 之后添加这一行Exit Function。对不起,我匆忙做了这个:)
  • 我已将其放入模块并输入您的公式,使用更新的代码,添加退出功能,但仍然无法正常工作。当我在没有 iferror 的情况下使用它时,我只得到#NAME?在那个单元格中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多