was hoping someone could help with the following.
this is the criteria:
Monday to Friday:06:00 to 7:30 (2 points per hour) 14:00 to 17:00 (1 point per hour) 17:00 to 19:00 (2 points per hour) 19:00-01:00 (3 points per hour)
Saturday 06:00 to 17:00 (2 points per hour) 17:00 to 01:00 (3 points per hour)
Sunday 06:00 to 17:00 (2 points per hour) 17:00 to 01:00 (4 points per hour)
This is the code:
Sub CalculateOvertimePoints()
Dim i As Long
Dim lastRow As Long
Dim startTime As Date, endTime As Date
Dim timeString As String
Dim duration As Double
' Get the last row in column E
lastRow = Cells(Rows.Count, "E").End(xlUp).Row
' Loop through each row from 30 to the last row
For i = 30 To lastRow
timeString = Cells(i, "F").Value
If InStr(1, timeString, "-") > 0 Then
startTime = TimeValue(Split(timeString, "-")(0))
endTime = TimeValue(Split(timeString, "-")(1))
If endTime < startTime Then
endTime = DateAdd("d", 1, endTime)
End If
duration = endTime - startTime
If duration > 0 Then
Dim points As Double
points = 0
Dim currentTime As Date
currentTime = startTime
While currentTime < endTime
Dim nextTime As Date
nextTime = DateAdd("n", 30, currentTime)
Dim hourPoints As Double
hourPoints = 0
Select Case Cells(i, "E").Value
Case "Monday-Friday"
If (currentTime >= TimeValue("06:00") And currentTime < TimeValue("07:30")) Then
hourPoints = 1
ElseIf (currentTime >= TimeValue("14:00") And currentTime < TimeValue("17:00")) Then
hourPoints = 0.5
ElseIf (currentTime >= TimeValue("17:00") And currentTime < TimeValue("19:00")) Then
hourPoints = 1
ElseIf (currentTime >= TimeValue("19:00") Or currentTime < TimeValue("01:00")) Then
hourPoints = 1.5
End If
Case "Saturday"
If (currentTime >= TimeValue("06:00") And currentTime < TimeValue("17:00")) Then
hourPoints = 1
ElseIf (currentTime >= TimeValue("17:00") And currentTime < TimeValue("19:00")) Then
hourPoints = 1
ElseIf (currentTime >= TimeValue("19:00") Or currentTime < TimeValue("02:00")) Then
hourPoints = 1.5
End If
Case "Sunday"
If (currentTime >= TimeValue("06:00") And currentTime < TimeValue("17:00")) Then
hourPoints = 2
ElseIf (currentTime >= TimeValue("17:00") And currentTime < TimeValue("18:00")) Then
hourPoints = 1
ElseIf (currentTime >= TimeValue("18:00") And currentTime < TimeValue("19:00")) Then
hourPoints = 2
End If
End Select
points = points + hourPoints
currentTime = nextTime
Wend
' Adjust points based on fractional hours
Dim fractionalHours As Double
fractionalHours = (endTime - startTime) * 24 Mod 1
points = points + fractionalHours * (hourPoints / 60)
totalPoints = totalPoints - prevHourPoints ' Subtract the last hourPoints value
totalPoints = totalPoints + fractionalHours * (prevHourPoints / 1) ' Calculate fractional points
Cells(i, "L").Value = points
Else
Cells(i, "L").Value = 0
End If
Else
Cells(i, "L").Value = 0
End If
Next i
End Sub
I attached a picture of 3 scenario's - 1Ste answer should be 4.5 second one should be 12 and 3de one should be 24.
Hope someone can help, thank you.
The code has issu's with only some scenario's and I dont understand it, if you change the times to maybe a bit later then it works out correctly.