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

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Try this:
Code:
Private Sub UserForm_Initialize()
ComboBox1.Value = Format(Now(), "mmm")
End Sub
 
Upvote 0
Hi,

Is something like this what your after?

Code:
MonthButton = "MonthButton" & Month(Now)

Me.Controls(MonthButton) = True

YearButton = "YearButton" & Year(Now)

Me.Controls(YearButton) = True

Hope this helps,
Cheers,
Alan.
 
Upvote 0
or even shorter...

Code:
Me.Controls("MonthButton" & Month(Now)) = True
Me.Controls("YearButton" & Year(Now)) = True
 
Upvote 0
O yes. And this for the combobox for the year:
Code:
Private Sub UserForm_Initialize()
ComboBox1 = Format(Date, "yyyy")
ComboBox2.Value = Format(Now(), "mmm")

End Sub
The first one is for year second one for Month
 
Last edited:
Upvote 0
or even shorter...

Code:
Me.Controls("MonthButton" & Month(Now)) = True
Me.Controls("YearButton" & Year(Now)) = True

This worked perfectly, thank you!

Now how do I shorten this...

Code:
If MonthButton1.Value = True Then CallMonth = 1
If MonthButton2.Value = True Then CallMonth = 2
If MonthButton3.Value = True Then CallMonth = 3
.
.
.
 
Upvote 0
No prob :)

I assume this bit is required after some selection has been made by the user and not on the UserForm Initilize?

try

Code:
For i = 1 To 12
    If Me.Controls("MonthButton" & i) = True Then
        CallMonth = i
    End If
Next
 
Upvote 0
No prob :)

I assume this bit is required after some selection has been made by the user and not on the UserForm Initilize?

try

Code:
For i = 1 To 12
    If Me.Controls("MonthButton" & i) = True Then
        CallMonth = i
    End If
Next

Correct. Again, perfect. Thank you very much!
 
Upvote 0
Just FYI, you may as well exit the loop once you found a True value:
Code:
For i = 1 To 12
    If Me.Controls("MonthButton" & i) = True Then
        CallMonth = i
        Exit For
    End If
Next
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

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