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:
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.
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.