Search Function in Userform With List Box

mrsec

Board Regular
Joined
Jan 28, 2016
Messages
54
Office Version
  1. 2016
Platform
  1. Windows
Good Day,
I am trying to write a code for a search button which searches a listbox based a specific input set in a textbox. The values searched are number or words, and the listbox contains values from a single column. The code i found below(ctto) but i don't understand why it only works for the first column of my list box.
  • SearchButton: A Button which upon clicking is supposed to initiate the search
  • SearchBox: The textbox which will contain the search value
  • AvailableNumberList: The listbox which contains the data
  • About 20 Columns in Total but I just want to search from Column A to G

Private Sub SearchButton_Click()

Dim SearchCriteria, i, n As Double
SearchCriteria = Me.SearchBox.Value
n = AvailableNumberList.ListCount

For i = 0 To n - 1
If Left(AvailableNumberList.List(i),Len(SearchCriteria))=SearchCriteria Then
AvailableNumberList.ListIndex = i
Exit For
End If
Next i

End Sub
 

Attachments

  • Screenshot 2021-10-10 174549.png
    Screenshot 2021-10-10 174549.png
    66.3 KB · Views: 97

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
You must compare with the 7 columns

Try this:

VBA Code:
Private Sub SearchButton_Click()
  Dim i As Long, j As Long
  Dim sc As Variant
  
  j = Len(SearchBox)
  With AvailableNumberList
    For i = 0 To .ListCount - 1
      Select Case SearchBox.Text
        Case Left(.List(i, 0), j), Left(.List(i, 1), j), Left(.List(i, 2), j), _
          Left(.List(i, 3), j), Left(.List(i, 4), j), Left(.List(i, 5), j), Left(.List(i, 6), j)
          .ListIndex = i
          Exit For
      End Select
    Next i
  End With
End Sub
 
Upvote 0
You must compare with the 7 columns

Try this:

VBA Code:
Private Sub SearchButton_Click()
  Dim i As Long, j As Long
  Dim sc As Variant
 
  j = Len(SearchBox)
  With AvailableNumberList
    For i = 0 To .ListCount - 1
      Select Case SearchBox.Text
        Case Left(.List(i, 0), j), Left(.List(i, 1), j), Left(.List(i, 2), j), _
          Left(.List(i, 3), j), Left(.List(i, 4), j), Left(.List(i, 5), j), Left(.List(i, 6), j)
          .ListIndex = i
          Exit For
      End Select
    Next i
  End With
End Sub
code works,but how do i make it case insensitive?
and make it "loop"when i have similar word in different rows?(image attached)

Thank You.
 
Upvote 0
and make it "loop"when i have similar word in different rows?(image attached)
I don't understand, but I guess if multiple records meet the condition then the code should select them.

Try:

VBA Code:
Private Sub SearchButton_Click()
  Dim i As Long, j As Long
  Dim sc As Variant
  
  j = Len(SearchBox)
  With AvailableNumberList
    .MultiSelect = fmMultiSelectMulti
    For i = 0 To .ListCount - 1
      Select Case LCase(SearchBox.Text)
        Case LCase(Left(.List(i, 0), j)), LCase(Left(.List(i, 1), j)), LCase(Left(.List(i, 2), j)), _
             LCase(Left(.List(i, 3), j)), LCase(Left(.List(i, 4), j)), LCase(Left(.List(i, 5), j)), _
             LCase(Left(.List(i, 6), j))
          .Selected(i) = True
      End Select
    Next i
  End With
End Sub
 
Upvote 0
I don't understand, but I guess if multiple records meet the condition then the code should select them.

Try:

VBA Code:
Private Sub SearchButton_Click()
  Dim i As Long, j As Long
  Dim sc As Variant
 
  j = Len(SearchBox)
  With AvailableNumberList
    .MultiSelect = fmMultiSelectMulti
    For i = 0 To .ListCount - 1
      Select Case LCase(SearchBox.Text)
        Case LCase(Left(.List(i, 0), j)), LCase(Left(.List(i, 1), j)), LCase(Left(.List(i, 2), j)), _
             LCase(Left(.List(i, 3), j)), LCase(Left(.List(i, 4), j)), LCase(Left(.List(i, 5), j)), _
             LCase(Left(.List(i, 6), j))
          .Selected(i) = True
      End Select
    Next i
  End With
End Sub
Case sensitive works.
For the "loop"i mentioned i attached a picture for you to view.
 

Attachments

  • Screenshot 2021-10-12 130255.png
    Screenshot 2021-10-12 130255.png
    51.1 KB · Views: 41
Upvote 0
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Search Function userform
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Search Function userform
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
Noted.thank you.
 
Upvote 0
If i have the same exact word it will only search the last entry.
I assume you mean select the last record found.

Try:

VBA Code:
Private Sub SearchButton_Click()
  Dim i As Long, j As Long
  Dim sc As Variant
  
  j = Len(SearchBox)
  With AvailableNumberList
    .MultiSelect = fmMultiSelectMulti
    For i = .ListCount - 1 to 0 step -1
      Select Case LCase(SearchBox.Text)
        Case LCase(Left(.List(i, 0), j)), LCase(Left(.List(i, 1), j)), LCase(Left(.List(i, 2), j)), _
             LCase(Left(.List(i, 3), j)), LCase(Left(.List(i, 4), j)), LCase(Left(.List(i, 5), j)), _
             LCase(Left(.List(i, 6), j))
           .ListIndex = i
           exit for
      End Select
    Next i
  End With
End Sub
 
Upvote 0
I assume you mean select the last record found.

Try:

VBA Code:
Private Sub SearchButton_Click()
  Dim i As Long, j As Long
  Dim sc As Variant
 
  j = Len(SearchBox)
  With AvailableNumberList
    .MultiSelect = fmMultiSelectMulti
    For i = .ListCount - 1 to 0 step -1
      Select Case LCase(SearchBox.Text)
        Case LCase(Left(.List(i, 0), j)), LCase(Left(.List(i, 1), j)), LCase(Left(.List(i, 2), j)), _
             LCase(Left(.List(i, 3), j)), LCase(Left(.List(i, 4), j)), LCase(Left(.List(i, 5), j)), _
             LCase(Left(.List(i, 6), j))
           .ListIndex = i
           exit for
      End Select
    Next i
  End With
End Sub
hmm..more like a partial search,like when I have the same content in different rows after I press the button it will go to the first one and if I click on the search button again it will select the 2nd row that has similar value.
 
Upvote 0

Forum statistics

Threads
1,215,433
Messages
6,124,861
Members
449,195
Latest member
MoonDancer

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top