簡體   English   中英

數組中的 Excel vba 搜索

[英]Excel vba 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

但我收到錯誤類型不匹配。 那么如何在數組中用 * 查找值呢?

只需遍歷數組並使用Like

未經測試的代碼:

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."

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM