![](/img/trans.png)
[英]Adding OptionButtons to the Userform programatically in VBA Excel
[英]Excel VBA - Optionbuttons, comboboxes and case select not communicating
在表单中,我有两个组合框“ Cb1”,“ Cb2”和三个选项按钮“ Op1”…“ Op3”,在表单中我还有一个按钮“ Btn”,当我单击该按钮时,它应该显示(请参见下面的代码),但实际上没有任何反应。
Private Sub Btn_Click()
Dim Output as string
Select Case Op1.Value
Case Is = 1
If Cb1.ListIndex = -1 Then ‘if nothing is selected
Output = MsgBox “Select one fruit at least”
End If
If (Cb1.ListIndex >= 0) And (Cb2.ListCount = 0) Then ‘ if Cb1 is populated but Cb2 is not
Output = MsgBox “There is no valid basket”
End If
If (Cb1.ListIndex >= 0) And (Cb2.ListCount = 1) And (Cb2.ListIndex = -1) Then ‘if Cb1 is populated and Cb2 has on item on the list but Cb2 is not selected
Output = MsgBox “One basket is available, but not selected”
End If
If (Cb1.ListIndex >= 0) And (Cb2.ListCount > 1) And (Cb2.ListIndex = -1) Then ‘if Cb1 and Cb2 are populated but Cb2 is not selected
Output = MsgBox “More than one basket is available, but none selected”
End If
If (Cb1.ListIndex >= 0) And (Cb2.ListCount > 0) Then ‘if Cb1 and Cb2 are populated and selected
Select Case Cb2.Text
Case "Basket 01"
Call Marry
Case "Basket 02"
Call John
Case Else
End Select
End If
Case Else
End Select
Select Case Op2.Value
Case Is = 1
Output = MsgBox “Today it will rain”
Case Else
End Select
Select Case Op3.Value
Case Is = 1
Output = MsgBox “Today it will NOT rain”
Case Else
End Select
If (Op1.Value=0) And (Op2.Value=0) And (Op3.Value=0) then ‘ if no optionbuttons are selected
Output = MsgBox “Optionbuttons are not selected yet”
End if
End Sub
回答您的帖子(假设您能够在UserForm初始化/激活中正确填充ComboBox和所有UserForm元素)。
OptionButton的值为True / False,因此不需要使用Select Case
,而是使用If / Else
。 参见下面的修改代码:
Private Sub Btn_Click()
Dim Output As String
If Op1.Value Then
If Cb1.ListIndex = -1 Then ' if Nothing Is Selected
Output = "Select one fruit at least"
MsgBox Output
End If
If (Cb1.ListIndex >= 0) And (Cb2.ListCount = 0) Then ' if Cb1 is populated but Cb2 is not
Output = "There is no valid basket"
MsgBox Output
End If
If (Cb1.ListIndex >= 0) And (Cb2.ListCount = 1) And (Cb2.ListIndex = -1) Then ' if Cb1 is populated and Cb2 has on item on the list but Cb2 is not selected
Output = "One basket is available, but not selected"
MsgBox Output
End If
If (Cb1.ListIndex >= 0) And (Cb2.ListCount > 1) And (Cb2.ListIndex = -1) Then ' if Cb1 and Cb2 are populated but Cb2 is not selected
Output = "More than one basket is available, but none selected"
MsgBox Output
End If
If (Cb1.ListIndex >= 0) And (Cb2.ListCount > 0) Then ' if Cb1 and Cb2 are populated and selected
Select Case Cb2.Text
Case "Basket 01"
' Call Marry
Case "Basket 02"
' Call John
Case Else
End Select
End If
Else
' do something here...
End If
If Op2.Value Then
Output = "Today it will rain"
MsgBox Output
Else
' do something here...
End If
If Op3.Value Then
Output = "Today it will NOT rain"
MsgBox Output
Else
' do something here...
End If
If Not Op1.Value And Not Op2.Value And Not Op3.Value Then ' if none of the Option buttons is selected
Output = "Optionbuttons are not selected yet"
MsgBox Output
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.