How to Sort a Multicolumn VBA ListBox by the Third Column
Question details
The user needs to sort an Access or VBA multicolumn ListBox based on the values in the third column (such as an account-holder name) while preserving the relationship between the data across all columns.

- Product
- VBA / Excel / Access
- Device & OS
- not provided
- Scenario
- Organizing custom user interfaces or userforms in VBA where a ListBox displays multiple columns of data, and the data must be sorted chronologically or alphabetically by a specific column.
- Observed behavior
- The ListBox displays data unsorted or sorted by the primary column by default. A custom script is required to extract the data, sort it by index 2, and write it back without triggering 'Invalid use of Null' errors.
Ensure you have saved a backup copy of your database or workbook, and verify that your ListBox is fully populated with data before running the sorting macro.
Use a VBA Array Bubble Sort Method
Extract the ListBox data into a 2D array, sort the array based on the third column, and reassign the sorted data to the ListBox.
By loading the ListBox contents into an array, you can use a bubble sort algorithm to compare the values in the third column (index 2). When a swap is needed, the entire row is swapped to preserve data integrity across all columns. Declaring the temporary swap variable as a Variant helps prevent errors when dealing with mixed data types.
In your VBA Editor, declare your variables and assign your ListBox data to a Variant array using: `Dim LbList As Variant` followed by `LbList = Me.ListBox1.List`.
Create nested `For` loops to compare the third column of each row: `If LbList(i, 2) > LbList(j, 2) Then`.
If the condition is met, loop through all columns `k` and swap values using a Variant temporary variable: `sTemp = LbList(i, k)`, `LbList(i, k) = LbList(j, k)`, `LbList(j, k) = sTemp`.
Once the loops finish sorting the data in memory, write the sorted array back to the ListBox interface using `Me.ListBox1.List = LbList`.

Add a Secondary Sort Key for Duplicates
Enhance the sorting logic to handle duplicate names in the third column by falling back to the first column for secondary sorting.
Looking for a Lightweight Alternative with Excellent Macro Support?
While Microsoft Access and Excel offer robust VBA tools, they can be costly and resource-heavy. WPS Office provides a free, lightweight alternative that boasts excellent compatibility with Microsoft Office formats, including robust support for Excel macros and VBA in its premium versions, ensuring a seamless transition for your spreadsheets.
- 1. Download WPS Office: Visit the official WPS website to download the free installer for your operating system.
- 2. Install the Suite: Follow the on-screen instructions to quickly install WPS Office on your device.
- 3. Open Your VBA Projects: Launch WPS Spreadsheets and open your existing XLSM files to continue working with your macros and userforms.

Frequently Asked Questions
Why do I get an 'Invalid use of Null' error when sorting my ListBox?
This error occurs if a cell in your ListBox is empty and you try to assign its value to a strongly typed variable like String or Integer during the swapping process. Declaring your temporary swap variable (e.g., `sTemp`) as a Variant resolves this issue.
How can I specify which column to sort by in VBA?
In VBA arrays and ListBoxes, columns are zero-indexed. To sort by the third column, you compare the items using index 2 (e.g., `LbList(i, 2)`). To sort by the first column, you would use index 0.
Can I sort a VBA ListBox directly without using an array?
VBA ListBoxes do not have a built-in sort method. The most efficient approach is to extract the `.List` property into a 2D array, sort the array in memory using standard sorting algorithms, and reassign it to the ListBox.
How do I handle duplicate values when sorting a ListBox?
You can add a secondary sort key within your sorting loop. If the primary column values match during the comparison, the code should check a secondary column (like column 1, index 0) to determine the final sort order.




