Excel VBA:IF ComboBox.Value 语句 [英] Excel VBA: IF ComboBox.Value statement

查看:36
本文介绍了Excel VBA:IF ComboBox.Value 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个组合框,如果组合框的值是巴黎,我想做一些命令

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

有什么帮助吗?谢谢

推荐答案

其实你的代码是正确的,但是只有在你的工作簿打开时才会调用你的条件 (WorkBook_open()) ...

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

此代码:

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

应该在其他程序中.

例如:如果您希望 A1 在选择项目时更改,您可以执行以下操作:

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

实际上 ComboBox1_Change 每次 ComboBox1 值改变时都会被调用(很明显)

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

注意:这段代码已经过测试并且对我有用,但还有其他方法可以做,比如添加一个 commandButton 并仅在单击此按钮时检查条件.

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.

这篇关于Excel VBA:IF ComboBox.Value 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆