Try
Option Explicit
Sub HPW()
Dim sDate As Date, Shift_Start As Double, lBreak As Double
Dim dT As Double, wkday As Integer, icol As Integer, n As Integer, lr As Long, wsn As Long
Dim res As Variant, bYear As Integer, r As Long
Dim wkhours As Double, ft As Double, Total_Workhours As Double
Dim Shift_End As Double
Dim wk_hrs(1 To 53) As Double, week_hours As Double
Application.ScreenUpdating = False
bYear = Year(Now) ' "Base" year for holiday checking
lr = Cells(Rows.Count, "B").End(xlUp).Row
For r = 5 To lr ' Loop through input data
sDate = Int(Cells(r, "E")): Shift_Start = Cells(r, "E") - sDate ' Initial Start date and Start time
dT = Cells(r, "G") ' Duration (Hours)
wsn = Cells(r, "F") ' Start Week
n = 1: Total_Workhours = 0: week_hours = 0
Do
wkday = Weekday(sDate, vbMonday) ' Weekday (1=Monday, 7=Sunday)
If wkday < 6 Then ' Check if Monday to Friday
If wkday <= 4 Then ' Monday to Thursday
Shift_End = Application.Min(Shift_Start + (dT / 24), Range("MT_End_time")) ' Finish time (16:30)
lBreak = 1 / 48 ' 30 minute lunch break
Else
Shift_End = Range("F_End_time") ' Friday finish (12:00)
lBreak = 0 ' No lunch break
End If ' increment "day" index
week_hours = week_hours + (Shift_End - Shift_Start - lBreak) * 24 ' Assign hours for this day
Total_Workhours = Total_Workhours + (Shift_End - Shift_Start - lBreak) * 24 ' Accumulate Total hours
Else
If wkday = 7 Then ' End of week
wk_hrs(n) = week_hours ' Weekly hours
week_hours = 0
n = n + 1 ' Increment week number
End If
End If
Do ' Check for holidays
sDate = sDate + 1
icol = Year(sDate) - bYear + 1 ' Holiday year column in range "Holidays"
res = Application.Match(CLng(sDate), Range("holidays").Columns(icol), 0) ' check if date is a holiday
Loop While IsNumeric(res) ' Loop while match found
Shift_Start = Range("Start_Time") ' Standard start time
Loop While Round(Total_Workhours, 2) < dT ' Loop while work hours < duration hours
If (Total_Workhours >= dT) Then wk_hrs(n) = week_hours - (Total_Workhours - dT) ' Hours in last week
With Sheets("Resource Requirements")
.Cells(r, wsn + 11).Resize(1, n) = wk_hrs ' Output weekly hours
End With
Next r
End Sub
Bookmarks