VBA & Macro Problems

How to Sort a Multicolumn VBA ListBox by the Third Column

Tauseeq MagsiTauseeq Magsi Nov 2, 2026 869 views

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.

How to Sort a Multicolumn VBA ListBox by the Third Column
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.
Before you start

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.

Solution 1Recommended

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.

1
Load data into an array

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`.

2
Implement the Bubble Sort loop

Create nested `For` loops to compare the third column of each row: `If LbList(i, 2) > LbList(j, 2) Then`.

3
Swap entire rows

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`.

4
Reassign the sorted array

Once the loops finish sorting the data in memory, write the sorted array back to the ListBox interface using `Me.ListBox1.List = LbList`.

Use a VBA Array Bubble Sort Method
Handle Null Values Effectively: If your ListBox contains empty fields, declaring `sTemp` as a `Variant` instead of a `String` prevents the 'Invalid use of Null' error during row swapping.
Free Microsoft Office alternative

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. 1. Download WPS Office: Visit the official WPS website to download the free installer for your operating system.
  2. 2. Install the Suite: Follow the on-screen instructions to quickly install WPS Office on your device.
  3. 3. Open Your VBA Projects: Launch WPS Spreadsheets and open your existing XLSM files to continue working with your macros and userforms.
Free and lightweight office suite for Windows, Mac, and LinuxExcellent compatibility with Microsoft Office formats (XLSX, XLSM, CSV)Familiar user interface for quick and easy migrationAdvanced macro and VBA support available in WPS Spreadsheets
microsoft office alternative - wps office

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.