Search code examples
vbaloopscellwildcard

vba wildcard search on cell


I'm trying to find something with a wild card search in a cell value. If the value in sheet("FC")Range("I2:I" & LastRowC) - match with the Sheets("Instr"),Range("A130:A190"). means sheet Instr match if few characters match with the other range mentioned above then do something code.

eg in sheet Instr above range a cell value is "Ajith" and In sheet FC above mentioned range one of the cell value is "Aji" the code should identify it.

All the below steps are okay for me except the wild card search through the loop range , please go through the code and range (rename the sheets if necessary as below) and provide an update.

Sub Exception()

Dim mfc As Worksheet
Dim mfp As Worksheet
Dim mfo As Worksheet
Dim instr As Worksheet

Set mfc = Sheets("FC")
Set mfp = Sheets("FP")
Set mfo = Sheets("OSLR")
Set inst = Sheets("Instr")

Dim irng As Range
Dim icel As Range

Set irng = inst.Range("A130:A190")

Dim LastRowC As Long
LastRowC = mfc.Cells(Rows.Count, 1).End(xlUp).Row

Dim fcphr As Range
Dim fcphc As Range

Set fcphr = mfc.Range("I2:I" & LastRowC)

For Each icel In irng.Rows
For Each fcphc In fcphr.Rows

If icel.Value = "" Then
Exit For
End If
If fcphc.Value = "" Then
Exit For
End If

If fcphc.Value = icel.Value Then

    msgbox fcphc
    msgbox icel

    '***(i need a wild card search for the above step)***

End If

Next fcphc
Next icel


End Sub

Solution

  • You could use the Like operator. For example:

    If fcphc.Value Like "*" & icel.Value & "*" Then
    

    If you wanted the comparison to work both ways:

    If _
        fcphc.Value Like "*" & icel.Value & "*" Or _
        icel.Value Like "*" & fcphc.Value & "*" _
    Then