ComboBox

ComboBox is one of the UserForm control. You can select and drag drop control on the UserForm. This control is used to store and display list of items to a list. This can be used on the UserForm. Please find more details about ComboBox Control in the following chapter. You can see how to load items to a Combo Box, how to get the value of combo box items, 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 ComboBox_Control on the UserForm

Please find more details about VBA ActiveX Combo Box 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 a ComboBox on the Userform from the Toolbox. Please find the screenshot 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()
    ComboBox1.AddItem "MBA"
    ComboBox1.AddItem "MCA"
    ComboBox1.AddItem "MSC"
    ComboBox1.AddItem "MECS"
    ComboBox1.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 ComboBox_Control on the UserForm using VBA

Please find the following steps and example code, it will show you how to add dynamic Combo 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_ComboBox ’
  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_ComboBox ’ and find the below procedure to run.
  8. Private Sub CommandButton1_Click()
    Call Add_Dynamic_ComboBox 
    End Sub
    

    Procedure to call in the Command Button :

    Sub Add_Dynamic_ComboBox()
    'Add Dynamic Combo Box and assign it to object 'CmbBx'
    Set CmbBx = UserForm3.Controls.Add("Forms.comboBox.1")
    'Combo Box Position
    CmbBx.Left = 20
    CmbBx.Top = 10
    End Sub
    
  9. Now, click F5 to run the macro, click ‘Create_ComboBox ’ button to see the result.
  10. You can see the created dynamic combo box in the following screen shot.
  11. output:

    Add Items to ComboBox_Control using VBA

    Please find the following code, it will show you how to add list items to Combo Box.

    Private Sub Insert _Items _To_ComboBox ()
    ComboBox1.AddItem "Item 1"
    ComboBox1.AddItem "Item 2"
    ComboBox1.AddItem "Item 3"
    ComboBox1.AddItem "Item 4"
    ComboBox1.AddItem "Item 5"
    End Sub
    

    In the above code ComboBox1 is the name of the Combo Box. Where ‘additem’ is the property of Combo Box.

    Clear Items from the ComboBox_Control using VBA

    Please find the following code, it will show you how to clear the Combo Box items. The below code clears the ComboBox1 items on the UserForm1.

    Sub Clr_CmbBx()
    UserForm3.ComboBox1.Clear
    End Sub
    

    Get the selected value of the ComboBox using VBA

    Please find the below code to know how to get the selected value of the Combo Box using VBA. In the below example value is the property of Combo box.

    Sub Chk_Item_SelectOrNot()
    MsgBox ComboBox1.Value
    End Sub
    

    VBA ComboBox Default Values in Excel

    Here is the VBA Combo Box default values in Excel. After adding items to Combo 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 Combo Box . Where ‘-1’ is the index number.

    Sub LstBx_Dflt_Val_Ex1()
    UserForm3.ComboBox1.ListIndex = -1
    End Sub
    

    Code 2:

    The below code is useful to select first item in the Combo Box from the available list. . Where ‘0’ is the index number.

    Sub LstBx_Dflt_Val_Ex2()
    UserForm3.ComboBox1.ListIndex = 0
    End Sub
    

    Code 3:

    The below code is useful to select second item in the Combo Box from the available list. Where ‘1’ is the index number.

    Sub LstBx_Dflt_Val_Ex3()
    UserForm3.ComboBox1.ListIndex = 1
    End Sub
    

    Code 4:

    The below code is useful to select the last item in the Combo Box from the available list. Where ‘1’ is the index number.

    Sub LstBx_Dflt_Val_Ex4()
    UserForm3.ComboBox1.ListIndex = UserForm3.ComboBox 1.Count - 1
    End Sub
    

    Get the total count of ComboBox Items

    Here is the following example, it will show you how to get the total count of items in a Combo Box. In the below example ComboBox1 is the Combo Box name and ListCount is the property of Combo Box .

    Sub Get_Ttl_Cnt()
    MsgBox "Total Items in a ComboBox is " & UserForm3.ComboBox1.ListCount
    End Sub
    

    More Details About the ComboBox_Control

    VBA ComboBox Excel Macros Examples Codes Adding Clearing Items

    Please find more details about Remove Duplicates in Combo Box in Excel VBA.

    Read More …

    VBA Remove Duplicates in ComboBox

    Please find the following link for more details about VBA Combo Box Excel Macros Examples and Codes Adding and Clearing Items.

    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

Related pages


excel formula to delete duplicatesvba code for accessexcel append data from multiple sheetscontoh minutes of meetinghow to adjust width of column in excelexcel last cell in columnvba rangestricky interview questions and answers for freshersgenerating xml from excelhow to learn macroshow to add chart title in excelcheck duplicates in excelinsert row excel 2007excel vba reference worksheetexcel charts examplessql ddl commandsvba array stringmacros in excel 2007 for beginnersiferror in vlookupexcel unprotectend loop vbahow do i unhide a row in excelindex function in excel 2007not equal to vbaxls macro tutorialsql coding basicshow to remove macros in excelusing vlookup in excel 2007run excel macro on openappend string in exceldelete blank cells in excelworkbooks closetick mark in excel 2007concatenate using vbahow to check duplicates in excel 2007project checklist template excelmacro buttonsddl commands in sql with syntax and examplesexcel logical functions with examplesruntime 1004 vbainstr in vbasave a workbook vbalistbox in userform excel vbaqc check listvba to remove duplicatesnaming a range in excelcountif formula in excelexcel vba buttonsbpo formatexcel combine multiple worksheetsms excel 2007 formulas with examples pdf in hindiwhat is a worksheet in excel definitionmsgbox yes no cancelsas programming for dummieshow to unhide multiple columns in excelvba subsql in vbaconstruction project management templates exceloutlook vba tutorialhighlight a column in excelproject tracking with excelloop in excel formulaexcel vba display userformmacro excel programmingsql groupingvba pop up windowworkbook save vbadatabase interview questions for freshersword template agendavba refresh all pivot tablesexport excel to txtcheckbox syntaxexcel macro matchchanging rows to columns in excelexcel formulas and functions for dummiesenable developer tab excel 2010excel print macroexcel hyperlink button