Loop through each Checked box selected and run Macro one after the other

hugocarvalho

New Member
Joined
Oct 6, 2015
Messages
2
Hello!

I have a database for years from 2005-2020 with value.
I created an Userform with check boxes. 15 check boxes each one is a year date. (This is my starting point)

What I need if possible

If I tick 2007,2008,2015,2019 for example
I would like that when I press a "Ok" button in the Userform, the macro runs for 2007 and then after for 2008,2015 and 2019.
For all box checked previously.

"
When user press OK, IF 2007, 2008, 2010, etc are selected then,

If Checkbox1.Value = True Then
YearWanted = 2007
Range("G1").Value = YearWanted
Run ("Macro109")

If Checkbox4.Value = True Then
YearWanted = 2010
Range("G1").Value = YearWanted
Run ("Macro109")

etc

"

and then after continue with all previous box checked by the user.


I cant figured it out the fact to store all checked box and run each one after when I click the button OK in the same userform

Thank you very much! :)
 
Assumptions:
1) You have not changed the (Name) property of any of the CheckBox's or Button
2) Each CheckBox sequentally corresponds to a year
3) the Macro109 is contained within the same project
4) and possible other...

Here is some code should work (assuming my assumptions are correct):

Code:
Private Sub CommandButton1_Click()    Dim n As Long
    For n = 1 To 15 Step 1
        If Me.Controls("CheckBox" & n).Value Then
            Range("G1").Value = 2004 + n
            Call Macro109
        End If
    Next n
End Sub

Always remember to backup your work before running new code

Hope this helps!
 
Upvote 0

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