I'm trying to create a search function in my userform that's connected to the listbox. I have three textbox options to search from - search via surname is Textbox1, first name is Textbox2 and by year is Textbox3. I've managed to get it to kind of work, however when I add the else if's for the other two textboxes it glitches. I want to be able to search individually, not having to put an input for every textbox.
I would also like to have it so that it just shows only the search results instead of having the results moved to the top if possible?
This is what I have so far:
Userform:
Search Button:
I have everything else working in the userform such as edit, delete and add to listbox. Just need the search function to make things easier. I'm still pretty new to Excel VBA so any assistance is appreciated. Thank you!
I would also like to have it so that it just shows only the search results instead of having the results moved to the top if possible?
This is what I have so far:
Userform:
VBA Code:
Private Sub UserForm_Initialize()
Set tblbreakdownTable = ThisWorkbook.Worksheets("Breakdown").ListObjects("breakdownTable")
With playerList
.List = tblbreakdownTable.DataBodyRange.Value2
.ColumnCount = 9
.ColumnHeads = False
End With
End Sub
Search Button:
Code:
Private Sub SearchButton_Click()
Sur2 = TextBox1.value
Fnam2 = TextBox2.value
Year2 = TextBox3.value
For sat = 2 To Cells(rows.Count, 1).End(xlUp).Row
Set Sur1 = Cells(sat, "A")
Set Fnam1 = Cells(sat, "B")
Set Year1 = Cells(sat, "C")
If Sur1 Like Sur2 & "*" Then
playerList.AddItem
playerList.List(s, 0) = Cells(sat, "A")
playerList.List(s, 1) = Cells(sat, "B")
playerList.List(s, 2) = Cells(sat, "C")
playerList.List(s, 3) = Cells(sat, "D")
playerList.List(s, 4) = Cells(sat, "E")
playerList.List(s, 5) = Cells(sat, "F")
playerList.List(s, 6) = Cells(sat, "G")
playerList.List(s, 7) = Cells(sat, "H")
playerList.List(s, 8) = Cells(sat, "I")
s = s + 1
ElseIf Fnam1 Like Fnam2 & "*" Then
playerList.AddItem
playerList.List(s, 0) = Cells(sat, "A")
playerList.List(s, 1) = Cells(sat, "B")
playerList.List(s, 2) = Cells(sat, "C")
playerList.List(s, 3) = Cells(sat, "D")
playerList.List(s, 4) = Cells(sat, "E")
playerList.List(s, 5) = Cells(sat, "F")
playerList.List(s, 6) = Cells(sat, "G")
playerList.List(s, 7) = Cells(sat, "H")
playerList.List(s, 8) = Cells(sat, "I")
s = s + 1
ElseIf Year1 Like Year2 & "*" Then
playerList.AddItem
playerList.List(s, 0) = Cells(sat, "A")
playerList.List(s, 1) = Cells(sat, "B")
playerList.List(s, 2) = Cells(sat, "C")
playerList.List(s, 3) = Cells(sat, "D")
playerList.List(s, 4) = Cells(sat, "E")
playerList.List(s, 5) = Cells(sat, "F")
playerList.List(s, 6) = Cells(sat, "G")
playerList.List(s, 7) = Cells(sat, "H")
playerList.List(s, 8) = Cells(sat, "I")
s = s + 1
End If: Next
End Sub
I have everything else working in the userform such as edit, delete and add to listbox. Just need the search function to make things easier. I'm still pretty new to Excel VBA so any assistance is appreciated. Thank you!