仅当Excel UserForm中的CheckBox1和CheckBox2 = True时激活按钮

时间:2014-03-25 13:48:51

标签: excel vba excel-vba userform

所以我想创建一个UserForm,如果两个复选框都被标记,那么将允许单击该按钮。我尝试了一堆不同的代码是我的第一次尝试。

Private Sub CommandButton1_Click()

If CheckBox1.Value = True & CheckBox2.Value = True Then
        CommandButton1.Enabled = True
    Call GOGOGO
    ElseIf CheckBox1.Value = True & CheckBox2.Value = False Then
        MsgBox ("Run Characteristics Version 1.2.xlsm")
    ElseIf CheckBox1.Value = False & CheckBox2.Value = True Then
        MsgBox ("Log in")
    Else
        MsgBox ("Log in and Run Characteristics Version 1.2.xlsm")
End If

End Sub

还试过

Private Sub CheckBox1_Change()

    'Evaluate the value of the CheckBox
    Select Case CheckBox1.Value
        Case True, False
    End Select

End Sub
Private Sub CheckBox2_Change()

    'Evaluate the value of the CheckBox
    Select Case CheckBox2.Value
        Case True, False
    End Select

End Sub
Private Sub CommandButton1_Click()
    Select Case CheckBox1.Value
    Select Case CheckBox2.Value
    If CheckBox1.Value = True & CheckBox2.Value = True Then
        CommandButton1.Enabled = True
        Call GOGOGO
        ElseIf CheckBox1.Value = True & CheckBox2.Value = False Then
            MsgBox ("Run Characteristics Version 1.2.xlsm")
        ElseIf CheckBox1.Value = False & CheckBox2.Value = True Then
            MsgBox ("Log in")
        Else
            MsgBox ("Log in and Run Characteristics Version 1.2.xlsm")
    End If
    End Select
    End Select
    End Sub

两者都只给我第一个ElseIf。因此,当我单击命令按钮时,复选框的所有不同组合我总是得到msgBox“运行特征....”

问题:如何创建按钮仅在Excel用户窗体中标记2个复选框时运行命令?

2 个答案:

答案 0 :(得分:1)

非常关闭,您的挑战只是您使用&而不是VBA And运营商。

例如,您的第一个子视图应如下所示:

Private Sub CommandButton1_Click()

    If CheckBox1.Value = True And CheckBox2.Value = True Then
        CommandButton1.Enabled = True
        Call GoGoGo
    ElseIf CheckBox1.Value = True And CheckBox2.Value = False Then
        MsgBox ("Run Characteristics Version 1.2.xlsm")
    ElseIf CheckBox1.Value = False And CheckBox2.Value = True Then
        MsgBox ("Log in")
    Else
        MsgBox ("Log in and Run Characteristics Version 1.2.xlsm")
    End If

End Sub

请注意,我所做的只是将&替换为And

希望能满足您的需求!!

答案 1 :(得分:0)

不要使用'&'在if语句中。它用于字符串连接。这应该使你的代码工作。另请注意,启用注释的位置不合理,因为按钮已被按下。

Private Sub CommandButton1_Click()

If (CheckBox1.Value And CheckBox2.Value) Then
    Call GOGOGO
else
    If not CheckBox1.Value  Then     
      If not CheckBox2.Value then
        MsgBox ("Log in and Run Characteristics Version 1.2.xlsm")
      else
        MsgBox ("Run Characteristics Version 1.2.xlsm")
      end if     
    else
        MsgBox ("Log in")
    end if  
end if

End Sub

对于第二种方法:

Private Sub CheckBox1_Change()

if CheckBox1.Value and CheckBox2.Value
   CommandButton1.Enabled = True
else
  CommandButton1.Enabled = False
end if

End Sub
Private Sub CheckBox2_Change()

if CheckBox1.Value and CheckBox2.Value
    CommandButton1.Enabled = True
else
    CommandButton1.Enabled = False
end if

End Sub

但是你还需要初始化commandbutton1