Function getFirstDayOfMonth(month As Date) As Date
On Error GoTo Err_Handler
'returns the first day of a month
Dim strDate As String
strDate = DatePart("m", month, vbMonday) & "/" & "1/" & DatePart("yyyy", month, vbMonday)
getFirstDayOfMonth = CDate(strDate)
Done:
Exit Function
Err_Handler:
MsgBox Err.Description, vbCritical, "Error #: " & Err.Number
Resume Done
End Function
Function getFirstDayOfNextMonth(month As Date) As Date
On Error GoTo Err_Handler
'returns the first day of the next month
Dim strDate As String
strDate = DatePart("m", month, vbMonday) & "/" & "1/" & DatePart("yyyy", month, vbMonday)
strDate = DateAdd("m", 1, strDate)
getFirstDayOfNextMonth = CDate(strDate)
Done:
Exit Function
Err_Handler:
MsgBox Err.Description, vbCritical, "Error #: " & Err.Number
Resume Done
End Function
Function numberofweeks(date1 As Date, date2 As Date)
On Error GoTo Err_Handler
'returns the number of weeks between two dates
'
numberofweeks = DateDiff("ww", date1, date2, vbMonday, vbFirstJan1)
Done:
Exit Function
Err_Handler:
MsgBox Err.Description, vbCritical, "Error #: " & Err.Number
Resume Done
End Function
Public Function getnumofmonths(date1 As Date, date2 As Date)
On Error GoTo Err_Handler
'returns the number of months on two dates
getnumofmonths = DateDiff("m", getFirstDayOfMonth(date1), EofMonth(date2), vbMonday, vbFirstJan1)
Done:
Exit Function
Err_Handler:
MsgBox Err.Description, vbCritical, "Error #: " & Err.Number
Resume Done
End Function
Public Function weeknum(date1 As Date) As Integer
On Error GoTo Err_Handler
'returns the week number
Dim strYear As String
strYear = "01/01/"
strYear = strYear & Right(date1, 2)
'get the week number based on the incoming date
'of the year...
weeknum = DateDiff("ww", strYear, date1, vbMonday, vbFirstJan1)
'weeks start at 1 till 52...
If weeknum = 0 Then
weeknum = weeknum + 1
Else
'do nothing
End If
Done:
Exit Function
Err_Handler:
MsgBox Err.Description, vbCritical, "Error #: " & Err.Number
Resume Done
End Function
Public Function monthname(date1 As Date, incre As Double) As Date
On Error GoTo Err_Handler
monthname = DateAdd("m", incre, date1)
Done:
Exit Function
Err_Handler:
MsgBox Err.Description, vbCritical, "Error #: " & Err.Number
Resume Done
End Function
Public Function EofMonth(date1 As Date) As Date
On Error GoTo Err_Handler
Dim myDate As Date
myDate = getFirstDayOfMonth(date1)
myDate = DateAdd("m", 1, myDate)
EofMonth = DateAdd("d", -1, myDate)
Done:
Exit Function
Err_Handler:
MsgBox Err.Description, vbCritical, "Error #: " & Err.Number
Resume Done
End Function