0

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?

1
  • Replace & with And Commented Dec 31, 2018 at 8:03

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Considering the "with" statement, I suspect the wsA reference was meant to specify the sheet the checkboxes were on. The "CheckBoxes" lines probably should have been dot-referenced in the original question.
I'm sure you're right, but the objects would need set also, and I think I omitted them enitrely because there's no clue in the question as to their name. Leaving them out also helps clarify the simplicity of what's needed (i.e. getting rid of the IF)
0

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

1 Comment

hi thanks for the reply, I have tried replacing but was getting the same result. bitwise comparison works fine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.