onfexcel-how-to-add-item-to-a-combobox

How to Add Item to ComboBox (populate ComboBox with items)

Users can select from predefined values assigned to the ComboBox. The UserForm creator has to be define this Values.

For example: gender (male/female), age (1, 2, 3, 4,…), etc.

Populate ComboBox when the value of an OptionButton/CheckBox Changes

To populate a ComboBox in accordance with the True/False value of an OptionButton or CheckBox you need to use the CHANGE event.

As in the previous example, it is possible to use both SELECT CASE as IF function but we advise in using the SELECT CASE.

Select Case

Private Sub OptionButton1_Change()

Select Case OptionButton1.Value
    Case Is = True' an option button can have 2 values true (selected) and false(not selected)
        With ComboBox3
            .Clear
            .AddItem ("OB 1 to 3")
            .AddItem ("OB 4 to 5")
            .AddItem ("OB 6 to 9")
        End With
    Case Is = False
        With ComboBox3
            .Clear
            .AddItem ("OB 11 to 13")
            .AddItem ("OB 14 to 15")
            .AddItem ("OB 16 to 19")
        End With
    End Select

End Sub

If

Private Sub OptionButton1_Change()
ComboBox2.Clear
     If OptionButton1.Value = True Then  ' an option button can have 2 values true (selected) and false(not selected)
         With ComboBox2
            .Clear
            .AddItem ("1 to 3")
            .AddItem ("4 to 6")
            .AddItem ("7 to 9")
            'add as many items you need
        End With
        Else
        If OptionButton1.Value = False Then
        With ComboBox2
            .Clear
            .AddItem ("10 to 13")
            .AddItem ("14 to 16")
            .AddItem ("17 to 19")
            'add as many items you need
        End With
    End If
    End If
End Sub
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *