excel - 如何根据多个条件显示值?
问题描述
在我的表单中,我希望用户能够根据选项框并根据他们的输入看到颜色或动物。到目前为止我尝试过的代码对我不起作用,我认为这是因为我不明白如何正确格式化它。
我已经尝试为每种颜色的每个数字做它,我尝试将数字添加为可能的值,我尝试创建一个数组。然而没有成功。
例如。
如果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
解决方案
这使用选择案例而不是 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
推荐阅读
- adobe - 数组的javascript多少和分钟
- python - 具有 NOBLOCK 标志行为的 pyzmq socket.recv()
- javascript - 如何循环并将键值添加到值
- zebra-printers - ZPL:同一请求中的多个 SGD(获取)
- azure-active-directory - 如何仅从所有 azure AD 用户列表中过滤掉人类用户?(没有服务帐户或房间)
- debugging - 我如何在 x32dbg/x64dbg 中自动跟随“转储窗口”中的堆栈(ESP)?
- python - Scipy 树状图中不同的链接颜色意味着什么?
- ruby-on-rails - 发出 API 请求时在 Rails 中插值
- ruby - Capybara 'visit' 没有考虑 URL 中的 '#' 字符?
- postman - 邮递员“无法得到任何回应,连接到时出错。”