Gregor
Gregor

Reputation: 21

Add item to combobox if condition is met

I have been trying to create a dropdown menu with the options depending on cell values, but have been getting error messages instead.

I have: A table with "0"s and different text items, i.e. "ABC" or "DEF" I want: A dropdown list only with the text items, without the zeros

What I have tried so far:

Private Sub Submit_Click() 'CommandButton to reset and fill Combobox
    Me.Combobox1.Clear
For Each cell In ["range name"]
    If Range(cell).Value <> 0 THen
    Me.Combobox1.AddItem cell
    End If
Next Cell

End Sub

I got the normal "for each"..."additem" command to work, but then I have all the "0"s as items in the dropdown menu.

Upvotes: 2

Views: 2071

Answers (1)

Siddharth Rout
Siddharth Rout

Reputation: 149335

Is this what you are trying?

Private Sub Submit_Click()
    Dim aCell As Range

    Me.Combobox1.Clear

    For Each aCell In ["range name"]
        If aCell.Value <> 0 Then Me.Combobox1.AddItem aCell.Value
    Next aCell
End Sub

Upvotes: 2

Related Questions