Hi InfoNow,
Just had a quick look at the FAQ and yes, it can be done, but it needs a slight amendment first.
The problem is that the counting of days is based partially on rounding of the Date/Time variables passed to it. If the variables contain times you may get funny results, for example:
28 Apr 2004 12:01pm to 29 Apr 2004 11:59am returns 1 day
28 Apr 2004 11:59am to 29 Apr 2004 11:59am returns 2 days
28 Apr 2004 11:59am to 29 Apr 2004 12:01pm returns 3 days
Apart from the inconsistent results, the way the code works means that there is not necessarily a check on whether the last (partial) day in the date range is a business day or not and, therefore, an adjustment for hours can not necessarily be made for that day. To remove this inconsistency, the
Clng Functions in the loop control need changing to
DateValue Functions so, firstly, change ..
Code:
[purple] For Idx = CLng(StartDate) To CLng(EndDate)[/purple]
.. to ..
Code:
[blue] For Idx = DateValue(StartDate) To DateValue(EndDate)[/blue]
Now, assuming you simply want a
total count of hours (24 per work day, and not any kind of business-only hours), you can make the appropriate adjustment for hours at the beginning and end of the date range by adding a couple of extra checks; change ..
Code:
[purple] If (rstHolidays.NoMatch) Then
NumDays = NumDays + 1
Else
[green]'Do Nothing, it is NOT a Workday[/green]
End If
[/purple]
.. to ..
Code:
[blue] If (rstHolidays.NoMatch) Then
[red] If DateValue(MyDate) = DateValue(StartDate) Then
HoursAdjust = (DateValue(StartDate) - CDbl(StartDate)) * 24
End If
If DateValue(MyDate) = DateValue(EndDate) Then
HoursAdjust = HoursAdjust + (DateValue(EndDate) - CDbl(EndDate)) * 24
End If[/red]
NumDays = NumDays + 1
Else
[green]'Do Nothing, it is NOT a Workday[/green]
End If[/blue]
.. and then change the returned value to hours instead of days, and including the adjustment. So, rename the function by changing ..
Code:
[purple]Public Function DeltaDays(StartDate As Date, EndDate As Date) As Integer[/purple]
.. to ..
Code:
[blue]Public Function Delta[red]Hours[/red](StartDate As Date, EndDate As Date) As Integer[/blue]
.. and, finally, change ..
Code:
[purple] DeltaDays = NumDays[/purple]
.. to ..
Code:
[blue] Delta[red]Hours[/red] = CLng((NumDays) * 24 + HoursAdjust)[/blue]
I haven't tested this thoroughly, so please make sure it's correct before relying on it.
Enjoy,
Tony
--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.