Link to home
Start Free TrialLog in
Avatar of Carly Carlyle Adams Cnf
Carly Carlyle Adams Cnf

asked on

VBA Wildcard

Hi, I am new to VBA and coding.
I have a textbox that if I type in text it looks up cells from 4 columns and if there is a match on the first word then it shows up in a list box.
It works but I want to add wildcard so it will look at the entire cell for words that match
ie: green apple slice in cell A1
I want to type apple or slice and it will get sell A1 displaying green apple slice it my list box. can someone please help?

Private Sub UserForm_Initialize()
TextBox1.SetFocus


End Sub

Private Sub TextBox1_Change()
TextBox1.Text = StrConv(TextBox1.Text, vbProperCase)
TextBox1.SetFocus
Dim i As Long
ListBox1.Clear
On Error Resume Next
For i = 1 To Application.WorksheetFunction.CountA(Sheet11.Range("A:A"))
a = Len(TextBox1.Text)
If UCase(Left(Sheet11.Cells(i, 2).Text, a)) = UCase(Left(TextBox1.Text, a)) Then
ListBox1.AddItem Sheet11.Cells(i, 1).Value
ListBox1.List(ListBox1.ListCount - 1, 1) = Sheet11.Cells(i, 2).Value
End If
If UCase(Left(Sheet11.Cells(i, 3).Text, a)) = UCase(Left(TextBox1.Text, a)) Then
ListBox1.AddItem Sheet11.Cells(i, 1).Value
ListBox1.List(ListBox1.ListCount - 1, 1) = Sheet11.Cells(i, 2).Value
End If
If UCase(Left(Sheet11.Cells(i, 4).Text, a)) = UCase(Left(TextBox1.Text, a)) Then
ListBox1.AddItem Sheet11.Cells(i, 1).Value
ListBox1.List(ListBox1.ListCount - 1, 1) = Sheet11.Cells(i, 2).Value
End If
If UCase(Left(Sheet11.Cells(i, 9).Text, a)) = UCase(Left(TextBox1.Text, a)) Then
ListBox1.AddItem Sheet11.Cells(i, 1).Value
ListBox1.List(ListBox1.ListCount - 1, 1) = Sheet11.Cells(i, 2).Value
End If
Next i
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Carly Carlyle Adams Cnf
Carly Carlyle Adams Cnf

ASKER

Wow, that's works great, that is so cool, thank you so much. Norie.
I have been working on this for a week watching videos and trying all kinds of things.
Thank you so much
Update on this to help the next person.
Code below.

  1. Private Sub TextBox1_Change()
  2. Dim I As Long
  3. Dim strSearch As String
  4.    TextBox1.Text = StrConv(TextBox1.Text, vbProperCase)
  5.    TextBox1.SetFocus
  6.    ListBox1.Clear
  7.    strSearch = "*" & UCase(TextBox1.Text) & "*"
  8.    'Note: Why For I = 10 as I have no info in rows 1 to 9 this is a heading. So change 10 to 1 if don't what to skip rows.
  9.    For I = 10 To Application.WorksheetFunction.CountA(Sheet11.Range("A:A"))
  10.    'This code will search columns 2,3,4 & 9.
  11.        If UCase(Sheet11.Cells(I, 2).Text) & UCase(Sheet11.Cells(I, 3).Text) & UCase(Sheet11.Cells(I, 4).Text) & UCase(Sheet11.Cells(I, 9).Text) Like strSearch Then
  12.            With ListBox1
  13.                .AddItem Sheet11.Cells(I, 1).Value
  14.                .List(.ListCount - 1, 1) = Sheet11.Cells(I, 2).Value
  15.            End With
  16.        End If
  17.    Next I
  18. End Sub