Share via

Multiple CheckBoxes condition vba

Anonymous
2012-10-21T12:50:48+00:00

Hello,

I have 3 checkboxes on a userform, I am trying to write a statement like:

if any of the checkboxes = true

do something

else

do something else

I am trying to avoid repeating the following  code for each checkbox (they do exactly the samer thing):

Private Sub ChkClothing_Click()

         If ChkClothing = True Then

            txtItem.Visible = True

            Item.Visible = True

            txtItem.SetFocus

        Else

            txtItem.Text = ""

            txtItem.Visible = False

            Item.Visible = False

        End If

End Sub

Thanks

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2012-10-21T13:24:37+00:00

You can create a procedure (sub) that is called by all three:

Private Sub ChkClothing_Click()

    Call HandleClick

End Sub

Private Sub ChkThis_Click()

    Call HandleClick

End Sub

Private Sub ChkThat_Click()

    Call HandleClick

End Sub

Private Sub HandleClick()

    If ChkClothing Or ChkThis Or ChkThat Then

        txtItem.Visible = True

        Item.Visible = True

        txtItem.SetFocus

    Else

        txtItem.Text = ""

        txtItem.Visible = False

        Item.Visible = False

    End If

End Sub

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2012-10-21T14:03:10+00:00

    Great, thanks. (Got stuck, I was trying to fit it all in one sub)

    Was this answer helpful?

    0 comments No comments