These two functions together can do what you want.
- It assumes Monday-Friday workdays.
- It is inclusive of the date of the EndDate.
- Returns 0 if EndDate < StartDate.
- Returns 0 if EndDate is same month as StartDate.
- You also find uses for WorkDaysBetweenDates() by itself.
Usage:
WorkDaysInSubsequentMonths(#10/1/2009#, #9/30/2009#)
or:
WorkDaysInSubsequentMonths(Job_Start_Date, Job_End_Date)
Begin Code (Paste into a Module)
====================
Option Compare Database
Option Explicit
Const MOD_NAME As String = "modMain"
Public Function WorkDaysBetweenDates(StartDate As Date, EndDate As Date) As
Integer
Const PROC_NAME As String = "WorkDaysBetweenDates"
Dim dtCurrentDate As Date
Dim iDaysBetweenDates As Integer
Dim iResult As Integer
Dim iDayOfWeek As Integer
Dim iDayCounter As Integer
On Error GoTo ErrorHandler
' Determine the number of days between the two dates.
iDaysBetweenDates = DateDiff("d", StartDate, EndDate)
' Loop for the number of days, + 1
For iDayCounter = 0 To iDaysBetweenDates
' Get the date, per the day counter.
dtCurrentDate = DateAdd("d", iDayCounter, StartDate)
' Get the day-of-the-week of the current date.
iDayOfWeek = DatePart("w", dtCurrentDate, vbSunday)
' If it is a work-day...
If iDayOfWeek > 1 And iDayOfWeek < 7 Then
' Count it.
iResult = iResult + 1
End If
Next
Cleanup:
' Assign the result.
WorkDaysBetweenDates = iResult
Exit Function
ErrorHandler:
MsgBox "Error: " & Err.Number & ", " & Err.Description, , MOD_NAME & "."
& PROC_NAME
On Error Resume Next
GoTo Cleanup
End Function
Public Function WorkDaysInSubsequentMonths(StartDate As Date, EndDate As
Date) As Integer
Const PROC_NAME As String = "WorkDaysInSubsequentMonths"
Dim dtFirstDayOfNextMonth As Date
Dim iResult As Integer
On Error GoTo ErrorHandler
' If the start-date and end-date are in the same month...
If Month(StartDate) = Month(EndDate) And Year(StartDate) = Year(EndDate)
Then
' Nothing to do. Return 0.
GoTo Cleanup
End If
' Add one Month to the start date (automatically handles months that
don't have the same number
' of days, like Jan 31 and Feb 28.
dtFirstDayOfNextMonth = DateAdd("m", 1, StartDate)
' Get the first day of the next month.
dtFirstDayOfNextMonth = CDate(Month(dtFirstDayOfNextMonth) & "/1/" &
Year(dtFirstDayOfNextMonth))
' Get the work-days between the first day of the subsequent month and
the specified end-date.
iResult = WorkDaysBetweenDates(dtFirstDayOfNextMonth, EndDate)
Cleanup:
' Assign the result.
WorkDaysInSubsequentMonths = iResult
Exit Function
ErrorHandler:
MsgBox "Error: " & Err.Number & ", " & Err.Description, , MOD_NAME & "."
& PROC_NAME
On Error Resume Next
GoTo Cleanup
End Function
====================
End Code