N.Almeida
N.Almeida

Reputation: 21

If statement using multiple checkbox values as condition

I am trying to set a checkbox value as true when three other checkboxes are true.

When I use an IF statement the execution skips the code even though all the conditions are met.

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

Is there any other way I can get the above result?

Upvotes: 0

Views: 2004

Answers (2)

Harun24hr
Harun24hr

Reputation: 37100

You just need AND operator.

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

Upvotes: 0

jamheadart
jamheadart

Reputation: 5343

You're looking to make sure that checkbox 68 is TRUE only if 416,417 and 418 are all definitely TRUE.

You can do this with bitwise comparison and remove the if statement completely

(Please note I've removed the wSA/wSB references because they have no effect in your given code (They're not Set so not sure what to do with them):

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

Upvotes: 2

Related Questions