PDA

View Full Version : Solved: graying out check boxes



alex108
06-06-2007, 11:57 AM
Is it possible to gray out check boxes based on the state of another check box?
Here's what I am trying to do:
I have a stand alone check box chkboxA and a grouping of two other check boxes chkboxB and chkboxC.
If chkboxA is not checked I would like the two check boxes in the grouping (B and C) to gray out so they can't be picked.
If A is picked then I would like B and C to "ungray" (not the best terminology) so they can be checked.

Gert Jan
06-06-2007, 12:36 PM
Would this do it for you?.
Private Sub ChkA_Click()
If ChkA = True Then
ChkB.Enabled = True
ChkC.Enabled = True
ElseIf ChkA = False Then
ChkB.Enabled = False
ChkC.Enabled = False
End If
End Sub

lucas
06-06-2007, 05:53 PM
Might want to add this to the userform code:
Private Sub UserForm_Initialize()
ChkB.Enabled = False
ChkC.Enabled = False
End Sub

alex108
06-07-2007, 04:29 AM
I've tried to run the code, but I end up getting a runtime error- 424 Object required.
I don't know if it is pertinent but the check boxes are on "Sheet1" of the workbook.

lucas
06-07-2007, 05:11 AM
You have to put the code in the module for the sheet that the buttons are on and they have to be created from the control toolbox toolbar not from the forms toolbar:

alex108
06-07-2007, 05:28 AM
Thanks to you both that did the trick. One more question- if I check either check box B or C and then uncheck check box A, B and C gray out like I wanted them to but remain checked. Can they revert to unchecked and gray out when A is unchecked?

lucas
06-07-2007, 05:37 AM
Private Sub ChkA_Click()
If ChkA = True Then
ChkB.Enabled = True
ChkC.Enabled = True
ElseIf ChkA = False Then
ChkB.Value = False
ChkB.Enabled = False
ChkC.Value = False
ChkC.Enabled = False
End If
End Sub

alex108
06-07-2007, 05:44 AM
Great! Works like a champ. Thanks again. On an informational note I have quite a few check boxes on Sheet1 that I used the forms toolbar to create, should I be using the control toolbar to create on a spreadsheet and only use the forms toolbar on forms. (This may be basic but this is my first major use of VBA)
Thanks again for your time.

lucas
06-07-2007, 05:50 AM
Hi Alex,
It mostly depends on the use. Checkboxes from the forms toolbar allow you to assign a macro to it....the ones from the control toolbox are just like the ones on a userform...with all the properties and you need to use code for the most part.

Be sure to mark your thread solved after you get all of your questions answered. Use thread tools at the top of the page.

alex108
06-07-2007, 06:17 AM
Thanks again.

cgannm
05-29-2008, 09:32 AM
HI, Gert. I recently applied this code to a simple worksheet and it works. In my worksheet I have over 20+ buttons and would like to apply a loop but do not know how to code. Can you help?


Would this do it for you?.
Private Sub ChkA_Click()
If ChkA = True Then
ChkB.Enabled = True
ChkC.Enabled = True
ElseIf ChkA = False Then
ChkB.Enabled = False
ChkC.Enabled = False
End If
End Sub