Afternoon All,
I've been looking at code posted on the forums in 2005, however the user has not been active since 2012. Looking to try and figure out why I don't get a correct answer when running the function. I've been staring at it for quite some time now to try and see the issue with no luck. Anyone able decipher why? I'm pretty good with VBA, but custom functions involving time is a bit much for my skill level.
I've edited the function to use an example of two dates, however your database will need a holiday table with the field HOLDATE in it to run properly.
USER PSIREN17 posted this code at this link originally.
(Linked Removed as you need > 10 posts)
I've been looking at code posted on the forums in 2005, however the user has not been active since 2012. Looking to try and figure out why I don't get a correct answer when running the function. I've been staring at it for quite some time now to try and see the issue with no luck. Anyone able decipher why? I'm pretty good with VBA, but custom functions involving time is a bit much for my skill level.
I've edited the function to use an example of two dates, however your database will need a holiday table with the field HOLDATE in it to run properly.
USER PSIREN17 posted this code at this link originally.
(Linked Removed as you need > 10 posts)
Code:
Public Function NetWorkhours()
'(dteStart As Date, dteEnd As Date) As Single
''''''''''''''''''''''''''''''''''''''
' CODE TAKEN FROM: LINK REMOVED < 10 POSTS
' USER: PSIREN17
'
'
''''''''''''''''''''''''''''''''''''''
Dim intGrossDays As Integer
Dim intGrossHours As Single
Dim dteCurrDate As Date
Dim i As Integer
Dim WorkDayStart As Date
Dim WorkDayend As Date
Dim nonWorkDays As Integer
Dim StartDayhours As Single
Dim EndDayhours As Single
''''''''''''''''''''''''''''''''''''''
'USE THIS TO TEST OUT THE FUNCTION
''''''''''''''''''''''''''''''''''''''
Dim dteStart As Date
Dim dteEnd As Date
dteStart = "9/15/2015 8:33:23 AM"
dteEnd = "9/17/2015 8:29:58 AM"
''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''
NetWorkhours = 0
nonWorkDays = 0
'Calculate work day hours on 1st and last day
WorkDayStart = DateValue(dteEnd) + TimeValue("08:00am")
WorkDayend = DateValue(dteStart) + TimeValue("06:00pm")
StartDayhours = DateDiff("n", dteStart, WorkDayend)
EndDayhours = DateDiff("n", WorkDayStart, dteEnd)
'adjust for time entries outside of business hours
If StartDayhours < 0 Then
StartDayhours = 0
End If
If EndDayhours > 8 Then
EndDayhours = 8
End If
'Calculate total hours and days between start and end times
intGrossDays = DateDiff("d", (dteStart), (dteEnd))
intGrossHours = DateDiff("n", (dteStart), (dteEnd))
'count number of weekend days and holidays (from a table called "Holidays" that lists them)
For i = 0 To intGrossDays
dteCurrDate = dteStart + i
If Weekday(dteCurrDate, vbSaturday) < 3 Then
nonWorkDays = nonWorkDays + 1
Else
If Not IsNull(DLookup("[HolDate]", "Holidays", "[HolDate] = #" & Int(dteCurrDate) & "#")) Then
nonWorkDays = nonWorkDays + 1
End If
End If
Next i
'Calculate number of work hours
Select Case intGrossDays
Case 0
'start and end time on same day
NetWorkhours = intGrossHours
Case 1
'start and end time on consecutive days
NetWorkhours = NetWorkhours + StartDayhours
NetWorkhours = NetWorkhours + EndDayhours
Case Is > 1
'start and end time on non consecutive days
NetWorkhours = NetWorkhours - (nonWorkDays * 1)
NetWorkhours = (intGrossDays - nonWorkDays) * 8
NetWorkhours = NetWorkhours + StartDayhours
NetWorkhours = NetWorkhours + EndDayhours
Debug.Print NetWorkhours / 60
End Select
End Function