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

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

问题描述

您好,我有这个ComboBox,我想做一些命令,如果combox值说,例如巴黎

  Workbook_open()

With Sheet1.ComboBox1
.AddItemParis
.AddItemNew York
.AddItemLondon
End With

如果Me.ComboBox1.Value =Paristhen
Range(A1)。Value = 5
End If

End Sub

有任何帮助吗?
谢谢

解决方案

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



此代码:

 如果Me.ComboBox1.Value =Paristhen 
Range(A1)。Value = 5
End If



应该在其他程序中。



:如果您希望在选择项目时更改 A1

  Private Sub Workbook_open()

使用Sheet1.ComboBox1
.AddItemParis
.AddItemNew York
.AddItem伦敦
结束于

结束子

私人子ComboBox1_Change()
如果Me.ComboBox1.Value =Paris然后
范围(A1)。值= 5
结束如果
结束子

实际上 ComboBox1_Change 每次都会被调用 ComboBox1 值变化很明显



注意:此代码已经测试并适用于我,但还有其他方法,例如添加 commandButton 条件只有当此按钮被点击。


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

解决方案

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.

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

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