Table Of Contents
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:
- 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. - Step 3: Draw a rectangle in the Excel worksheet to create a list box, as shown in the following image.
- Step 4: Select the list box, go to the Developer tab, and select "properties". The "properties" window opens.
- Step 5: In the "properties" window, enter "days" in "ListFillRange". "Days" is the named range for weekdays.
- Step 6: Click the "MultiSelect" property. From the drop-down list, select "1- fmMultiSelectMulti" as shown in the succeeding image.
- 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.
- Step 8: Close the "properties" window. The list box appears as shown in the following image.
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.
- Step 3: In âListFillRange,â enter âMonth.â
- Step 4: The list box appears as shown in the succeeding image.
The data in cell B17 changes when you change your choice in the list box. This is shown in the following image.
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.
- Step 2: In the Insert menu, click âUserForm.â
- Step 3: From the toolbox, drag the list box control on the âUserForm.â
- Step 4: Double-click on âUserFormâ and select âUserForm event,â as shown in the following screenshot.
- 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
- 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.â
- Step 2: Right-click on the âCommandbuttonâ and click âproperties.â
- Step 3: Change the caption of the âCommandButtonâ to âCreate_Listbox.â
- Step 4: Double-click on the command button and the following code appears.
Private Sub CommandButton1_Click()
End Sub
- 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.
The Constants of List Box in VBA
The constants, their values, and description are given in the following table: