My working hours are Monday to Friday between 09:00 and 17:00. I have a sub that detects if a cell has been modified in Column 5 and returns the timestamp it was modified in a corresponding Cell in Column 6. My problem is, I want to Subtract the value between the delivery date in column 3 and the timestamp and return in a corresponding Cell in Column 8 a Value like "2 Days 3 Hrs 20 Mins". Any help will free me from this migraine. Thanks in advance. Below is my code so far.
Sub WorkSheet_Change(ByVal Target As Range)
Dim DeliveryDate As Date
Dim DayCount As Long
Dim EoD As Date
Dim SoD As Date
Dim StartDiff As Long
Dim EndDiff As Long
Dim TotalDiff As Long
Dim TotalHrs As Long
DayCount = 0
DeliveryDate = Cells(Target.Row, 6).Value
For x = Day(Now) + 1 To Day(DeliveryDate) - 1
D = Weekday(x)
If D <> 1 And D <> 7 Then DayCount = DayCount + 1
Next x
EoD = DateSerial(Year(Now), Month(Now), Day(Now)) + TimeSerial(17, 0, 0)
SoD = DateSerial(Year(DeliveryDate), Month(DeliveryDate), Day(DeliveryDate)) + TimeSerial(9, 0, 0)
StartDiff = DateDiff("n", Now, EoD)
EndDiff = DateDiff("n", SoD, DeliveryDate)
If StartDiff + EndDiff >= 480 Then
DayCount = DayCount + 1
TotalDiff = StartDiff + EndDiff - 480
Else
TotalDiff = StartDiff + EndDiff
End If
If TotalDiff >= 60 Then
TotalHrs = TotalDiff \ 60
TotalDiff = TotalDiff Mod 60
Else
TotalHrs = 0
End If
Application.EnableEvents = False
If Target.Column = 5 Then
If Target.Value Like "*" Then
Cells(Target.Row, 7).Value = DateTime.Now 'Timetamp
Cells(Target.Row, 8).Value = DayCount & " Business Days, " & TotalHrs & " Business Hours, " & TotalDiff & " Business Mins Remain"
End If
If Target.Value = "" Then
Cells(Target.Row, 7).Value = ""
Me.Cells(Target.Row, 8).Value = ""
End If
Application.EnableEvents = True
End If
End Sub
EDIT: AT LONG LAST... a working solution! Let me know if this works!
First it finds out how many days (weekdays), then it finds out the remaining hours and minutes (
SoD
andEoD
for Start of Day and End of Day), then if those minutes are more than one day it adds that to total days, then it finds remaining hours by dividing out the minutes, then it leaves the remainder in minutes. Let me know if this works.EDIT: Added a check for if ReqDate is on the weekend.