arraysexcelvbaloopsmatch

Using Application.Match and an array with wildcards


So I have some code similar to this and it works well and seems to be fast and simple. I like that I can place a few dozen words in the array for words to search for.

Dim ArrDataType As Variant

ArrDataType = Array("Circle:", "Point:")

For i = 1 To 10 Step 1
    
    CurrentCellValue1 = Trim(mybook.Sheets(SourceSh).Range("A" & i))
    
    If IsNumeric(Application.Match(CurrentCellValue1, ArrDataType, 0)) Then  
        Debug.Print "Match Found!: " & CurrentCellValue1
    End If
    
Next i

However I was trying to modify this to find words using wildcards but can't figure out how to do it. For example:

Dim ArrDataType As Variant

ArrDataType = Array("Circle:*", "Point:*")

For i = 1 To 10 Step 1
    
    CurrentCellValue1 = Trim(mybook.Sheets(SourceSh).Range("A" & i))
    
    If IsNumeric(Application.Match(CurrentCellValue1, ArrDataType, 0)) Then  
        Debug.Print "Match Found!: " & CurrentCellValue1
    End If
    
Next i

This doesn't work so clearly I am implementing the wildcard incorrectly. I know Could search letter by letter and things but I have thousands of rows to search (not only 10) and I assume these other methods would perform quite a bit slower. It seems like it shouldn't be hard to incorporate wildcards into this code that is already working well.


Solution

  • Iterate the array inside the column iteration. Then I would use Instr instead. No need for WildCards.

    Dim ArrDataType As Variant
    
    ArrDataType = Array("Circle:", "Point:")
    
    For i = 1 To 10 Step 1
        
        CurrentCellValue1 = Trim(mybook.Sheets(SourceSh).Range("A" & i))
    
        For j = lbound(ArrDataType) to ubound(ArrDataType)
        
            If Instr(CurrentCellValue1,ArrDataType(j)) > 0 Then  
                Debug.Print "Match Found!: " & CurrentCellValue1
                Exit For
            End If
        Next j
        
    Next i