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.
- Open the Excel Document where you want this Formula created.
- On the Tools menu, point to Macro, and then click Visual Basic Editor.
- On the Insert menu in the Microsoft Visual Basic window, click Module.
- 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 - Save the Module and Close the Microsoft Visual Basic window.
- Now in your Worksheet if you have 2 dates in 2 Cells, say D1 and F1 then your formula would be "=CountWorkingDays(D1, F1)"