如何显示基于多个条件的值?

问题描述 投票:0回答:1

在我的表单中,我希望用户能够基于选项框以及基于他们的输入来看到颜色或动物。到目前为止,我尝试过的代码对我没有用,我认为这是因为我不了解如何正确设置其格式。

我尝试针对每种颜色按数字进行操作,尝试将数字作为可能的值相加,并尝试创建数组。但是没有成功。

例如。

如果选择Optionbutton A,并且textbox A值为1、4或10,则textbox B将显示蓝色。但是,如果textbox A值为2、3、5或9,则textbox B将显示绿色。

示例2。

Optionbutton B,并且textbox A的值为1、3、4、10或12,则textbox B将显示蝴蝶。但是,如果textbox A的值为2、5、7,则textbox B将显示Caterpie。

[添加注释,我不知道表单是否可行,但是如果用户要在选项框之间切换,则文本框B会相应更改。

Private Sub txtboxA_Change()

Dim txtboxA As String
Dim Colourarray As Variant

Blue = Array("1", "4", "10")

Me.txtboxA.MaxLength = 2

'If OptA is selected then pick colour based on textbox value

If Me.OptA.Value = True And Me.txtboxA.Value = "1" Then 'This bit seemed to work, but once I copied this for every number which needs to show green, vba got unhappy at me. 
'If Me.OptA.Value = True And Me.txtboxA.Value = "1", "4", "10" Then
'If Me.optA.Value = True And (Poort = "1", "4", "10") Then
'If Me.txtboxA.Value = Colourarray And Me.optA.Value = True Then
Me.txtboxB.Value = "Blue"
End If
End Sub
excel vba multiple-conditions
1个回答
0
投票

这使用选择大小写而不是if。

Private Sub txtboxA_Change()

    Dim txtboxA As String
    Dim optionchoice As Long

    Me.txtboxA.MaxLength = 2

    'option A = 1; option B = 2
    If Me.optA.Value = True Then 'Easier to do a select case on optionchoice over option button
        optionchoice = 1
    ElseIf Me.optB.Value = True Then
        optionchoice = 2
    End If


    Select Case optionchoice 'Based on Option Button selection
        Case 1 'Option A
            Select Case Me.txtboxA.Value 'Based on Text Box value
                Case 1, 4, 10
                    Me.txtBoxB.Value = "Blue"
                Case 2, 3, 5, 9
                    Me.txtBoxB.Value = "Green"
            End Select
        Case 2 'Option B
            Select Case Me.txtboxA.Value
                Case 1, 3, 4, 10, 12
                    Me.txtBoxB.Value = "Butterfly"
                Case 2, 5, 7
                    Me.txtBoxB.Value = "Caterpie"
            End Select
        End Select

End Sub
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.