I know that I can get the number of a month by feeding VB's built in MonthName function the month numberHowever, I need to the number of the month based on its name, and I could not find anything that would give me that.vb Code:
'returns November Msgbox MonthName(11)
So, I wrote this function (I have the names of all 12 months in a dropdown combo).Works just fine but I can't help feeling I'm overlooking something.vb Code:
Private Function GetMonthNumber(pstrMonthName As String) As Integer Dim i As Integer ReDim Months(1 To 12) ReDim MonthNumber(1 To 12) Months = Array("January", "February", "March", "April", "May", "June", "July", _ "August", "September", "October", "November", "December") MonthNumber = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12") For i = 0 To 11 If cboMonths.Text = Months(i) Then GetMonthNumber = MonthNumber(i) Exit For End If Next End Function 'and to get the number Private Sub cboMonths_Click() Dim iMonNum As Integer iMonNum = GetMonthNumber(cboMonths.Text) MsgBox iMonNum End Sub
Am I having a senior moment? There is an easier way of doing this, right?




Reply With Quote