kolis29
kolis29

Reputation: 113

Excel VBA: IF ComboBox.Value statement

Hi I have this ComboBox and I would like to do some command if the combox value says for example Paris

Private Sub Workbook_open()

With Sheet1.ComboBox1
.AddItem "Paris"
.AddItem "New York"
.AddItem "London"
End With

If Me.ComboBox1.Value = "Paris" Then
Range("A1").Value = 5
End If

End Sub

Any help? Thank you

Upvotes: 0

Views: 98786

Answers (1)

smagnan
smagnan

Reputation: 1257

Actually, your code is correct, but your condition will be called only when your workbook will be opened (WorkBook_open()) ...

This code:

If Me.ComboBox1.Value = "Paris" Then
     Range("A1").Value = 5
End If

should be in an other procedure.

Ex: If you want A1 to change when you select an item you can do:

Private Sub Workbook_open()

    With Sheet1.ComboBox1
        .AddItem "Paris"
        .AddItem "New York"
        .AddItem "London"
    End With

End Sub

Private Sub ComboBox1_Change()
    If Me.ComboBox1.Value = "Paris" Then
        Range("A1").Value = 5
    End If
End Sub

Actually ComboBox1_Change is called every time ComboBox1 value changes (pretty obvious)

NOTE: This code is tested and works for me, but there are other ways to do, like adding a commandButton and checking the condition only when this button is clicked.

Upvotes: 4

Related Questions