VBA Code to Create a Combo Box in Excel

VBA Code:

' VBA Code to Create a Combo Box in Excel

Sub CreateComboBox()
    Dim ws As Worksheet
    Dim comboBox As OLEObject
    Dim comboBoxTop As Double
    Dim comboBoxLeft As Double
    Dim comboBoxWidth As Double
    Dim comboBoxHeight As Double
    ' Set the worksheet to work with
    Set ws = ThisWorkbook.Worksheets("Sheet1") ' Replace with the name of your worksheet
    ' Set the dimensions and position of the combo box
    comboBoxTop = 100 ' Replace with the desired top position
    comboBoxLeft = 100 ' Replace with the desired left position
    comboBoxWidth = 120 ' Replace with the desired width
    comboBoxHeight = 20 ' Replace with the desired height
    ' Create the combo box
    Set comboBox = ws.OLEObjects.Add(ClassType:="Forms.ComboBox.1", Link:=False, _
        DisplayAsIcon:=False, Left:=comboBoxLeft, Top:=comboBoxTop, _
        Width:=comboBoxWidth, Height:=comboBoxHeight)
    ' Customize the combo box properties
    With comboBox
        .Name = "ComboBox1" ' Replace with the desired name for the combo box
        .Object.AddItem "Option 1" ' Add items to the combo box
        .Object.AddItem "Option 2"
        ' Add more items as needed
        ' Customize other combo box properties as needed
        ' Add event handler code for combo box events, if necessary
        ' For example, to execute a specific procedure when the selection changes:
        '.Object.OnChange = "ComboBox1_Change"
    End With
    ' Display a message
    MsgBox "Combo box has been created."
End Sub

Check All VBA Codes

Join Our Telegram Group techguruplus telegram group Join Our WhatsApp Group techguruplus whatsapp group
Nazim Khan - Author Image

Nazim Khan (Author) 📞 +91 9536250020
[MBA in Finance]

Nazim Khan is an expert in Microsoft Excel. He teaches people how to use it better. He has been doing this for more than ten years. He is running this website (TechGuruPlus.com) and a YouTube channel called "Business Excel" since 2016. He shares useful tips from his own experiences to help others improve their Excel skills and careers.

Leave a Comment