简体   繁体   English

在vba中是否有一个等效于vlookup的数组?

[英]Is there an array equivalent to vlookup in vba?

My vba code for a bunch of large ranges uses worksheetfunction.vlookup to find needed values. 我的大量VBA代码使用worksheetfunction.vlookup查找所需的值。 Ranges can be upwards of 25,000 cells, however, so this takes forever. 但是,范围可以超过25,000个单元,因此这需要永远。 Is there an equivalent function for arrays? 数组有等效功能吗?

I've seen lots of SO answers that seem to address returning true/false in there is an exact string match. 我已经看过很多SO答案,这些答案似乎解决了在精确匹配字符串的情况下返回true / false的问题。 I need the string's location. 我需要字符串的位置。

How about this ... 这个怎么样 ...

Function MyVLook(Arg As Range, Target As Range, ColIdx As Integer) As Range
Dim Idx As Integer

    If Arg = "" Then
        Set MyVLook = [ParamNothing]
    Else
        For Idx = 1 To Target.Rows.Count

            If Target(Idx, 1) = Arg Then
                If ColIdx < 0 Then
                    Set MyVLook = Target(Idx, 1).Offset(0, ColIdx)
                Else
                    Set MyVLook = Target(Idx, ColIdx)
                End If
                Exit For
            End If

        Next Idx
    End If
End Function

[ParamNothing] is a single cell range in a worksheet containing some application-specific text; [ParamNothing]是工作表中的单个单元格区域,其中包含一些特定于应用程序的文本; otherwise this works almost like a normal VLOOKUP ... you can specify negative column offsets though (something I often miss in regular VLOOKUP), and I didn't built in a flag for range searches. 否则,它的工作原理几乎与普通的VLOOKUP相似...但是,您可以指定负的列偏移 (在常规VLOOKUP中我经常会错过的东西),并且我没有为范围搜索构建标记。

If you're only looking for the first occurrence try this: 如果您只寻找第一次出现的情况,请尝试以下操作:

Public Sub FindInRange()

    Dim sValueToFind As String
    Dim rRangeToSearch As Range
    Dim rFoundRange As Range

    sValueToFind = "The value I'm searching for"

    With ThisWorkbook.Worksheets("Sheet1")
        Set rRangeToSearch = .Range("A1:A1193")

        Set rFoundRange = rRangeToSearch.Find( _
            What:=sValueToFind, _
            After:=rRangeToSearch.Cells(1, 1), _
            LookIn:=xlValues, _
            LookAt:=xlWhole, _
            SearchDirection:=xlNext, _
            MatchCase:=False)

        If Not rFoundRange Is Nothing Then
            MsgBox sValueToFind & " found in cell " & rFoundRange.Address & _
             " and the value two cells to the right is " & rFoundRange.Offset(, 2), vbInformation + vbOKOnly
        Else
            MsgBox sValueToFind & " not found.", vbInformation + vbOKOnly
        End If

    End With

End Sub

This will find an exact match due to LookAt:=xlWhole and will not match the case due to MatchCase:=False. 由于LookAt:= xlWhole,这将找到完全匹配,而由于MatchCase:= False,这将不匹配大小写。 If you want to find the last occurrence use SearchDirection:=xlPrevious. 如果要查找最后一次出现,请使用SearchDirection:= xlPrevious。

This mimics using Ctrl + F on the worksheet. 这类似于在工作表上使用Ctrl +F。 For more info on VBA FIND see: https://msdn.microsoft.com/en-us/library/office/ff839746.aspx 有关VBA FIND的更多信息,请参见: https : //msdn.microsoft.com/zh-cn/library/office/ff839746.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM