blossomthe2nd
Active Member
- Joined
- Oct 11, 2010
- Messages
- 450
Hi Guys
I totally plagerised this code to calculate how much time has elapsed between a startdate and starttime to enddate and endtime , counting the day as a 9 to 6 Working day. -
It works perfectly for everyday except when the Month changes and then it does not work, so I guess I need to change the code -- I dont even know where to start ,
Can anyone PLEASE PLEASE Help me ?
Thanks
A
=nhw(C8 + B8,P8+O8,"09:00","18:00",0)
I totally plagerised this code to calculate how much time has elapsed between a startdate and starttime to enddate and endtime , counting the day as a 9 to 6 Working day. -
It works perfectly for everyday except when the Month changes and then it does not work, so I guess I need to change the code -- I dont even know where to start ,
Can anyone PLEASE PLEASE Help me ?
Thanks
A
=nhw(C8 + B8,P8+O8,"09:00","18:00",0)
HTML:
Function nhw(StartingDateTime As Date, EndingDateTime As Date, HourStart As Date, _
HourEnd As Date, ReturnType As Integer) As Variant
Dim TotalNetHoursWorked
Dim ThisDayEnd As Date
Dim ThisDayBegin As Date
Dim Cntr As Integer
Application.Volatile
If StartingDateTime = 0 Then Exit Function
If EndingDateTime = 0 Then Exit Function
If Format(StartingDateTime, "DDDD") = "Friday" And Hour(StartingDateTime) > 17 Then
StartingDateTime = Format(DateAdd("d", 3, StartingDateTime), "MM/DD/YY") & " 09:00 AM"
ElseIf Format(StartingDateTime, "DDDD") = "Saturday" Then
StartingDateTime = Format(DateAdd("d", 2, StartingDateTime), "MM/DD/YY") & " 09:00 AM"
ElseIf Format(StartingDateTime, "DDDD") = "Sunday" Then
StartingDateTime = Format(DateAdd("d", 1, StartingDateTime), "MM/DD/YY") & " 09:00 AM"
End If
If DateDiff("d", StartingDateTime, EndingDateTime) > 1826 Then
MsgBox "Out of range - Greater than five years."
nhw = "Invalid Time"
Exit Function
End If
ThisDayEnd = Month(StartingDateTime) & "/" & Day(StartingDateTime) & "/" & _
Year(StartingDateTime)
ThisDayEnd = ThisDayEnd + HourEnd
If ThisDayEnd > EndingDateTime Then
TotalNetHoursWorked = DateDiff("n", StartingDateTime, EndingDateTime)
GoTo AllDone
End If
TotalNetHoursWorked = DateDiff("n", StartingDateTime, ThisDayEnd)
For Cntr = 1 To 1826
ThisDayBegin = Month(StartingDateTime + Cntr) & "/" & Day(StartingDateTime + Cntr) & "/" & _
Year(StartingDateTime + Cntr) & Format(HourStart, " HH:MM")
ThisDayEnd = ThisDayBegin + (HourEnd - HourStart)
If ThisDayBegin > EndingDateTime Then Exit For
If ThisDayEnd > EndingDateTime Then
If Format(ThisDayEnd, "MMMM") = "Saturday" Then Exit For
TotalNetHoursWorked = TotalNetHoursWorked + DateDiff("n", ThisDayBegin, EndingDateTime)
Exit For
End If
If Format(ThisDayEnd, "DDDD") <> "Saturday" And _
Format(ThisDayEnd, "DDDD") <> "Sunday" Then
TotalNetHoursWorked = TotalNetHoursWorked + DateDiff("n", ThisDayBegin, ThisDayEnd)
End If
Next
AllDone:
Debug.Print TotalNetHoursWorked Mod 60
If TotalNetHoursWorked > 1440 Then
nhw = Int(TotalNetHoursWorked / 60) & ":" & TotalNetHoursWorked Mod 60 & ":00"
Else: nhw = 0.000694444 * TotalNetHoursWorked
End If
End Function