VBA List Box

Publication Date :

Blog Author :

Edited by :

Download FREE VBA List Box Excel Template and Follow Along!
VBA List Box Excel Template.xlsx

Table Of Contents

arrow

What is List Box in Excel VBA?

The list box in Excel VBA is a list assigned to a variable. This list has various inputs to select from and allows selecting multiple options at once. A list box can be inserted on a UserForm by choosing the list box option.

List boxes use named ranges having certain values. The purpose of using a list box is to display a list of values that users can choose from.

How to Create List Box in Excel?

The steps to create a list box in Excel are listed as follows:

  1. Step 1: Enable the Developer tab on Excel ribbon to access ActiveX controls. In the Excel Options dialog box, click "customize ribbon in Excel" and select the Developer checkbox.
    Step 2: In the Developer tab, click on Insert, followed by the "list box" control. This inserts a new list box in the Excel worksheet.


    List-Box-in-Excel-1

  2. Step 3: Draw a rectangle in the Excel worksheet to create a list box, as shown in the following image.


    List-Box-in-Excel-2

  3. Step 4: Select the list box, go to the Developer tab, and select "properties". The "properties" window opens.


    List-Box-in-Excel-3

  4. Step 5: In the "properties" window, enter "days" in "ListFillRange". "Days" is the named range for weekdays.


    List-Box-in-Excel-4

  5. Step 6: Click the "MultiSelect" property. From the drop-down list, select "1- fmMultiSelectMulti" as shown in the succeeding image.


    List-Box-in-Excel-5

  6. Step 7: Click the "ListStyle" property. From the drop-down list, select "1- fmListStyleOption". This adds checkboxes to the left of the listed items, as shown in the following image.


    List-Box-in-Excel-6

  7. Step 8: Close the "properties" window. The list box appears as shown in the following image.


    List-Box-in-Excel-7

    Note: You can select "design mode off" by clicking Design Mode. This closes the "design mode".

How to Create a List Box in VBA?

The steps to create a list box in VBA are listed as follows:

  • Step 1: Right-click on the list box and make sure the Design Mode is selected. This links the list box to a cell.
  • Step 2: In “properties,” go to the “LinkedCell” property and fill in “B17” as shown in the following image.
List Box in Excel 8
  • Step 3: In “ListFillRange,” enter “Month.”
List Box in Excel 9
  • Step 4: The list box appears as shown in the succeeding image.
List Box in Excel 10

The data in cell B17 changes when you change your choice in the list box. This is shown in the following image.

List Box in Excel 11

How to Create a List Box on a UserForm?

The steps to create a list box on a UserForm are listed as follows:

  • Step 1: In the Developer tab, click “Visual Basic” from the Code or Press Alt+F11.
List Box in Excel 12
  • Step 2: In the Insert menu, click “UserForm.”
List Box in Excel 13
  • Step 3: From the toolbox, drag the list box control on the “UserForm.”
List Box in Excel 14
  • Step 4: Double-click on “UserForm” and select “UserForm event,” as shown in the following screenshot.
List Box in Excel 15
  • Step 5: Add the following code to the in-between procedure.

Private Sub UserForm_Initialize()

ListBox1.AddItem "MBA"

ListBox1.AddItem "MCA"

ListBox1.AddItem "MSC"

ListBox1.AddItem "MECS"

ListBox1.AddItem "CA"

End Sub

List Box in Excel 16
  • Step 6: Press F5 to run the code.

How to Add a Dynamic Control to List Box?

The steps to add a dynamic control to the list box on the UserForm using VBA are listed as follows:

  • Step 1: From the toolbox, add a “CommandButton” on the “UserForm.”
List Box in Excel 17
  • Step 2: Right-click on the “Commandbutton” and click “properties.”
List Box in Excel 18
  • Step 3: Change the caption of the “CommandButton” to “Create_Listbox.”
List Box in Excel 20
  • Step 4: Double-click on the command button and the following code appears.

Private Sub CommandButton1_Click()

End Sub

List Box in Excel 21
  • Step 5: Name the following procedure as “Add_Dynamic_Listbox.”

Sub Add_Dynamic_Listbox()

'Add Dynamic List Box and assign it to object 'LstBx'

Set LstBx = UserForm3.Controls.Add("Forms.ListBox.1")

'List Box Position

LstBx.Left = 20

LstBx.Top = 10

End Sub

  • Step 6: Click F5 to run the macro.

How to add items to list box control in VBA?

The code to add items to list box control in VBA is mentioned as follows:

Private Sub Insert _Items _To_LstBox ()

ListBox1.AddItem "Item 1"

ListBox1.AddItem "Item 2"

ListBox1.AddItem "Item 3"

ListBox1.AddItem "Item 4"

ListBox1.AddItem "Item 5"

End Sub

How to clear items from list box control using VBA?

The code to clear items from the list box control in VBA is mentioned as follows:

Sub Clr_LstBx()

UserForm3.ListBox1.Clear

End Sub

Example

Let us say we have daily sales data. We want to prepare a graphical user interface of this sales data that allows us to select items from a list box. To fetch the sales details, we have to change the Index cell to “cell link” and apply the VLOOKUP formula as shown in the following images.

List Box in Excel 22
List Box in Excel 23

The Constants of List Box in VBA

The constants, their values, and description are given in the following table:

List Box in Excel 24

Frequently Asked Questions (FAQs)

1

How to use a list box in Excel?

Arrow down filled
2

How to populate a list box in Excel VBA?

Arrow down filled
3

How to retrieve data from a list box in Excel?

Arrow down filled
4

What is the difference between a list box and a combo box?

Arrow down filled