Chief Ace
Member
Good evening Excel Ninja's,
Kindly requesting assistant to calculate the following either by VBA macros or formula. What i am trying to do is to calculate the hours of work from time in to time out but first the 8 hours of regular must be met before employee can be credited overtime. Please see sample file. I even try using the below macro to no avail it doesn't separate the overtime to another column once the 8 hours of regular has been met. Thank you in advance for any assist.
>>> use code - tags <<<
Kindly requesting assistant to calculate the following either by VBA macros or formula. What i am trying to do is to calculate the hours of work from time in to time out but first the 8 hours of regular must be met before employee can be credited overtime. Please see sample file. I even try using the below macro to no avail it doesn't separate the overtime to another column once the 8 hours of regular has been met. Thank you in advance for any assist.
>>> use code - tags <<<
Code:
Sub CalculateHours()
Dim StartTime As Date
Dim EndTime As Date
Dim RegularHours As Double
Dim OvertimeHours As Double
For Each cell In Range("B2:B" & Cells(Rows.Count, "B").End(xlUp).Row)
StartTime = cell.Value
EndTime = cell.Offset(0, 1).Value
If IsDate(StartTime) And IsDate(EndTime) Then
If EndTime < StartTime Then
EndTime = EndTime + 1
End If
RegularHours = (EndTime - StartTime) * 24
If RegularHours >= 8 Then
OvertimeHours = RegularHours - 8
RegularHours = 8
Else
OvertimeHours = 0
End If
cell.Offset(0, 2).Value = RegularHours
cell.Offset(0, 3).Value = OvertimeHours
If OvertimeHours > 0 Then
cell.Offset(0, 3).Value = OvertimeHours
cell.Offset(0, 2).Value = 8
End If
End If
Next cell
End Sub
Attachments
Last edited by a moderator: