Question : Working Days Calculator

Is there any way of writing a function that will calculate the no of working days between two dates, WITHOUT looping through each row.

I am currently using this ...



Public Function GetNumberOfWorkDays(sStartDate, sEndDate)
  Dim iDays
  Dim iWorkDays
  Dim sDay
  Dim i
 
  iDays = DateDiff("d", sStartDate, sEndDate)
 
  iWorkDays = 0
 
  For i = 0 To iDays
    'First day of the week is sunday
    sDay = WeekDay(DateAdd("d", i, sStartDate))
    If sDay <> 1 And sDay <> 7 Then
      iWorkDays = iWorkDays + 1
    End If
  Next
  GetNumberOfWorkDays = iWorkDays
End Function

This function works fine, however has one small glitch! it doesn't run too well if there 65000 rows to check through.. in fact it gets to the point of crashing the application. 65000 rows * the amount of loops for each one { depending on the difference between the dates } This doesn't take into account for holidays ... but i'm not worried about this for now i can build that in after.

Any ideas would be greatly appreciated..

thanks

Dave

Answer : Working Days Calculator

butcherd11
If you want to use Datediff properly, then the following is a complete one line function to count weekday differences, no loops, no external references. It doesn't matter if the start date is later than the end date:


Function GetNumberOfWorkDays(sStartDate, sEndDate)

GetNumberOfWorkDays=Abs(5 - (Abs(Weekday(sStartDate, 7) - 2) + (Weekday(sStartDate, 7) - 2)) / 2 + (DateDiff("ww", sStartDate, sEndDate, 7) - 1) * 5 + (Abs(Weekday(sEndDate, 7) - 2) + (Weekday(sEndDate, 7) - 2)) / 2)

End Function

Regards,
John
Random Solutions  
 
programming4us programming4us