How to Clear All Data from a VBA UserForm ListBox in Excel
Question details
The user needs a reliable method to remove every item from a VBA UserForm ListBox to reset the form, avoiding compile or index errors caused by incorrect item removal logic.

- Product
- Excel VBA
- Device & OS
- not provided
- Scenario
- Resetting a UserForm for a new time period by emptying the ListBox entirely rather than just removing highlighted items.
- Observed behavior
- Attempting to remove only selected items or using a standard forward loop results in compile errors, index out of bounds errors, or undefined variable references.
Before modifying your VBA code, ensure your ListBox is not bound to a worksheet range via the RowSource property, as bound ListBoxes cannot be cleared using standard VBA methods.
Use the ListBox Clear Method
The simplest and most efficient way to remove all items from an unbound ListBox is by using the built-in Clear method.
Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.
In the Project Explorer panel, right-click the UserForm containing your ListBox and select 'View Code'.
Inside the subroutine for your reset button (e.g., Sub cmdClear_Click), type the command 'lstDatabase.Clear'. Ensure you replace 'lstDatabase' with the actual name of your ListBox control.
Run the UserForm by pressing F5. Click your designated clear button to verify that the ListBox empties completely.

Use a Reverse Loop to Remove Items
If you need to evaluate items before removing them, use a reverse loop to delete items one by one without triggering index errors.
Manage VBA UserForms with WPS Spreadsheet
WPS Office provides robust support for VBA and Macros, allowing you to create customized UserForms, execute scripts, and automate data tasks seamlessly.
- 1. Install WPS Office: Download and install the free WPS Office suite on your computer.
- 2. Open your macro workbook: Launch WPS Spreadsheet and open your .xlsm file containing the UserForm.
- 3. Access the Developer Tab: Navigate to the Developer tab on the ribbon and click on 'VBA Editor'.
- 4. Edit the UserForm: Apply the ListBox Clear method directly within the WPS VBA editor just as you would in Microsoft Excel.

Frequently Asked Questions
Why do I get an index error when using a standard For loop to remove ListBox items?
When you remove an item, the ListBox's total item count decreases, and the remaining indices shift upwards. A standard forward loop will eventually try to reference an index number that no longer exists, causing a runtime error. You must use a reverse loop (Step -1) to avoid this.
Why doesn't the Selected property clear all items?
The Selected property evaluates whether a user has actively clicked and highlighted an item in the UserForm. If you only target selected items in your code, unhighlighted data will remain in the ListBox.
Can I use the Clear method if my ListBox is populated using the RowSource property?
No. If your ListBox is bound to a worksheet range via the RowSource property, utilizing the .Clear method will produce a 'Permission denied' error. To clear a bound ListBox, you must either delete the data in the source worksheet range or set the RowSource property to an empty string.




