Excel VBA to Display search result in listbox

In case if you have a large list of employee data in a sheet and you want to select one employee details in main sheet based on the employee number. You can put a vlookup table and the details can be retrieved based on id in that table as below.

Display search result in listbox

But in case if you cant remember all employee id’s, you can create a small search box in excel VBA to search based on name and the table can be updated based on the selection from search results in listbox.

Display search result in listbox

To create this search box, you can create a userform with following objects.

  • Textbox to enter search name with name Textbox1
  • Search button with name CommandButton1 
  • List box to give the search output with name Listbox1 
  • OK button to exit the userform with name CommandButton2

The search box will look like as below:

Then put below code in Userform1 to search and display the result based on name of the employee.

Private Sub CommandButton1_Click()
' to search and display in listbox when search button is pressed.

Dim RowNum As Long

RowNum = 1
Do Until Sheets("Data").Cells(RowNum, 1).Value = ""

If InStr(1, Sheets("Data").Cells(RowNum, 2).Value, TextBox1.Value, vbTextCompare) > 0 Then
On erro GoTo next1
ListBox1.AddItem Sheets("Data").Cells(RowNum, 1).Value
ListBox1.List(ListBox1.ListCount - 1, 1) = Sheets("Data").Cells(RowNum, 2).Value
End If
next1:
RowNum = RowNum + 1
Loop
End Sub


Private Sub CommandButton2_Click()
' to exit from searchbox
Unload Me

End Sub

Private Sub ListBox1_Click()
' enter the id number in sheet when selected in listbox
Dim rowcount As Variant
rowcount = ListBox1.Column(0, ListBox1.ListIndex)

Sheets("Results").Range("B3").Value = rowcount

End Sub

To initiate the search box from Sheet you can insert a Shape and link to macro to put below simple code to call userform1.

Sub Searchbox()

UserForm1.Show

End Sub

When you search for the name in serchbox and click on Search, it will show the list of matching records in listbox as below:

You can select the employee number from list to show in the table.

Same logic or procedure can be applied for inventory search, location search and many more from the data dump.

Download Display Search results in listbox excel file

5 thoughts on “Excel VBA to Display search result in listbox”

  1. Hello.

    This is a great tutorial!

    This ‘Display Search results in listbox’ excel file is nearly exactly what i am looking for, however i need your help please.

    I have an excel file with 15 worksheets of data (you have just one – ‘Data’). And i am hoping to add more worksheets full of data.

    How can i amend the code to search all worksheets for a name, and then display the results in the listbox please?

    Your help is greatly appreciate.

    Thank you.

    James M

    Reply
  2. Sir i have no excel sheet, there I need to color a cell where I can show with color there booking date with there name. Length of stay or you can say booking calendar.

    Please help lerner like me..

    Many thanks..

    Reply
  3. Hi.. I am Luis.. I have learned Excel and VBA by Self-efforts. Thank you so much for sharing, this Tutorial is Great, and it will surely help me with my little ” tool box” I have gathered with Sub Procedures and Functions from all over. You are Great Thanks again..

    Reply
  4. Hi, I am Luis.. First of All my Thanks for Sharing.. This Article with Sample Codes are Great.. I now understand a lot more of how VBA works.. Sheers

    Reply

Leave a Comment