REAL-TIME

VBA Projects

Full Access with Source Code

  • Designed and Developed by PNRao

  • Full Access with VBA Source Code

  • Well Commented Codes Lines

  • Creative and Professional Design

120+ PROFESSIONAL

Project Management Templates

120+ PM Templates Includes:
  • 50+ Excel Templates

  • 50+ PowerPoint Templates

  • 25+ Word Templates

Effortlessly Manage Your Projects

Seamlessly manage your projects with our powerful & multi-purpose templates for project management.

Share Post

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..,

VBA ListBox_Control on the UserForm

Please find more details about VBA ActiveX ListBox_Control and how we are adding it on the UserForm.

    1. Go To Developer Tab and then click Visual Basic from the Code or Press Alt+F11.
    2. Go To Insert Menu, Click UserForm. Please find the screenshot for the same.

Excel VBA UserForm CheckBox

    1. Drag Listbox_Control on the Userform from the Toolbox. Please find the screen shot for the same.

List Box Excel ActiveX Control _Im10

    1. Double Click on the UserForm, and select the Userform event as shown in the below screen shot.

Excel VBA ListBox ActiveX Control

    1. Now can see the following code in the module.
Private Sub UserForm_Initialize()

End Sub
    1. Now, add the following code to the in between procedure.

Code:

Private Sub UserForm_Initialize()
    ListBox1.AddItem "MBA"
    ListBox1.AddItem "MCA"
    ListBox1.AddItem "MSC"
    ListBox1.AddItem "MECS"
    ListBox1.AddItem "CA"
End Sub
    1. Now, Press ‘F5’ to run the code. You can see the following Output. It is shown in the following Screen Shot.

Excel VBA ListBox ActiveX Control1

Add dynamic ListBox_Control on the UserForm using VBA

Please find the following steps and example code, it will show you how to add dynamic list box control on the userform.

    1. Add command button on the userform from the toolbox.
    2. Right click on the command button, click properties
    3. Change the command button caption to ‘Create_Listbox’
    4. Double click on the command button
    5. Now, it shows following code.
Private Sub CommandButton1_Click()
 
End Sub
    1. Call the below procedure named ‘Add_Dynamic_Listbox’ and find the below procedure to run.
Private Sub CommandButton1_Click()
    Call Add_Dynamic_Listbox
End Sub

Procedure to call in the Command Button:

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
    1. Now, click F5 to run the macro, click ‘Create_Listbox’ button to see the result.
    2. You can see the created dynamic checkbox in the following screen shot.

output:

Excel VBA ListBox ActiveX Control2

Add 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 Sub

In 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 Sub

Check 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 Sub

VBA 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 Sub

Code 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 Sub

Code 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 Sub

Code 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 Sub

Get 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 Sub

Output:

Excel VBA ListBox ActiveX Control3

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 Sub

Get 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 Sub

Make 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.

Sub Multiple_ListBox_Selection()
    ListBox1.MultiSelect = fmMultiSelectMulti
End Sub

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 Sub

More 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 …

Effortlessly Manage Your Projects and Resources
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.

Save Up to 85% LIMITED TIME OFFER
Project Management Templates

All-in-One Pack
120+ Project Management Templates

Essential Pack
50+ PM Templates

Excel Pack
50+ Excel PM Templates

PowerPoint Pack
50+ Excel PM Templates

MS Word Pack
25+ Word PM Templates

Ultimate Project
Management Template
Ultimate Resource
Management Template
Project Portfolio
Management Templates
Last Updated: March 2, 2023

Comments are closed.