ListBox

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

PREMIUM TEMPLATES LIMITED TIME OFFER

ON SALE80% OFF

50+ Project Management Templates Pack
Excel PowerPoint Word

Advanced Project Plan & Portfolio Template
Excel Template

Business Presentations Templates Pack
PowerPoint Slides

20+ Excel Project Management Pack
Excel Templates

20+ PowerPoint Project Management Pack
PowerPoint Templates

10+ MS Word Project Management Pack
Word Templates


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.
  3. Drag Listbox_Control on the Userform from the Toolbox. Please find the screen shot for the same.
  4. Double Click on the UserForm, and select the Userform event as shown in the below screen shot.
  5. Now can see the following code in the module.
  6. Private Sub UserForm_Initialize()
    End Sub
    
  7. Now, add the following code to the in between procedure.
  8. Code:

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

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.
  6. Private Sub CommandButton1_Click()
    End Sub
    
  7. Call the below procedure named ‘Add_Dynamic_Listbox’ and find the below procedure to run.
  8. 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
    
  9. Now, click F5 to run the macro, click ‘Create_Listbox’ button to see the result.
  10. You can see the created dynamic checkbox in the following screen shot.
  11. output:

    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:

    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 …

LIMITED TIME OFFER