数组中的 Excel vba 搜索

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24423731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:35:40  来源:igfitidea点击:

Excel vba search in array

arraysexcelvbasearchaccess-vba

提问by user3774667

I need search in array

我需要在数组中搜索

Sub f()
    Dim myArray As Variant
    myArray = Worksheets("QQ").Range("D:F")

    Dim searchTerm As String
    searchTerm = "927614*"

    'Check if a value exists in the Array
    If UBound(Filter(myArray, searchTerm)) >= 0 And searchTerm <> "" Then
        MsgBox "Your string match value from F column is " & myArray(Application.Match(searchTerm, myArray, False),3)
    Else
        MsgBox ("Search Term could NOT be located in the Array")
    End If
End Sub

But I get error Type mismatch. So how to lookup value with * in array?

但我收到错误类型不匹配。那么如何在数组中用 * 查找值呢?

回答by Jean-Fran?ois Corbett

Just loop through the array and use Like.

只需遍历数组并使用Like.

Untested code:

未经测试的代码:

Dim matchFound As Boolean
matchFound = False
For i = 1 To UBound(myArray, 1)
    For j = 1 To UBound(myArray, 2)
        If myArray(i, j) Like searchTerm Then
            MsgBox "Found match at (" & i & "," & j & ") : " & myArray(i, j)
            matchFound = True
            Exit For
        End If
    Next j
    If matchFound Then Exit For
Next i
If Not matchFound Then MsgBox "No match found."