使用组合框在excel userform

时间:2016-08-23 11:30:19

标签: excel vba excel-vba userform radio-button

我是vba的新手,并且在使用userform方面遇到了一些麻烦。 我试着这样,当组合框中的选择包含某个字母时,会选择一个选项按钮。

我尝试使用的代码是: -

Private Sub ComboBox1_Change()
If ComboBox1.Value = "*C*" Then
    OptionButton3.Value = True
    End If
If ComboBox1.Value = "FR 850 C BLUE" Then
    TextBox2.Value = "BLUE"
    End If
If ComboBox1.Value = "FR 850 C WHITE" Then
    TextBox2.Value = "WHITE"
    End If
End Sub

但是,TextBox2仅可见如下:

Private Sub OptionButton3_Click()
TextBox2.Visible = True
With Me.TextBox2
    .Value = "Colour here"
    .SetFocus
    .SelStart = 0
    .SelLength = Len(.Text)
End With
End Sub

当包含" C"的选项时,请参阅userform的this printscreen(见下文)。已被选中 - OptionButton3仍然未被选中。

printscreen

此外,我还尝试过编码,这样当" FR 850 C BLUE"如果已选中,TextBox2将显示"蓝色"。我怀疑一旦我找到第一部分的修复程序,这将更好地工作,但是,如this printscreen(见下文)所示,如果您在" FR 850 C BLUE"之后选择OptionButton3Combobox1TextBox2并没有像我希望的那样显示"蓝色"

printscreen

有人可以帮忙吗?我怀疑我在某个地方错过了什么......

欢迎任何/所有评论!

1 个答案:

答案 0 :(得分:1)

你在说:

If ComboBox1.Value = "*C*" Then

这意味着,如果ComboBox1.Value等于"*C*"做某事 要检查值是否部分匹配,请使用Like

If ComboBox1.Value Like "*C*" Then