Working Days incl Holidays (1 Viewer)

Samand

Registered User.
Local time
Today, 12:37
Joined
May 14, 2013
Messages
10
I'm trying to work out the number of working days between 2 days (eg 01/05/2013 & today). I found the following code:
Code:
' Weekdays Function
Public Function Weekdays(ByRef startDate As Date, _
    ByRef endDate As Date _
    ) As Integer
    ' Returns the number of weekdays in the period from startDate
    ' to endDate inclusive. Returns -1 if an error occurs.
    ' If your weekend days do not include Saturday and Sunday and
    ' do not total two per week in number, this function will
    ' require modification.
    On Error GoTo Weekdays_Error
    
    ' The number of weekend days per week.
    Const ncNumberOfWeekendDays As Integer = 2
    
    ' The number of days inclusive.
    Dim varDays As Variant
    
    ' The number of weekend days.
    Dim varWeekendDays As Variant
    
    ' Temporary storage for datetime.
    Dim dtmX As Date
    
    ' If the end date is earlier, swap the dates.
    If endDate < startDate Then
        dtmX = startDate
        startDate = endDate
        endDate = dtmX
    End If
    
    ' Calculate the number of days inclusive (+ 1 is to add back startDate).
    varDays = DateDiff(Interval:="d", _
        date1:=startDate, _
        date2:=endDate) + 1
    
    ' Calculate the number of weekend days.
    varWeekendDays = (DateDiff(Interval:="ww", _
        date1:=startDate, _
        date2:=endDate) _
        * ncNumberOfWeekendDays) _
        + IIf(DatePart(Interval:="w", _
        Date:=startDate) = vbSunday, 1, 0) _
        + IIf(DatePart(Interval:="w", _
        Date:=endDate) = vbSaturday, 1, 0)
    
    ' Calculate the number of weekdays.
    Weekdays = (varDays - varWeekendDays)
    
Weekdays_Exit:
    Exit Function
    
Weekdays_Error:
    Weekdays = -1
    MsgBox "Error " & Err.Number & ": " & Err.Description, _
        vbCritical, "Weekdays"
    Resume Weekdays_Exit
End Function
' Workdays Function
Public Function WorkDays(ByRef startDate As Date, _
     ByRef endDate As Date, _
     Optional ByRef strHolidays As String = "Holidays" _
     ) As Integer
    ' Returns the number of workdays between startDate
    ' and endDate inclusive.  Workdays excludes weekends and
    ' holidays. Optionally, pass this function the name of a table
    ' or query as the third argument. If you don't the default
    ' is "Holidays".
    On Error GoTo Workdays_Error
    Dim nWeekdays As Integer
    Dim nHolidays As Integer
    Dim strWhere As String
    
    ' DateValue returns the date part only.
    startDate = DateValue(startDate)
    endDate = DateValue(endDate)
    
    nWeekdays = Weekdays(startDate, endDate)
    If nWeekdays = -1 Then
        WorkDays = -1
        GoTo Workdays_Exit
    End If
    
    strWhere = "[Holidays] >= #" & startDate _
        & "# AND [Holidays] <= #" & endDate & "#"
    
    ' Count the number of holidays.
    nHolidays = DCount(Expr:="[Holidays]", _
        Domain:=strHolidays, _
        Criteria:=strWhere)
    
    WorkDays = nWeekdays - nHolidays
    
Workdays_Exit:
    Exit Function
    
Workdays_Error:
    WorkDays = -1
    MsgBox "Error " & Err.Number & ": " & Err.Description, _
        vbCritical, "Workdays"
    Resume Workdays_Exit
    
End Function

The holidays table has the following dates:
Holidays
01/01/2013
29/03/2013
01/04/2013
06/05/2013

For some reason the code deducts 29/03 & 01/04 when run the code and I can't figure out why.
 

spikepl

Eledittingent Beliped
Local time
Today, 21:37
Joined
Nov 3, 2010
Messages
6,142
Show how you call this function and with what values
 

spikepl

Eledittingent Beliped
Local time
Today, 21:37
Joined
Nov 3, 2010
Messages
6,142
Read this http://allenbrowne.com/ser-36.html

This will explain why #01/05/2013# is perceived as Jan 5, 2013.

Also, '' are not legit delimiters for dates, but ## are, so a date in Access is written as #01/05/2013#
 

Samand

Registered User.
Local time
Today, 12:37
Joined
May 14, 2013
Messages
10
Read this...

This will explain why #01/05/2013# is perceived as Jan 5, 2013.

Also, '' are not legit delimiters for dates, but ## are, so a date in Access is written as #01/05/2013#

Thanks. I had a feeling that this might be it, but didn't fully get it until I read the article.
 

Users who are viewing this thread

Top Bottom