So I have a simple userform where I require the User to input the target month, and the day to analyse data up to.
This is just a small section of code to govern populating the month selection and then determining days based on the month.
Particular questions:
Is this a smart way to handle dates generally?
Is this a good way to ensure that the User cannot enter a non-existent date?
Any edge cases I haven't considered?
And, as always, if you were handed this code to maintain, what would you be thinking as you read through it?
Private Sub UserForm_Initialize()
PopulateMonthBox Me.UF_BankRec_cbx_Month
End Sub
Private Sub PopulateMonthBox(ByRef monthBox As MSForms.ComboBox)
Dim ix As Long
Dim monthText As String
For ix = 1 To 12
monthText = MonthName(ix)
monthBox.AddItem monthText
Next ix
End Sub
Private Sub UF_BankRec_cbx_Month_Change()
Dim dayBox As MSForms.ComboBox
Set dayBox = Me.UF_BankRec_cbx_EndDay
Dim monthBox As MSForms.ComboBox
Set monthBox = Me.UF_BankRec_cbx_Month
Dim monthText As String
monthText = monthBox.Text
dayBox.Clear
PopulateDayBox dayBox, monthText
End Sub
Private Sub PopulateDayBox(ByRef dayBox As MSForms.ComboBox, ByVal monthText As String)
Dim ix As Long, dayLimit As Long
Dim ixMonth As Long, ixNextMonth As Long
Dim ixYear As Long
Dim firstDayNextMonth As Date, lastDayThisMonth As Date
ixMonth = Month("01/" & monthText & "/2000")
ixNextMonth = ixMonth + 1
ixYear = Year(Now)
firstDayNextMonth = DateSerial(ixYear, ixNextMonth, 1)
lastDayThisMonth = DateAdd("d", -1, firstDayNextMonth)
dayLimit = Day(lastDayThisMonth)
For ix = 1 To dayLimit
dayBox.AddItem ix
Next ix
End Sub