【问题标题】:VB Vlookup which will find the last matchVB Vlookup 将找到最后一个匹配项
【发布时间】:2017-02-06 23:20:38
【问题描述】:
  • 如何创建一个 VB Vlookup 来查找最后一个匹配项或最后 3 个匹配项?
  • 从我的范围底部查找B4:B9999

我试过了:

Private Sub FindRecord_Click()

Label21 = Application.WorksheetFunction.VLookup(ComboBox3.Value, Worksheets("Transactions").Range("B4:P9999"), 1, False)
Label21 = Application.WorksheetFunction.VLookup(ComboBox3.Value, Worksheets("Transactions").Range("B4:P9999"), 2, False)***

End Sub
Private Sub UserForm_Initialize()
ComboBox3.RowSource = "'[TEST46.xlsm]Transactions'!B4:B9999"**
End Sub

我已经尝试过 google,那些提供的解决方案是用于 excel vlookup 或不工作。

【问题讨论】:

  • 您可以将自己的MyVlookUp 创建为UDF,在里面您将只使用一个常规的For 循环,从末尾开始并找到匹配项每行。
  • 谢谢 Shai,我会先检查 Darren Bartrup-Cook 的模板。

标签: excel vlookup vba


【解决方案1】:

这将返回对最后三个找到的项目的引用。
然后,您可以使用 OFFSET 从相邻单元格返回值。
代码有点乱,可能可以改进,但它给了你想法。

Public Sub Test()

    Dim MyRange As Range
    Dim rCell As Range

    'Look for the value 4 in second column of Sheet3.
    Set MyRange = Find_Last_Three(4, Sheet3.Columns(2))

    If Not MyRange Is Nothing Then
        For Each rCell In MyRange
            'Print the values from the 2 cells to the right of the found cells.
            Debug.Print rCell.Offset(, 1) & " : " & rCell.Offset(, 2)
        Next rCell
    End If

End Sub

Public Function Find_Last_Three(ValueToFind As Variant, RangeToLookAt As Range) As Range

    Dim rFound As Range
    Dim rReturnedRange As Range
    Dim sFirstAddress As String
    Dim x As Long

    With RangeToLookAt
        Set rFound = .Find(What:=ValueToFind, _
                           After:=.Cells(1, 1), _
                           LookIn:=xlValues, _
                           LookAt:=xlWhole, _
                           SearchDirection:=xlPrevious)
        If Not rFound Is Nothing Then
            Set rReturnedRange = rFound
            sFirstAddress = rFound.Address
            For x = 1 To 2
                Set rFound = .FindPrevious(rFound)
                If rFound.Address <> sFirstAddress Then
                    Set rReturnedRange = Union(rReturnedRange, rFound)
                End If
            Next x
        End If
    End With

    Set Find_Last_Three = rReturnedRange

End Function

编辑:
将代码在用户表单上付诸实践:

  • 创建一个名为ComboBox3 的组合框。
  • 创建一个名为Label1 的标签。确保标签足够宽和足够高以显示所有数据(三行)。

将此代码添加到用户表单
(你必须在一个模块中仍然有Find_Last_Three。如果你愿意,你可以删除Test):

Private Sub UserForm_Initialize()
    Me.ComboBox3.RowSource = "Transactions!B4:B9999"
End Sub

Private Sub ComboBox3_Change()
    Dim rLastThree As Range
    Dim rCell As Range

    Set rLastThree = Find_Last_Three(Me.ComboBox3.Value, Range(Me.ComboBox3.RowSource))

    If Not rLastThree Is Nothing Then
        Me.Label1.Caption = ""
        For Each rCell In rLastThree
            Me.Label1.Caption = Me.Label1.Caption & rCell.Offset(, 1) & " : " & rCell.Offset(, 2) & vbCr
        Next rCell
    End If
End Sub

注意:rCell.Offset(,1)rcell.Offset(,2) 是从中获取额外信息的地方 - 从 B 列偏移 1 列和 2 列。

使用我的示例数据,它返回的结果显示最后三个 H 出现在第 11、15 和 18 行:
在此输入代码

【讨论】:

  • 嗨达伦,希望你能指导我吗?我在“'打印找到的单元格右侧的 2 个单元格中的值”上找不到任何结果。在上面运行后通过使用变体模拟 Sheet3
  • 我已经更新了我的答案,以展示如何在表单中使用代码。您的原始代码似乎有一个标签和组合框,所以使用了这些。
  • 1000 谢谢达伦,我一定会对其进行测试并发表评论。这是非常有价值的分享,因为我在互联网上找不到任何其他相同的分享。
猜你喜欢
  • 2016-12-24
  • 1970-01-01
  • 1970-01-01
  • 2019-05-07
  • 1970-01-01
  • 2022-11-25
  • 2020-11-29
  • 2015-05-20
  • 1970-01-01
相关资源
最近更新 更多