Making All Checkboxes Run the Same Code
Unfortunately, Excel VBA does not support control arrays. It's a huge bummer, especially since Excel is such a natural array medium
But there are some goofy, yet slightly more elegant ways to handle your situation. You can't create custom properties for a checkbox control, but you can embed the category type in the name of the control and use that to group them programmatically. Of course it's best if you use your category designation as a prefix or suffix, so you don't have to search the whole name for it
In iterating over such a group of controls you can either just check the name, like
Dim ctrl as Objec
Dim b as Boolea
'It's easier to assume they're all checke
b = Tru
For each ctrl in ActiveSheet.OLEObject
'If this is a category one checkbox and it's value is False, we're outa her
If Left$(ctrl.Name, 3) = PREFIX_CATEGORY_ONE and Not ctrl.Value The
b = Fals
Exit Fo
End I
Next ctr
If b The
cmdButton.Caption = "Clear Category One
Els
cmdButton.Caption = "Check Category One
End I
The main advantage of this code over yours is that you can add controls to the category without changing the code. Other than that it's still a little clunky
Using the OLEObjects collection can be a great way to get around controls in a more logical way. Be careful if you're using Excel '97 (and maybe 2000?) - if you refer to Worksheet.OLEObjects("controlname"), you must also remember to set the version of the control name that resides in the Define Name box in the Excel toolbar. Excel XP sets this automatically when you set the Name property of the control, but '97 doesn't. Yes, there are two versions of the control name
Good Luck
James
|