VBA Listbox - Selected Item (2024)

In this Article

  • Create List Box in a VBA Form
    • Add Values to the List Box
  • Select Values from the List Box
  • Work with the Selected Values in VBA
    • Assigning the Value to a Variable
    • Use a Command Button to Return the Value to Excel
  • Select Multiple Values

This article will demonstrate how to work with the selected item in a List Box in Excel VBA.

List Boxes show a list of options to users, allowing them to select one or more of the items. They are largely used in VBA forms but can also be used within your Excel worksheet.

Create List Box in a VBA Form

To create a list box in a VBA form, we first need to create the UserForm.

Once you have created your form, select the List Box control in the toolbox and then drag to create a list box on your form.

VBA Listbox - Selected Item (1)

Add Values to the List Box

In the Initialize event of the form, type the following code. The List Box will pick up values that are stored in a Range of Cells in your Excel Worksheet.

Private Sub UserForm_Initialize() Dim rng As Range For Each rng In Range("A1:A50") Me.lstState.AddItem rng.Value Next rngEnd Sub

When we run the form, the list box will be shown as demonstrated in the image below:

VBA Listbox - Selected Item (2)

Select Values from the List Box

By default, a single value can be selected in a List Box in a user form. However this can be amended by changing the Multi-Select property of the list box.

Click on the list box to select it, and then in the Properties window, change the Multi-Select Property from 0-frmMultiSelectSingle to 1-frmMultiSelectMulti.

VBA Listbox - Selected Item (3)

Now when we run the form, we can select more than one option in the List Box.

VBA Listbox - Selected Item (4)

If we change the option to be 2-frmMultiSelectExtended, it means that we can select one of the values, and then, holding down the SHIFT key, select another value further down the list, and all the items between the 2 values selected will also be selected.

VBA Listbox - Selected Item (5)

Work with the Selected Values in VBA

Depending on the type of option we have used for the Multi-Select property in the List Box, there are a number of ways we can use the value or values selected in the list box in VBA Code.

Assigning the Value to a Variable

We can use the After_Update event of the list box to assign the value selected to a variable.

Firstly, let us create a module level variable at the top of the form module.

Underneath the words, Option Explicit, create the following string variable.

Dim strState as String.

Once we have created this variable, we can double-click on the List box to go to the code behind the form, or we can click on the code button in the VBE Editor.

The Click Event of the list box will be automatically created. Select the After_Update Event form the list of Procedures available.

VBA Listbox - Selected Item (6)

In the After_Update event, type the following code:

Private Sub lstState_AfterUpdate() strState = Me.lstStateEnd Sub

NOTE: You can delete the Click event as it is not required.

Now, if we run the form and click on the list box, the selected value will be store in the variable. To test this, we can put a BREAK point in the code.

VBA Listbox - Selected Item (7)

Now when we run the form, if we click on the list box, the code will go into DEBUG mode and stop at our break point. If we then press F8 on the keyboard to move a step further in the code, the variable will be populated with the selected item in the list.

We can view this value by resting the mouse on the variable.

VBA Listbox - Selected Item (8)

OR

We can view the value in the Immediate Window.

VBA Listbox - Selected Item (9)

Use a Command Button to Return the Value to Excel

Firstly, we create a command button on the form in order to have an OK button to return the value or values selected in the list box to Excel.

Select the Command Button control, and then click and drag in your form to create the button.

VBA Listbox - Selected Item (10)

In the Properties window, change the name of the button to cmdOK, and change the Caption and Accelerator of the button.

VBA Listbox - Selected Item (11)

The purpose of the Accelerator is for the user to use the keyboard to activate the button, in this case Alt+O would activate the button.

VBA Listbox - Selected Item (12)

For the command button to work, we need to add code behind it so that when the button is clicked, the code runs. This is called the Click event of the button.

To get to the click event, double-click on the button in the design view of the form. The click event will be automatically created as this is the event that is most commonly used for Command buttons.

Type the following code into the click event of the command button.

Private Sub cmdOK_Click() Range("E1") = strStateEnd Sub

The code will pick up the variable we declared in the After_Update event of the ListBox and return the value to the Range in Excel.

Alternatively, we can pick up the value directly from the List Box without using a variable.

Private Sub cmdOK_Click() Range("E1") = me.lstStateEnd Sub

When we run the form, the selected value will be returned to Excel when we click the OK button.

VBA Listbox - Selected Item (13)

Select Multiple Values

If we have set the multi-select property of the list box to 1 or 2 which allows us to select multiple values in the list, then the code to select these values is slightly different.

The After_Update event is no longer fired when selecting the values in the list box – we therefore cannot use this event.

We can still use the command button click event, but we need to loop through the values selected in the list box in order to return them to Excel.

In the command button Click event, type the following code.

Private Sub cmdOK_Click() Dim x As Integer Range("E1").Select For x = 0 To Me.lstState.ListCount - 1 If Me.lstState.Selected(x) = True Then ActiveCell = Me.lstState.List(x) ActiveCell.Offset(1, 0).Select End If Next xEnd Sub

Now when we run the form, only the values that are selected will be returned to the Excel sheet.

VBA Listbox - Selected Item (14)

VBA Listbox - Selected Item (2024)

References

Top Articles
Latest Posts
Article information

Author: Kieth Sipes

Last Updated:

Views: 6195

Rating: 4.7 / 5 (67 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Kieth Sipes

Birthday: 2001-04-14

Address: Suite 492 62479 Champlin Loop, South Catrice, MS 57271

Phone: +9663362133320

Job: District Sales Analyst

Hobby: Digital arts, Dance, Ghost hunting, Worldbuilding, Kayaking, Table tennis, 3D printing

Introduction: My name is Kieth Sipes, I am a zany, rich, courageous, powerful, faithful, jolly, excited person who loves writing and wants to share my knowledge and understanding with you.