Class module to handle checkbox event

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
The following code works for ActiveX checkboxes:

ThisWorkbook:

Code:
Option Explicit

    Dim CheckBoxesColl As Collection
    
Private Sub Workbook_Open()

    Dim CheckBoxHandler As ClsCheckBoxEvent

    Set CheckBoxesColl = New Collection

    Dim MyShp As Shape

    For Each MyShp In Sheet1.Shapes
    
        With MyShp

            If .Type = msoOLEControlObject Then

                With .OLEFormat.Object

                    If TypeOf .Object Is MSForms.CheckBox Then

                        Set CheckBoxHandler = New ClsCheckBoxEvent

                        Set CheckBoxHandler.CheckGroup = .Object

                        CheckBoxesColl.Add Item:=CheckBoxHandler

                    End If

                End With

            End If

        End With
            
    Next MyShp
    
End Sub

ClsCheckBoxEvent:

Code:
Option Explicit

    Public WithEvents CheckGroup As MSForms.CheckBox

Private Sub CheckGroup_Click()

    Select Case CheckGroup.Caption
    
        Case "CheckBox1"
            
            Select Case CheckGroup.Value
            
                Case True
                
                    MsgBox "CheckBox 1 checked"
            
                Case False
            
                    MsgBox "CheckBox 1 unchecked"
                    
            End Select
            
        Case "CheckBox2"
        
            Select Case CheckGroup.Value
            
                Case True
                
                    MsgBox "CheckBox 2 checked"
            
                Case False
            
                    MsgBox "CheckBox 2 unchecked"
                    
            End Select
            
    End Select

End Sub

How can I adapt it so it works for Forms checkboxes? (I don't mean a checkbox on a userform but a checkbox under Developer -> Insert - Forms Controls).

Thanks
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Not sure if that can be done but if your aim is to have one procedure that handles multiple checkboxes then maybe you can use Application.Caller to determine which checkbox was clicked. Assign each checkbox to the FormsCheckBoxHandler macro.

VBA Code:
Sub FormsCheckboxHandler()

Dim checkBoxName As String

checkBoxName = Application.Caller

MsgBox checkBoxName & " was clicked."

End Sub

Does that help?
 
Upvote 0
Solution
Not sure if that can be done but if your aim is to have one procedure that handles multiple checkboxes then maybe you can use Application.Caller to determine which checkbox was clicked. Assign each checkbox to the FormsCheckBoxHandler macro.

VBA Code:
Sub FormsCheckboxHandler()

Dim checkBoxName As String

checkBoxName = Application.Caller

MsgBox checkBoxName & " was clicked."

End Sub

Does that help?
Thanks, that'll do nicely.
 
  • Like
Reactions: dk
Upvote 0

Forum statistics

Threads
1,214,947
Messages
6,122,413
Members
449,082
Latest member
tish101

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