Count working days difference


Since function workingDaysDifference uses some non-built VBA functions, they also must be included in your code for the function to work properly.

Otherwise the following error will occur: Compile error: Sub or Function not defined.

Required functions are listed below. You can get to each function's source code by clicking its name:

When adding the functions above to your VBA project, make sure you haven't done it before. If there are two different public functions with the same name in a single VBA project, the following compilation error is thrown: Compile error: Ambiguous name detected: function_name.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
'**********************************************************************************************************
' Name:                 workingDaysDifference
' Author:               mielk | 2014-02-10
'
' Comment:              Function to count the number of working days between two dates.
'                       Only Saturdays and Sundays are considered to be non-working days. Function doesn't
'                       recognize holidays.
'
' Parameters:
'   baseDate            Base date.
'   comparedDate        Date to be compared with the base date.
'
'
' Returns:
'   Integer             The number of working days between two dates.
'                       If [baseDate] is earlier than [comparedDate] the result is positive, otherwise the
'                       result is negative.
'
'
'
' --- Changes log -----------------------------------------------------------------------------------------
' 2013-04-10        mielk           Function created.
'**********************************************************************************************************
Public Function workingDaysDifference(baseDate As Date, comparedDate As Date) As Integer
    Const METHOD_NAME As String = "workingDaysDifference"
    '------------------------------------------------------------------------------------------------------
    Dim weeksDifference As Integer
    '------------------------------------------------------------------------------------------------------

    weeksDifference = VBA.DateDiff("ww", baseDate, comparedDate, vbMonday)
    workingDaysDifference = comparedDate - baseDate - (weeksDifference * 2)

End Function