【问题标题】:Activating a button only when CheckBox1 and CheckBox2 = True in Excel UserForm仅当 Excel 用户窗体中的 CheckBox1 和 CheckBox2 = True 时激活按钮
【发布时间】:2015-08-06 20:10:38
【问题描述】:

所以我正在尝试制作一个用户窗体,如果两个复选框都被标记,则将允许单击该按钮。我尝试了一堆不同的代码,这是我的第一次尝试。

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 "Run Characteristics...."

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

【问题讨论】:

    标签: vba excel userform


    【解决方案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

    希望能满足你的需要!!

    【讨论】:

    • B 的儿子哈哈谢谢我上周一直在用 JS 编码,昨天把它放在一起哈哈。非常感谢兄弟。
    • :) - 很高兴它被证明是一个简单的解决方案...祝其余代码好运!
    【解决方案2】:

    不要在 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

    【讨论】:

      猜你喜欢
      • 2017-11-08
      • 1970-01-01
      • 1970-01-01
      • 2010-09-14
      • 1970-01-01
      • 1970-01-01
      • 2019-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多