VBA & Macro Problems

Filter All Columns in an Excel VBA UserForm ListBox

Algirdas JasaitisAlgirdas Jasaitis Nov 18, 2026 870 views

Question details

Filter a multi-column UserForm ListBox using text entered in a TextBox and select the first matching row.

Filter All Columns in an Excel VBA UserForm ListBox
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A UserForm displays worksheet records in a multi-column ListBox and needs to search across the displayed columns.
Observed behavior
As the user types, the ListBox should show matching records and select the first result when one exists.
Before you start

Use desktop Excel with macros enabled and save the workbook as an Excel Macro-Enabled Workbook (.xlsm). Confirm the worksheet, source columns, TextBox name, and ListBox name before adapting the code.

Solution 1Recommended

Filter the ListBox in the TextBox Change event

Use this approach to search each source column as the user types and rebuild the ListBox with matching rows.

This example searches columns A through D on a worksheet named Data. It assumes headers are in row 1 and every source record has a value in column A. Matching is case-insensitive and includes partial text matches.

1
Open the UserForm code

In desktop Excel, open Developer > Visual Basic. In the VBA editor, locate your UserForm in Project Explorer and open its code window. The example uses controls named TextBox1 and ListBox1.

2
Add the filtering code

Paste this code into the UserForm code window, replacing any existing procedures with the same event names: Private Sub UserForm_Initialize() ListBox1.ColumnCount = 4 TextBox1_Change End Sub Private Sub TextBox1_Change() Dim ws As Worksheet Dim lastRow As Long, r As Long, c As Long Dim searchText As String Dim v As Variant Dim matched As Boolean Set ws = ThisWorkbook.Worksheets("Data") lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row searchText = CStr(TextBox1.Value) ListBox1.Clear For r = 2 To lastRow matched = (Len(searchText) = 0) For c = 1 To 4 v = ws.Cells(r, c).Value2 If Not IsError(v) Then If InStr(1, CStr(v), searchText, vbTextCompare) > 0 Then matched = True Exit For End If End If Next c If matched Then ListBox1.AddItem For c = 1 To 4 v = ws.Cells(r, c).Value2 If IsError(v) Then ListBox1.List(ListBox1.ListCount - 1, c - 1) = "#ERROR" Else ListBox1.List(ListBox1.ListCount - 1, c - 1) = CStr(v) End If Next c End If Next r If ListBox1.ListCount > 0 Then ListBox1.ListIndex = 0 End Sub

3
Adapt the source and controls

In the code window, replace Data, TextBox1, and ListBox1 with your actual names. If you change the number of source columns, update ColumnCount and both For c = 1 To 4 loops. Set the ListBox MultiSelect property to 0 - fmMultiSelectSingle in the Properties window.

4
Run and test the form

Select the UserForm in the VBA editor and press F5. Type text from different columns into the TextBox, check that matching rows appear, and clear the text to show all records again.

Filter the ListBox in the TextBox Change event
Selection and focus: ListIndex = 0 selects the first result without moving keyboard focus away from the TextBox, so the user can continue typing.
Free Microsoft Office alternative

Work with spreadsheet data in WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for common Excel formats, making it easier to move everyday spreadsheet work. It does not fix an Excel VBA UserForm, and macro compatibility can vary.

Open and edit Excel XLSX and XLS spreadsheet filesSearch worksheet data without building a VBA UserFormUse a familiar spreadsheet interface for everyday tasksMove common spreadsheet workflows to a lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

Why does the ListBox show every row when the search box is empty?

The code treats an empty TextBox as a request to show all source records. Type a search term into TextBox1 to narrow the list.

How do I search more than four columns?

Update ListBox1.ColumnCount and change both For c = 1 To 4 loops to use the desired last column number. Make sure the worksheet contains data in those columns.

Why are some source rows missing from the results?

The example finds the last source row using column A. Ensure column A contains a value for every record, or change the lastRow calculation to use a column that does.

Can I select the first result without interrupting typing?

Yes. The code sets ListBox1.ListIndex = 0 when results exist but does not call SetFocus, leaving keyboard focus in the TextBox.