Add items to a UserForm ComboBox

When you don’ t want the user to input their own value, set the combobox style to 2. This will force the user to pick from the list of items. The default style is 0, it will behaves as a drop-down combo box or a region to type any value.

ComboBox54NozzleSize.sytle = 2

'0=fmStyleDropDownCombo 
'2=fmStyleDropDownList 

The problem with style set at 2 is you can no longer assign a default value. For example, ComboBox54NozzleSize.value = 15 will result in an error. To fix this issue, change the style back to 0 and place the following code to prevent unwanted values

Private Sub ComboBox54NozzleSize_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    KeyCode = 0
End Sub

There are three ways to add items to a combo box shown below. Any of the three methods can be used and there is no distict advantage or disadvantage to any

1.) use a named range

 ComboBox54NozzleSize.RowSource = "NozzleList"
Named region called NozzleList

2) add as an array

ComboBox54NozzleSize.List = Array(1.5, 2, 3, 4, 6, 8, 10, 12, 14, 16, 18, 20, 24)

3.) use the .additem method

    With ComboBox54NozzleSize
        .AddItem 1.5
        .AddItem 2
        .AddItem 3
        .AddItem 4
        .AddItem 6
        .AddItem 8
        .AddItem 10
        .AddItem 12
        .AddItem 14
        .AddItem 16
        .AddItem 18
        .AddItem 20
        .AddItem 24
    End With

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s