Excel VBA: IF ComboBox.Value statement

让人想犯罪 __ 提交于 2019-12-19 11:55:12

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/25440637/excel-vba-if-combobox-value-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!