A VBA ListBox is a control you place on an Excel UserForm (or embed directly on a worksheet) that displays a list of items the user can pick from, and you fill and read it with a few lines of VBA. The shortest working recipe is: insert a UserForm, drag a ListBox onto it, add items in the UserForm's Initialize event with ListBox1.AddItem, then read the selection with ListBox1.ListIndex or ListBox1.Value. Everything below expands that recipe and covers the parts that trip people up, including multi-select, populating from a worksheet range, and clearing items without duplicating them.
What You'll Need
- Excel on Windows or Mac with the Visual Basic Editor available (press Alt+F11 on Windows; on Mac use Tools > Macro > Visual Basic Editor, or the Developer tab if you have enabled it).
- The Developer tab visible: File > Options > Customize Ribbon > tick Developer. On Mac: Excel > Preferences > Ribbon & Toolbar.
- A workbook saved as .xlsm (macro-enabled) once you add code, otherwise your VBA is discarded on close.
- Macro security set to allow macros in trusted locations, or you will get a security prompt each time. Check under File > Options > Trust Center > Trust Center Settings > Macro Settings.
- Basic familiarity with opening the VBA Editor and inserting a module. No add-ins are required.
Note that menu labels can differ slightly between Excel versions, platforms and regions, so if a path looks different, look for the closest equivalent.
Step-by-Step
- Open the VBA Editor. Press Alt+F11 (Windows) or use the Developer tab > Visual Basic. The editor opens in its own window.
- Insert a UserForm. In the editor, choose Insert > UserForm. A blank form appears and the Toolbox should show alongside it. If the Toolbox is missing, use View > Toolbox.
- Add the ListBox. Click the ListBox icon in the Toolbox (it looks like a small box with lines), then drag a rectangle on the form. Excel names it ListBox1 by default. Resize it so several rows are visible.
- Set useful properties. With the ListBox selected, open the Properties window (F4). Set
ColumnCountif you want multiple columns,MultiSelectto1 - fmMultiSelectMultior2 - fmMultiSelectExtendedif users should pick more than one item, andListStyleto1 - fmListStyleOptionif you want option buttons beside each row. - Add items in code. Double-click the UserForm itself (not the ListBox) to open its code window. In the
UserForm_Initializeprocedure, add lines such asListBox1.AddItem "Apples"andListBox1.AddItem "Pears". The Initialize event runs each time the form is shown, so this is the right place to load data. - Populate from a worksheet range instead. If your items live in cells, you can assign the range directly:
ListBox1.List = Sheets("Sheet1").Range("A2:A10").Value. This fills the list in one line and is faster than looping AddItem for large lists. The range must be a single column or a rectangular block matching ColumnCount. - Read the user's choice. Add a CommandButton to the form, double-click it, and in its Click event read
ListBox1.ListIndex. It returns 0 for the first item, 1 for the second, and -1 when nothing is selected.ListBox1.Valuegives the text of the selected item. For multi-select, loop from 0 toListBox1.ListCount - 1and testListBox1.Selected(i). - Show the form. In a normal module, write a short macro such as
Sub ShowForm()followed byUserForm1.ShowandEnd Sub. Run it with F5 or assign it to a button on the sheet via Developer > Insert > Button (Form Control). - Save and test. Save as .xlsm, close the form, and run the macro. Click through the list, confirm the value your button writes to a cell or message box is the one you expected.
Common Mistakes
- Duplicated items. If you load the list in a button's Click event rather than Initialize, items pile up every time the button is pressed. Either load in Initialize or call
ListBox1.Clearbefore adding. - Off-by-one errors.
ListIndexis zero-based while worksheet rows are one-based, so item 0 corresponds to row 2 if your data starts in A2. Add the header offset deliberately. - Forgetting MultiSelect. Reading only
ListBox1.Valueon a multi-select list returns the first selected item and silently ignores the rest. - Assuming a selection exists. If the user closes the form without clicking,
ListIndexis -1 andValueis an empty string; code that indexes an array with -1 throws a runtime error. - Losing code on save. Saving as .xlsx strips all VBA. Always use .xlsm.
- Naming confusion. Renaming a control in the Properties window does not update existing code references, so the old name fails to compile.
Tips
- Set
ColumnWidths(for example"60;120") when using multiple columns, otherwise wide text is clipped. - Use
ListBox1.List = Range(...).Valuefor bulk loads; it is dramatically faster than a loop of AddItem calls on lists of hundreds of rows. - Sort or filter your source range before assigning it, since the ListBox displays data in the order given and has no built-in sorting.
- Give controls meaningful names (lstFruit rather than ListBox1) before you write code, and keep a consistent prefix such as lst, cmd and txt.
- Use
ListBox1.ListCountto size loops rather than hard-coding a number. - If you need a dropdown rather than an always-visible list, a ComboBox behaves almost identically in code but takes less space.
Troubleshooting
If the list appears empty, first confirm the Initialize code is actually in the UserForm's code module, not a standard module, and that the control name matches exactly. If you see a compile error, check for a renamed control or a missing reference.
If the form will not open at all, macro security is the usual cause: enable macros for the file or move the workbook to a trusted location. If nothing happens when you run the macro, make sure the module containing UserForm1.Show is a standard module and that the form name is correct.
If values land in the wrong cell, print ListIndex to a message box to see the actual index before trusting your row arithmetic. If the list shows only one column when you expected several, set ColumnCount before assigning the range and make sure the source range has that many columns.
If the file prompts about lost macros after reopening, it was saved in the wrong format; use Save As and choose .xlsm. As a last resort, export the form (File > Export File in the editor) before making structural changes so you can restore it.
Frequently Asked Questions
What is the difference between ListBox and ComboBox in VBA?
A ListBox shows all items at once and can allow multiple selections; a ComboBox shows one item plus a dropdown arrow and normally allows a single choice. Their item-loading and reading code is nearly identical.
How do I clear all items from a ListBox?
Call ListBox1.Clear. To remove one row, use ListBox1.RemoveItem with the zero-based index.
Can I put a ListBox directly on a worksheet without a UserForm?
Yes. Use Developer > Insert > List Box (ActiveX Control) and set its ListFillRange property to a range, or fill it in code. ActiveX controls can behave inconsistently across platforms, so a UserForm is often more reliable.
Why does ListIndex return -1?
Nothing is selected. Either the user has not clicked an item yet or you cleared the list. Check for -1 before using the index.
How do I get every selected item in a multi-select ListBox?
Loop from 0 to ListCount - 1 and collect each item where Selected(i) is True. Reading Value only returns one item.
Does the ListBox sort itself?
No. Sort the source data first, or build a sorted array in code and assign it to ListBox1.List.