Excel VBA wildcard search
You can use Like
operator (case-sensitive):
Private Sub search_Click()
For firstloop = 3 To 10
If Range("G" & firstloop).Text Like name.Text & "*" Then
MsgBox "Found!"
Exit Sub
Else
MsgBox "NOT FOUND"
End If
Next
End Sub
for case-insensitive search use:
If UCase(Range("G" & firstloop).Text) Like UCase(name.Text) & "*" Then
Also if you want to determine whether cell contains text (not only starts with text), you can use (case-sensitive):
If InStr(1, Range("G" & firstloop).Text, name.Text) > 0 Then
or (case-insensitive)
If InStr(1, Range("G" & firstloop).Text, name.Text, vbTextCompare) > 0 Then
UPD:
If the point only to show msgbox
, then I'd suggest to use Application.Match
:
Private Sub search_Click()
If Not IsError(Application.Match("abc" & "*", Range("G3:G10"), 0)) Then
MsgBox "Found!"
Else
MsgBox "NOT FOUND"
End If
End Sub