Excel VBA UserForm: How to Select an Item
In this article
To let a user select an item in an Excel VBA UserForm, add a ListBox or ComboBox control, fill it with the AddItem method (or set its RowSource to a range), and then read the user's choice from the control's ListIndex or Value property when the form runs. That is the whole mechanism: the control holds the list, and your code reads which entry was picked.
The steps below use a ListBox on a UserForm, but the same properties apply to a ComboBox. Menu and dialog labels in the VBA editor can differ slightly by Excel version and by language, so if a label does not match exactly, look for the closest equivalent.
What You'll Need
- Excel on Windows or Mac with the Developer tab enabled. On Windows: File > Options > Customize Ribbon, then tick Developer. On Mac: Excel > Preferences > Ribbon & Toolbar, then tick Developer.
- A workbook saved as
.xlsm(macro-enabled) so your UserForm code is kept. - Basic familiarity with the VBA editor: press Alt+F11 on Windows, or Option+F11 on Mac, to open it.
Step-by-Step
- Insert a UserForm. In the VBA editor, right-click your project in the Project Explorer, choose Insert > UserForm. A blank form appears. If the Toolbox is not visible, choose View > Toolbox.
- Add a ListBox. Click the ListBox icon in the Toolbox (it looks like a small list) and drag it onto the form. Resize it so several rows are visible. If you prefer a drop-down, use the ComboBox instead; the code below works the same way.
- Name the control. With the ListBox selected, open the Properties window (F4). Change
(Name)to something meaningful such aslstItems. This name is what your code will refer to. - Fill the list when the form loads. Right-click the form and choose View Code, then paste a
UserForm_Initializeprocedure that adds items. For example:Private Sub UserForm_Initialize()With Me.lstItems.AddItem "Apples".AddItem "Pears".AddItem "Plums"End WithEnd Sub
Alternatively, set the ListBox'sRowSourceproperty to a worksheet range such asSheet1!A1:A10in the Properties window, which fills the list without any code. - Let the user pick one item. By default a ListBox allows a single selection. Leave
MultiSelectat0 - fmMultiSelectSingleso only one row can be chosen at a time. For a ComboBox, the user picks from the drop-down. - Read the selection. Add a command button (from the Toolbox) named
cmdOKand put this in its Click event:Private Sub cmdOK_Click()If Me.lstItems.ListIndex = -1 ThenMsgBox "Please select an item first."Exit SubEnd IfMsgBox "You selected: " & Me.lstItems.ValueEnd SubListIndexis the zero-based position of the chosen row, and-1means nothing is selected.Valuereturns the text of the selected item, which is usually what you want to display or store. - Show the form from a normal module. Insert a module (Insert > Module) and add:
Sub ShowPicker()UserForm1.ShowEnd Sub
Run it with F5. The form appears, the list is populated, and clicking OK reports the chosen item. - Store the result where you need it. If you want the selection back in the worksheet, declare a public variable at the top of the form's code module, set it in the Click event before
Unload Me, and read it afterShowreturns in your calling macro.
Common Mistakes
- Forgetting that ListIndex starts at 0. The first item is index 0, not 1. Off-by-one errors are the most common source of picking the wrong row.
- Treating -1 as a valid selection.
-1means no item is selected. Always test for it before usingValue, or you will store an empty string. - Using the wrong control name. If you renamed the ListBox to
lstItems, code that refers toListBox1will fail with a compile or run-time error. - Mixing up ListBox and ComboBox properties. Both share
ListIndexandValue, but a ComboBox also hasText, and behaviour differs while the user is typing. Stick toValuefor the confirmed selection. - Filling the list in the wrong event. Use
UserForm_Initializefor a form shown withShow. If you reload the form without unloading it,Initializedoes not run again; useActivateif you need to refresh items each time. - Leaving MultiSelect on. With multi-select enabled,
ListIndexonly reflects one of the selected rows. For a single choice, keep it set to single.
Tips
- Use
RowSourcefor lists that live on a worksheet and change often; useAddItemfor short, fixed lists. - Set
ColumnCountandColumnWidthson the ListBox to show several columns from a range without extra code. - Use
ListIndexwhen you need the position (for example to look up a matching row in a table) andValuewhen you only need the text. - Add a label next to the control so users know what they are choosing; a bare list is easy to misread.
- Test on both Windows and Mac if your workbook is shared, because some control behaviour and shortcut keys differ.
Troubleshooting
If the form opens but the list is empty, check that your Initialize code runs (put a MsgBox at the top temporarily) and that RowSource points to the correct sheet and range. If clicking OK does nothing, confirm the button's Click event contains code and that the button name matches. If you get a "Could not find the specified object" error, a control name in your code does not match the name in the Properties window. If Value returns blank, the user has not selected anything and ListIndex is -1; add the guard shown in step 6. If the macro will not run at all, check that macros are enabled and the file is saved as .xlsm. If the form does not appear, make sure you called UserForm1.Show from a standard module, not from the form's own code.
Frequently Asked Questions
How do I get the selected item's text in VBA?
Use Me.lstItems.Value after checking that ListIndex is not -1.
Why is ListIndex 0 for the first item?
VBA collections and list controls are zero-based, so the first row is index 0 and the last is ListCount - 1.
Can a ListBox allow multiple selections?
Yes. Set MultiSelect to 1 - fmMultiSelectSimple or 2 - fmMultiSelectExtended, then loop through ListCount and test Selected(i) for each row.
What is the difference between ListBox and ComboBox for selecting an item?
A ListBox shows all options at once; a ComboBox shows one line and expands when clicked. Both expose ListIndex and Value, so the selection code is the same.
How do I clear the selection after use?
Set Me.lstItems.ListIndex = -1 to deselect, or call Me.lstItems.Clear to remove all items before refilling.
Can I fill the list from a named range?
Yes. Set RowSource to the range name, such as ItemList, in the Properties window or in code.