Excel - Calculate Working Days

I needed to calculate Number of Working dates between to dates. If I subtract two dates I get total number of days.. but I had no clue on how to remove regular weekend holidays from that.

I searched on the net and found a solution, however they do not explain me where to start with and how to implement it. I being a Java guy.. I dont know how to work with Functions and VBScripting. I had to search multiple sites to get the complete information and finally arrived at the solution.

For all those ppl out there who are like me.. and need this useful script.. Simply follow the below steps to calculate number of working days between 2 dates.

  1. Open the Excel Document where you want this Formula created.
  2. On the Tools menu, point to Macro, and then click Visual Basic Editor.
  3. On the Insert menu in the Microsoft Visual Basic window, click Module.
  4. In the Module window, type the following code for the Date function...
    Public Function CountWorkingDays(ByVal varStartDate As Variant, ByVal varEndDate As Variant)
    Dim intCounter As Integer, intTotal As Integer
    Dim dtmCurrent As Date
    intTotal = 0

    If IsEmpty(varEndDate) Or IsEmpty(varStartDate) Then
    'Do Nothing
    Else

    For intCounter = 0 To (varEndDate - varStartDate)

    dtmCurrent = (varStartDate + intCounter)

    If (Weekday(dtmCurrent) = 1) Or (Weekday(dtmCurrent) = 7) Then
    'Do Nothing

    Else

    'Add the count
    intTotal = intTotal + 1

    End If

    Next intCounter

    End If

    CountWorkingDays = intTotal

    End Function

  5. Save the Module and Close the Microsoft Visual Basic window.
  6. Now in your Worksheet if you have 2 dates in 2 Cells, say D1 and F1 then your formula would be "=CountWorkingDays(D1, F1)"

No comments: