ListBox is one of the UserForm control. You can select and drag ListBox on the UserForm. This control is used to display list of items to a list. This is used on the UserForm. Please find more details about ListBox_Control in the following chapter. You can see how to load items to listbox_Control, how to move items from one listbox to another listbox, how to select items from a listbox_Control, etc..,
In this topic:
- VBA ListBox_Control on the UserForm
- Add Dynamic ListBox_Control on the UserForm Using VBA
- Add Items to ListBox_Control Using VBA
- Clear Items from the ListBox_Control Using VBA
- Check if a ListBox Item is Selected or not Using VBA
- VBA ListBox Default Values in Excel
- Get the total count of ListBox Items
- Move all Items from ListBox1 to ListBox2
- Get Selected Items from ListBox1 to ListBox2
- Make ListBox to Select Multiple Items
- Populate ListBox from an Array
- More Details About the ListBox_Control
VBA ListBox_Control on the UserForm
Please find more details about VBA ActiveX ListBox_Control and how we are adding it on the UserForm.
- Go To Developer Tab and then click Visual Basic from the Code or Press Alt+F11.
- Go To Insert Menu, Click UserForm. Please find the screenshot for the same.
- Drag Listbox_Control on the Userform from the Toolbox. Please find the screen shot for the same.
- Double Click on the UserForm, and select the Userform event as shown in the below screen shot.
- Now can see the following code in the module. Private Sub UserForm_Initialize[] End Sub
- Now, add the following code to the in between procedure.
- Now, Press F5 to run the code. You can see the following Output. It is shown in the following Screen Shot.
Code:
Private Sub UserForm_Initialize[] ListBox1.AddItem "MBA" ListBox1.AddItem "MCA" ListBox1.AddItem "MSC" ListBox1.AddItem "MECS" ListBox1.AddItem "CA" End SubAdd Items to ListBox_Control using VBA
Please find the following code, it will show you how to add list items to list box.
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 SubIn the above code ListBox1 is the name of the listbox_Control. Where additem is the property of listbox.
Clear Items from the ListBox using VBA
Please find the following code, it will show you how to clear the list box items. The below code clears the list box1 items on the UserForm1.
Sub Clr_LstBx[] UserForm3.ListBox1.Clear End SubCheck if a List box item is selected or not using VBA
Please find the below code to know how to check if a List box is selected or not using VBA. In the below example [0] is the index number.
Sub Chk_Item_SelectOrNot[] If UserForm3.ListBox1.Selected[0] = True Then MsgBox "First item has selected in the ListBox." Else MsgBox "First item has not selected in the ListBox." End If End SubVBA ListBox Default Values in Excel
Here is the VBA list box default values in Excel. After adding items to list box by using any of the below code you can define the default value.
Code 1:
The below code is useful to select blank option in list box. Where -1 is the index number.
Sub LstBx_Dflt_Val_Ex1[] UserForm3.ListBox1.ListIndex = -1 End SubCode 2:
The below code is useful to select first item in the list box from the available list. . Where 0 is the index number.
Sub LstBx_Dflt_Val_Ex2[] UserForm3.ListBox1.ListIndex = 0 End SubCode 3:
The below code is useful to select second item in the list box from the available list. Where 1 is the index number.
Sub LstBx_Dflt_Val_Ex3[] UserForm3.ListBox1.ListIndex = 1 End SubCode 4:
The below code is useful to select the last item in the list box from the available list. Where 1 is the index number.
Sub LstBx_Dflt_Val_Ex4[] UserForm3.ListBox1.ListIndex = UserForm3.ListBox1.Count - 1 End SubGet the total count of Listbox Items
Here is the following example, it will show you how to get the total count of items in a list box. In the below example ListBox1 is the list box name and ListCount is the property of list box.
Sub Get_Ttl_Cnt[] MsgBox "Total Items in a ListBox is " & UserForm3.ListBox1.ListCount End SubOutput:
Move all Items from ListBox1 to ListBox2
Please find the below example code, it shows how to Move all Items from ListBox1 to ListBox2. In the below example ListBox1 and ListBox2 are the list box names.
Sub Move_ListBox_Items[] 'Variable declaration Dim iCnt As Integer 'Moving ListBox1 Items to ListBox2 For iCnt = 0 To ListBox1.ListCount - 1 ListBox2.AddItem ListBox1.List[iCnt] Next iCnt 'Then Clear the ListBox1 Items ListBox1.Clear End SubGet Selected Items from ListBox1 to ListBox2
Please find the below example code, it shows how to Get Selected Items from ListBox1 to ListBox2. In the below example ListBox1 and ListBox2 are the list box names.
Sub Get_ListBox_Selected_Items[] 'Variable declaration Dim iCnt As Integer 'Get Selcted Items from ListBox1 to ListBox2 For iCnt = 0 To ListBox1.ListCount - 1 'Check ListBox Item has selcted or not If ListBox1.Selected[iCnt] = True Then ListBox2.AddItem ListBox1.List[iCnt] End If Next End SubMake ListBox to Select Multiple Items
Please find the below example code, it shows how to make ListBox to Select Multiple Items. In the below example ListBox1 is the list box name.
Populate ListBox from an Array
Please find the below example code, it shows how to populate ListBox from an Array. In the below example arrList is the array name. And ListBox1 is the list box name.
Sub Load_ListBox_From_Array[] 'Load Items to an Array arrList = Array["Item 1", "Item 2", "Item 3"] 'Load an Array Items to ListBox ListBox1.List = arrList End SubMore Details About the ListBox_Control
VBA ListBox Excel Macros Examples Codes Adding Clearing Multiple Items
Please find the following link for more details about VBA ListBox Excel Macros Examples and Codes Adding and Clearing Multiple Items.
Read More
VBA to Remove Duplicates in ListBox Excel
Please find more details about Remove Duplicates in ListBox in Excel VBA.
Read More
Excel VBA FAQs: Frequently Asked Questions
Please find the most frequently asked questions and answers for your reference. These are explained more detailed way with examples.
Read More
Premium Project Management Templates
120+ Professional Project Management Templates!
A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.
PREMIUM TEMPLATES
LIMITED TIME OFFER
ON SALE80% OFF
BROWSE ALL TEMPLATES
Advanced Project Planning Templates
Excel Templates
VIEW DETAILS
120+ Project Management Templates Pack
Excel | PowerPoint | Word
VIEW DETAILS
ULTIMATE RESOURCE MANAGEMENT TEMPLATE
Excel Template
VIEW DETAILS
50+ Essential Project Management Templates
Excel | PowerPoint | Word
VIEW DETAILS
Project Portfolio ManagementTemplates
Excel | PowerPoint Templates
VIEW DETAILS
50+ Excel Project Management Templates
Excel Templates
VIEW DETAILS
Share This Story, Choose Your Platform!