Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA UserForm: How to Select an Item

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. 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.
  2. 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.
  3. Name the control. With the ListBox selected, open the Properties window (F4). Change (Name) to something meaningful such as lstItems. This name is what your code will refer to.
  4. Fill the list when the form loads. Right-click the form and choose View Code, then paste a UserForm_Initialize procedure that adds items. For example:
    Private Sub UserForm_Initialize()
        With Me.lstItems
            .AddItem "Apples"
            .AddItem "Pears"
            .AddItem "Plums"
        End With
    End Sub
    Alternatively, set the ListBox's RowSource property to a worksheet range such as Sheet1!A1:A10 in the Properties window, which fills the list without any code.
  5. Let the user pick one item. By default a ListBox allows a single selection. Leave MultiSelect at 0 - fmMultiSelectSingle so only one row can be chosen at a time. For a ComboBox, the user picks from the drop-down.
  6. Read the selection. Add a command button (from the Toolbox) named cmdOK and put this in its Click event:
    Private Sub cmdOK_Click()
        If Me.lstItems.ListIndex = -1 Then
            MsgBox "Please select an item first."
            Exit Sub
        End If
        MsgBox "You selected: " & Me.lstItems.Value
    End Sub
    ListIndex is the zero-based position of the chosen row, and -1 means nothing is selected. Value returns the text of the selected item, which is usually what you want to display or store.
  7. Show the form from a normal module. Insert a module (Insert > Module) and add:
    Sub ShowPicker()
        UserForm1.Show
    End Sub
    Run it with F5. The form appears, the list is populated, and clicking OK reports the chosen item.
  8. 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 after Show returns 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. -1 means no item is selected. Always test for it before using Value, or you will store an empty string.
  • Using the wrong control name. If you renamed the ListBox to lstItems, code that refers to ListBox1 will fail with a compile or run-time error.
  • Mixing up ListBox and ComboBox properties. Both share ListIndex and Value, but a ComboBox also has Text, and behaviour differs while the user is typing. Stick to Value for the confirmed selection.
  • Filling the list in the wrong event. Use UserForm_Initialize for a form shown with Show. If you reload the form without unloading it, Initialize does not run again; use Activate if you need to refresh items each time.
  • Leaving MultiSelect on. With multi-select enabled, ListIndex only reflects one of the selected rows. For a single choice, keep it set to single.

Tips

  • Use RowSource for lists that live on a worksheet and change often; use AddItem for short, fixed lists.
  • Set ColumnCount and ColumnWidths on the ListBox to show several columns from a range without extra code.
  • Use ListIndex when you need the position (for example to look up a matching row in a table) and Value when 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.