2 questions on checkboxes

MBjorn

New Member
Joined
Jun 1, 2011
Messages
10
Hello,

I am quite new to VBA so excuse me if this is a simple question. My objective is this:

I want a number of checkboxes that:

1) When clicked make all the others go 'false'
2) Updates a lot of data, based on wich checkbox was clicked

Since all the checkboxes do almost the same thing, and I want to be able to make them programatically, Id like to have them calling the same macro, and then in the macro base my actions on which of the checkboxes were clicked.

is that possible?

Alternatively, is their a way to programatically write a new macro, when making a checkbox?
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Where are checkboxes located: sheet or UserForm?
 
Upvote 0
To be able to use one event handler for several checkboxes, use following steps:

1. Create class. Call it, say, EventsTrapper.
2. In this class create public variable, say, Chk.

Class EventsTrapper.
Code:
    Public WithEvents Chk As MSForms.CheckBox

3. Create standard module (name it whateve) and create variable of EventsTrapper and Collection variable:
Code:
    Public col As Collection
    Public EvTrapper As EventsTrapper

4. In Workbook_Open event write down following (assume that checkboxes are on Sheet1):
Code:
Private Sub Workbook_Open()

    Dim ch As MSForms.CheckBox
    Dim ole As OLEObject

    Set col = New Collection
    
    For Each ole In Sheet1.OLEObjects
        If TypeOf ole.Object Is MSForms.CheckBox Then
            Set ch = ole.Object
            Set EvTrapper = New EventsTrapper
            EvTrapper.Chk = ch
            col.Add EvTrapper
            Set EvTrapper = Nothing
        End If
    Next

End Sub

5. In class module choose Chk from top drop-down menu (you'll automatically get Chk_Click event handler).
Code:
Private Sub Chk_Click()

End Sub
Here you write your code. Chk variable is a checkbox which was clicked.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,240
Members
452,898
Latest member
Capolavoro009

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