Class to control multiple checkboxes

confusion123

Active Member
Joined
Jul 27, 2014
Messages
400
My worksheet has many checkboxes and instead of writing the following:

Code:
Private Sub chk1_Click()

End Sub

Private Sub chk2_Click()

End Sub

etc.

I want to put it in a class.

So far, I have this in a standard module:

Code:
Dim MyCheck() As Class1
    
Dim ChkCount As Integer

Dim Ctl As Shape ' NOT SURE IF I SHOULD DECLARE AS SHAPE OR SOMETHING ELSE

ChkCount = 0

For Each Ctl In Sheet4.Shapes
    
    If Left(Ctl.Name, 3) = "chk" Then ChkCount = ChkCount + 1
    
Next Ctl

ReDim MyCheck(1 To ChkCount) As Class1

ChkCount = 0

For Each Ctl In Sheet4.Shapes

    If Left(Ctl.Name, 3) = "chk" Then

        ChkCount = ChkCount + 1
        Set MyCheck(ChkCount) = New Class1
        Set MyCheck(ChkCount).abc = Ctl

    End If

Next Ctl

and this in Class1:

Code:
Option Explicit

Public WithEvents CheckGroup As ?????

Private Sub CheckGroup_Click()

End Sub

My question is, what should replace the ?????

I've tried MS.Forms.Checkbox or simply checkbox but neither worked.

Thanks
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Typo, should have read:

Code:
ChkCount = ChkCount + 1
Set MyCheck(ChkCount) = New Class1
Set MyCheck(ChkCount).CheckGroup= Ctl
 
Upvote 0
My Mac doesn't support ActiveX controls, and my messing around at work (don't tell the boss) hasn't revealed how to automate Checkbox objects.

An Alternative would be to use Forms checkboxes. This can be done quickly by

1) Create one Forms checkbox
2) Write the generic macro that you want all checkboxes to run
3) Assign that macro to the single checkbox
4) Copy that checkbox and paste new ones, the new checkboxes will also be assigned to that macro.
 
Upvote 0
Thanks but the whole point was to avoid having to write lots of the same code (which I have already done, using my ActiveX Control checkboxes).

It's not that I am against writing loads of code, just thought there would be a shorter and more easier way to maintain the code.
 
Upvote 0
If you follow Mike's suggestion, all the checkboxes run the same macro, so there isn't lots of code. If you want to use a class, the variable should be declared as MSForms.Checkbox
 
Upvote 0
Thanks, got it in the end.

Code:
Dim CheckBoxesColl As Collection

Private Sub Worksheet_Activate()

Dim CheckBoxHandler As ClsCheckBoxEvent
Dim MyShp As Shape

Set CheckBoxesColl = New Collection

For Each MyShp In Me.Shapes
    If MyShp.Type = msoOLEControlObject Then
        If TypeOf MyShp.OLEFormat.Object.Object Is MSForms.CheckBox Then
            Set CheckBoxHandler = New ClsCheckBoxEvent
            Set CheckBoxHandler.CheckGroup = MyShp.OLEFormat.Object.Object
            CheckBoxesColl.Add CheckBoxHandler
        End If
    End If
Next MyShp
End Sub

This is in a class module:

Code:
Option Explicit

Public WithEvents CheckGroup As MSForms.CheckBox

Private Sub CheckGroup_Click()

End Sub
 
Upvote 0
If you follow Mike's suggestion, all the checkboxes run the same macro, so there isn't lots of code. If you want to use a class, the variable should be declared as MSForms.Checkbox

Does that work for an ActiveX check box on a sheet. My (minimal) testing gave me type mismatch errors when I tried to assign that kind of check box to an event bearing Class property
 
Upvote 0
^^ just got to work (windows machine), did some testing and answered my question.
Good to know that an ActiveX checkbox is an msForms.Checkbox. I thought the msForms was userform only.
 
Upvote 0

Forum statistics

Threads
1,215,025
Messages
6,122,732
Members
449,093
Latest member
Mnur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top