• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Microsoft Excel Tips and Tricks - Computergaga

  • Home
  • Excel Online Courses
  • Excel Functions List
  • Free Downloads
  • Blog
  • Tutorials
  • About
Home / VBA / Display Search Results in a ListBox – Excel VBA

Display Search Results in a ListBox – Excel VBA

In this blog post, we will look at showing search results in a ListBox of a userform in Excel VBA.

We want a user to type into a text box, and the search results of that entry to appear in a ListBox. A search result can then be selected and added to a row on a spreadsheet.

This is the example.

We have the form below. The user enters the ID of a product, and VLOOKUP formulas return the related information from a product list into the other columns.

Form on a spreadsheet that has an advanced search feature

Sometimes though, the user does not know the product ID.

If so, they can click on the Advanced Search button. This opens the form below, where they can enter keywords to search for the product they need.

Then our VBA code will return the search results to the ListBox, and add the product they select to the form on the spreadsheet.

Search results in a ListBox on a VBA userform

Watch the Video – Display Search Results in a ListBox

This video tutorial will take you through the entire process. You can also read on to see the VBA code and techniques used.

Inserting the ListBox Control

The first task is the insert the ListBox control and to set some key properties for it.

This tutorial is assuming that the other parts of the userform (text box and buttons) have already been created. This is done to keep this tutorial shorter and to the point.

Click on the ListBox control button and draw it onto the userform.

Inserting a ListBox on a userform

With the ListBox inserted, we shall change some important properties in the Properties Window (if you do not see this, click View > Properties Window).

Setting the ListBox properties

The Name is changed to lstSearchResults so that it is easy to reference when writing the VBA code.

The ColumnCount setting is set to 3. This is because we want to show 3 columns of information in the ListBox (ID, Product Name and Quantity Per Unit)

The ColumnHeads property is set to True. This will show the first row above the row source selection as headers.

The ColumnWidths have been set to 20pt, 200pt and 80pt. This has been tested to work nicely with the data used.

Creating a Dynamic Named Range for the Search Results

We now want to create a dynamic named range for the search results. This will ultimately be used as the row source to populate the ListBox.

We are using a worksheet named Product Search. This worksheet is to be filled with all the products that meet the search criteria, and is then used to populate the ListBox.

The dynamic named range will give us a way of easily referencing this range.

Click the Formulas tab and the Define Name button.

Type a name for the range. We have used SearchResults.

Then this formula is used for the Refers to field.

=OFFSET('Product Search'!$A$2,0,0,COUNTA('Product Search'!$A:$A)-1,3)
A dynamic named range for the search results in a ListBox

Add the VBA Code for the Search Button

We now need some VBA code to return the search results to the ListBox dependent upon the keywords in the text box.

The code used can be seen below.

Private Sub cmdSearch_Click()
Dim RowNum As Long
Dim SearchRow As Long
RowNum = 2
SearchRow = 2
Worksheets("Stock Data").Activate
Do Until Cells(RowNum, 1).Value = ""
If InStr(1, Cells(RowNum, 2).Value, txtKeywords.Value, vbTextCompare) > 0 Then
Worksheets("Product Search").Cells(SearchRow, 1).Value = Cells(RowNum, 1).Value
Worksheets("Product Search").Cells(SearchRow, 2).Value = Cells(RowNum, 2).Value
Worksheets("Product Search").Cells(SearchRow, 3).Value = Cells(RowNum, 3).Value
SearchRow = SearchRow + 1
End If
RowNum = RowNum + 1
Loop
If SearchRow = 2 Then
MsgBox "No products were found that match your search criteria."
Exit Sub
End If
lstSearchResults.RowSource = "SearchResults"
End Sub

The Instr VBA function has been used to test if there is a match between the words written in the text box (txtKeywords) and each row of the products list.

Each time there is a match, the product is written to the Product Search sheet (where our dynamic named range will be pick it up).

The last statement then assigns this dynamic named range, which I called SearchResults, as the RowSource of the ListBox.

If you are new to Excel VBA and this seems too much. Enrol in our Excel VBA course for beginners to get up to speed fast.

VBA to Add the Product to the Form

Now we need some VBA code for the Add Product button.

Private Sub cmdAdd_Click()
Dim RowNum As Long
Dim ListBoxRow As Long
Worksheets("Form").Activate
RowNum = Application.CountA(Range("A:A")) + 2
ListBoxRow = lstSearchResults.ListIndex + 2
Cells(RowNum, 1).Value = Worksheets("Product Search").Cells(ListBoxRow, 1).Value
Unload Me
End Sub

The ListIndex property of the ListBox has been used here to detect which product the user selected.

This code has also been used on the Initialize event of the userform to clear the Product Search worksheet each time the form is opened. And to set the text box as the active field when the form is opened.

Private Sub UserForm_Initialize()
txtKeywords.SetFocus
Worksheets("Product Search").Range("A2:C100").ClearContents
End Sub

This example can be adapted as per your requirements, and serves as a realistic example of populating a ListBox with search results.

Download a copy of the completed display search results in a ListBox file.

Related Posts:

  • Moving average in Excel thumbnail
    Moving Average in Excel
  • Custom sort in Excel
    Custom Sort in Excel
  • Sum formulas only in Excel
    Sum Formulas Only in Excel
  • SWITCH function feature
    Excel SWITCH Function - Nested IF Function Alternative

Reader Interactions

Comments

  1. Sandeep Kothari says

    19 June 2018 at 1:13 pm

    Dear Allan
    Great article!

    Reply
    • computergaga says

      19 June 2018 at 3:05 pm

      Thanks Sandeep.

      Reply
  2. asim says

    19 June 2018 at 4:14 pm

    very informative

    Reply
    • computergaga says

      19 June 2018 at 5:59 pm

      Thank you Asim.

      Reply
  3. Didiek says

    22 June 2018 at 7:52 am

    Very helpfull. Nice to have this information

    Reply
    • computergaga says

      22 June 2018 at 8:23 am

      Thanks Didiek.

      Reply
  4. Robert Mofle says

    18 June 2020 at 12:44 am

    Why am i getting this debug error?
    lstSearchResults.RowSource = “SearchResults”

    Reply
  5. arash says

    23 October 2022 at 9:58 pm

    Hello, I really do not know how to thank you
    I tried several times, but I could not write this macro correctly
    Thank you very much for the possibility to download the file
    Thank you a million times
    Thank you so much
    arash

    Reply
    • Alan Murray says

      12 January 2023 at 10:07 pm

      You’re very welcome.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Posts

  • Excel SWITCH Function – Nested IF Function Alternative
  • Sum Formulas Only in Excel
  • How to Calculate Age in Excel
  • Custom Sort in Excel
  • Case Sensitive Lookup with XLOOKUP in Excel
Advanced Excel Success book

Footer

Contact

[email protected]

LinkedIn Twitter YouTube

Useful Links

  • Home
  • Excel Online Courses
  • Tutorials
  • Sitemap
  • Privacy Policy
  • Jobs by Jooble

Course Topics

  • Blog
  • Charts
  • Courses
  • Dashboards
  • Downloads
  • Excel Tips
  • Formulas
  • Mail Merge
  • Office
  • PivotTables
  • Power BI
  • Power Pivot
  • Power Query
  • VBA

Join Our Mailing List

© Copyright 2009 Computergaga · All Rights Reserved ·