Userform Optionboxes - use VBA to set which ones are selected when userform opens

AmandaCatRuth

Board Regular
Joined
Apr 20, 2012
Messages
79
I have a worksheet that tracks jobs per month. I have a button used to create a report.

This button opens a userform. There are two frames of optionboxes. One contains the months January-December. The other contains years, currently 2010-2016.

I want the optionboxes for the current month and year to be automatically selected when the userform opens, as those will be the ones most used. The following works perfectly well:

Code:
Private Sub userform_activate()


If Month(Now) = 1 Then MonthButton1.Value = True
If Month(Now) = 2 Then MonthButton2.Value = True
If Month(Now) = 3 Then MonthButton3.Value = True
If Month(Now) = 4 Then MonthButton4.Value = True
If Month(Now) = 5 Then MonthButton5.Value = True
If Month(Now) = 6 Then MonthButton6.Value = True
If Month(Now) = 7 Then MonthButton7.Value = True
If Month(Now) = 8 Then MonthButton8.Value = True
If Month(Now) = 9 Then MonthButton9.Value = True
If Month(Now) = 10 Then MonthButton10.Value = True
If Month(Now) = 11 Then MonthButton11.Value = True
If Month(Now) = 12 Then MonthButton12.Value = True


If Year(Now) = 2010 Then YearButton2010.Value = True
If Year(Now) = 2011 Then YearButton2011.Value = True
If Year(Now) = 2012 Then YearButton2012.Value = True
If Year(Now) = 2013 Then YearButton2013.Value = True
If Year(Now) = 2014 Then YearButton2014.Value = True
If Year(Now) = 2015 Then YearButton2015.Value = True
If Year(Now) = 2016 Then YearButton2016.Value = True


End Sub

I can't help but think I don't need 19 rows of "if" statements to accomplish this - it's sloppy code. Month(Now) and Year(Now) both produce an integer value. I should be able to link that to the button name and have one small slice of code for the month and another for the year. However, repeated slams of my head against my desk have not produced the method by which to do so.

Could someone please advise me on how to tidy this up?

Thank you very much.

PS - Merry Christmas.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

Forum statistics

Threads
1,223,248
Messages
6,171,021
Members
452,374
Latest member
keccles

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