If循环使用多个复选框值作为If条件

时间:2018-12-31 07:53:00

标签: excel vba excel-vba

在下面的代码中,我试图仅在其他三个复选框为true时才将复选框值设置为true。当我使用简单的IF语句检查执行时,即使满足所有条件,也会跳过代码。

Private Sub CommandButton1_Click()
Dim wsA As Worksheet
Dim wbA As Workbook
With wsA
If CheckBoxes("Check Box 416").Value = True & _
CheckBoxes("Check Box 417").Value = True & _
CheckBoxes("Check Box 418").Value = True Then
CheckBoxes("Check Box 68").Value = True
Else
CheckBoxes("Check Box 68").Value = False
End If
End With
End Sub

还有其他方法可以得到上述结果吗?

2 个答案:

答案 0 :(得分:0)

您希望确保仅在416,417和418都为TRUE时,复选框68才为TRUE。

您可以按位比较并完全删除if语句

(请注意,我删除了wSA / wSB引用,因为它们在您的给定代码中无效(它们不是Set,因此不确定如何处理它们):

Private Sub CommandButton1_Click()
CheckBoxes("Check Box 68").Value = _
    CheckBoxes("Check Box 416").Value And _
    CheckBoxes("Check Box 417").Value And _
    CheckBoxes("Check Box 418").Value
End Sub

答案 1 :(得分:0)

您只需要AND运算符。

Private Sub CommandButton1_Click()
Dim wsA As Worksheet
Dim wbA As Workbook
    With wsA
        If (CheckBoxes("Check Box 416").Value = True) And _
            (CheckBoxes("Check Box 417").Value = True) And _
            (CheckBoxes("Check Box 418").Value = True) Then
            CheckBoxes("Check Box 68").Value = True
        Else
            CheckBoxes("Check Box 68").Value = False
        End If
    End With
End Sub