Hi,
Needing help please in possibly correcting the VB code below to remove holidays and weekends for time stamp data. I use a table called tblHolidays (column name 'HolidayDate') with the Function below to remove holidays 12/24/15 and 12/25/15 out of the calculation. However the results seems to calculate 1 day less than what it should. Basically if someone started a task on 12/21/15 11:15 AM and finished on 12/28/15 11:17 AM - I would think that the result should be three days instead of two - given that there were 2 corporate holidays and 2 weekend days and then subtracted from a total of 7 days.
For the second row, I can see the code reducing by one day compared to the first row - since it wasn't quite a full 7 days (6.99), but the second row seems like it should be 2 days removing 4 days of holidays and weekends.
Any help is appreciated. Thanks!
Query:
Time_Minus_Holidays_Minus_Weekends: WorkingDays2([Begin_Date_Time],[End_Date_Time])
Result:
Begin_Date_Time End_Date_Time Time_Minus_Holidays_Minus_Weekends Real_Time_No_Holidays_No_Weekends 12/21/15 11:15 AM 12/28/15 11:17 AM 2 7.001 12/21/15 11:20 AM 12/28/15 11:18 AM 1 6.999
Code:Option Compare Database Option Explicit Public Function WorkingDays2(StartDate As Date, EndDate As Date) As Integer On Error GoTo Err_WorkingDays2 Dim intCount As Integer Dim rst As DAO.Recordset Dim DB As DAO.Database Set DB = CurrentDb Set rst = DB.OpenRecordset("SELECT [HolidayDate] FROM tblHolidays", dbOpenSnapshot) StartDate = StartDate + 1 intCount = 0 Do While StartDate <= EndDate 'rst.FindFirst "[HolidayDate] = #" & StartDate & "#" rst.FindFirst "[HolidayDate] >= #" & StartDate _ & "# AND [HolidayDate] <= #" & EndDate & "#" If Weekday(StartDate) <> vbSunday And Weekday(StartDate) <> vbSaturday Then If rst.NoMatch Then intCount = intCount + 1 End If StartDate = StartDate + 1 Loop WorkingDays2 = intCount Exit_WorkingDays2: Exit Function Err_WorkingDays2: Select Case Err Case Else MsgBox Err.Description Resume Exit_WorkingDays2 End Select End Function