Title a Sub Routine with a variable.

Engineer123

New Member
Joined
Nov 21, 2019
Messages
8
I have over a dozen checkboxes. They are titled checkbox1, checkbox2, checkbox3... etc. They each have a sub routine that tells them what to do when click. These subroutines are identical except for the number that indexes each checkbox. While this was easy to create using copy/paste. It is messy and very difficult to debug, and may get unreasonable should I have to add more checkboxes. My question is...

…is there anyway to have one subroutine that governs all of them? Something like...


Private Sub Checkbox{n}

msgbox( "You have clicked on the " & n & "th checkbox.")

End Sub

Is it possible to have one subroutine that is triggered by any checkbox, and knows which checkbox triggered it?

Thank you.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Are the checkboxes on a sheet, or userform?
If they are on a sheet are they Form Control, or ActiveX?
 
Upvote 0
In that case you can do something like this.
Create a new Class module & call it ClsChkBx & then put this code in it
VBA Code:
Option Explicit
Public WithEvents ChkBox As MSForms.CheckBox

Private Sub ChkBox_Click()
   MsgBox "you have clicked " & ChkBox.Name
End Sub
Then in the userform module use
Rich (BB code):
Option Explicit
Private ChkBoxClick As Collection

Private Sub UserForm_Initialize()
    Dim Ctrl As MSForms.Control
    Dim Chkbx As ClsChkBx
    
    Set ChkBoxClick = New Collection
    For Each Ctrl In Me.Controls
        If TypeName(Ctrl) = "CheckBox" Then
            Set Chkbx = New ClsChkBx
            Set Chkbx.ChkBox = Ctrl
            ChkBoxClick.Add Chkbx
        End If
    Next
End Sub
The lines in blue must go at the very top of the module, before any code
 
Upvote 0

Forum statistics

Threads
1,223,941
Messages
6,175,537
Members
452,652
Latest member
eduedu

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