简体   繁体   中英

Excel vba search in array

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?

Just loop through the array and use 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."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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