对左侧执行 VLOOKUP 并进行近似 (TRUE, 1) 匹配

问题描述 投票:0回答:1

我有 VBA 代码来执行左侧的 VLOOKUP。我在 MrExcel 上找到了它并进行了编辑,因此

lookup_value
可以是任何类型的数据:

Public Function VLOOKUPLEFT(lookup_value As Variant, table_array As Range, col_index_num As Integer, Optional range_lookup As Boolean = False) As Variant

Dim wk As Workbook
Dim ws As Worksheet
Dim valFind As Range

Set wk = Application.ActiveWorkbook
Set ws = wk.ActiveSheet

Set valFind = table_array.Find(What:=lookup_value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
MatchCase:=False, SearchFormat:=False).Offset(0, -1 * col_index_num)

VLOOKUPLEFT = valFind.Value

End Function

代码中的其他任何地方都没有引用

col_index_num
,因为我不知道把它放在哪里。

显然,

.Find
方法不允许近似匹配。

我以为会是

SearchFormat
LookAt
,但文档很清楚,他们认为有些不同:

  1. SearchFormat:这指示要搜索的值是否应采用特定格式(如粗体或斜体)。如果搜索值应遵循格式化技术,则此参数指定为 true,否则为 false。该参数的默认值为 false。

  2. LookAt:决定是匹配整个单元格的内容(完全匹配)还是匹配部分单元格内容(部分匹配)。常量 xlWhole 和 xlPart 分别表示精确匹配和部分匹配。该参数的默认值为 xlPart。

我可以使用 INDEX/MATCH/MATCH 或 XLOOKUP,但我想知道是否也有一种方法可以实现此目的以用于学习目的。

我尝试搜索一个可以给我近似匹配的值。
它返回了#VALUE! Excel 中出现错误,因为代码不允许近似匹配。

excel vba
1个回答
0
投票
Public Function VLOOKUPLEFT(lookup_value As Variant, table_array As Range, col_index_num As Integer, Optional range_lookup As Boolean = False) As Variant
    
    Dim wk             As Workbook
    Dim ws             As Worksheet
    Dim valFind        As Range
    Dim counter        As Long
    Dim formula_col    As Integer
    
    Set wk = Application.ActiveWorkbook
    Set ws = wk.ActiveSheet
    counter = 0
    
    If range_lookup = TRUE Then
        Do
            Set valFind = table_array.Find(What:=lookup_value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
                MatchCase:=False, SearchFormat:=False)
            If Not valFind Is Nothing Then
                Exit Do
            End If
            For i = 1 To table_array.Rows.Count - 1
                If lookup_value >= table_array(i, table_array.Columns.Count).Value And _
                   lookup_value < table_array(i + 1, table_array.Columns.Count).Value Then
                lookup_value = table_array(i, table_array.Columns.Count)
                Set valFind = table_array.Find(What:=lookup_value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
                    MatchCase:=False, SearchFormat:=False)
                Exit Do
            End If
        Next i
        counter = counter + 1
        If counter >= table_array.Rows.Count Then
            Exit Do
        End If
    Loop
Else
    Set valFind = table_array.Find(What:=lookup_value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
        MatchCase:=False, SearchFormat:=False)
End If

If Not valFind Is Nothing Then
    formula_col = table_array.Columns.Count - col_index_num
    VLOOKUPLEFT = valFind.Offset(0, -1 * formula_col).Value
    'If you want the columns to count from right to left, substitute "-1 * formula_col" with "(-1 * col_index_num) + 1" in the second argument of the .Offset method'
Else
    VLOOKUPLEFT = CVErr(xlErrValue)
End If

End Function
© www.soinside.com 2019 - 2024. All rights reserved.