仅允许检查用户表单

时间:2016-11-25 23:30:49

标签: excel-vba checkbox userform vba excel

所以我正在创建一个userform,我必须使用一组互斥的复选框,或者只允许用户选择一个。 “但只需使用选项按钮!”你哭。两个问题:

  1. 我已经在userform中有一组单独的选项按钮(我相信你可以以某种方式将它们分组以允许多组但我不熟悉如何实际执行此操作)。

  2. 我的教授特别想要复选框

  3. 所以我试图像这样解决这个问题

    If CheckBoxBar.Value = True And CheckBoxatm.Value = True Then
    GoTo Here:
    End If
    
    If CheckBoxatm.Value = True And CheckBoxmmHg.Value = True Then
    GoTo Here:
    End If
    
    If CheckBoxatm.Value = True And CheckBoxpsia.Value = True Then
    GoTo Here:
    End If
    
    If CheckBoxBar.Value = True And CheckBoxmmHg.Value = True Then
    GoTo Here:
    End If
    
    If CheckBoxBar.Value = True And CheckBoxpsia.Value = True Then
    GoTo Here:
    End If
    
    If CheckBoxmmHg.Value = True And CheckBoxpsia.Value = True Then
    GoTo Here:
    End If
    

    这里会显示一个消息框,在msg框中显示“您只能选择一个”,并使用此代码

    后重新初始化用户窗体
    Here: MsgBox "You are only allowed to select on pressure unit."
    

    代码“有效”,但它始终转到Here:语句,尽管只选中了一个复选框。你能发现什么问题吗?

    感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

正如Doug Glancy在评论中所述,您当前的代码可能在标签Vagrant.configure(2) do |config| .... # your other stuff from vagrant here config.vm.provider "virtualbox" do |vb| vb.gui = true end end 之前缺少Exit Sub,因此允许您的代码落入标签之后的部分。

实现目标的另一种方法是只有一个Here:语句检查是否检查了多个If,然后显示MsgBox,如果是,例如如下:

CheckBox

或者你可以依赖If CheckBoxBar.Value + _ CheckBoxatm.Value + _ CheckBoxmmHg.Value + _ CheckBoxpsia.Value < -1 Then MsgBox "You are only allowed to select one pressure unit." Exit Sub End If 作为CheckBox的默认属性,因此&#34; reduce&#34;该代码:

.Value

注意:如果If CheckBoxBar + CheckBoxatm + CheckBoxmmHg + CheckBoxpsia < -1 Then MsgBox "You are only allowed to select one pressure unit." Exit Sub End If 的{​​{1}}属性设置为.TripleState,则此方法无法使用。 (感谢共产国际指出这一点。)

答案 1 :(得分:0)

如果您覆盖单击功能并清除其他框,则可以使复选框像选项按钮一样(尽管您也可以没有选中)。

Private Sub CheckBoxatm_Click()
     If Me.Controls("CheckBoxatm").Value = True Then Call ClearOtherValues("CheckBoxatm")
End Sub

Private Sub CheckBoxBar_Click()
    If Me.Controls("CheckBoxBar").Value = True Then Call ClearOtherValues("CheckBoxBar")
End Sub

Private Sub CheckBoxmmHg_Click()
    If Me.Controls("CheckBoxmmHg").Value = True Then Call ClearOtherValues("CheckBoxmmHg")
End Sub

Private Sub CheckBoxpsia_Click()
    If Me.Controls("CheckBoxpsia").Value = True Then Call ClearOtherValues("CheckBoxpsia")
End Sub

Private Function ClearOtherValues(cb As String)
    Dim cbPressure() As String, i As Long
    cbPressure = Split("CheckBoxBar,CheckBoxatm,CheckBoxmmHg,CheckBoxpsia", ",")
    For i = 0 To UBound(cbPressure)
        If cbPressure(i) <> cb Then Me.Controls(cbPressure(i)).Value = False
    Next i
End Function