Forum Discussion
Calculate total working hours between date/time excluding weekends and break time
- Jul 15, 2024
I attached the wrong version, sorry.
The 2 hours in the first example is because the 1st of May is in the holiday list. If you remove that date from the holiday list, you get 3 hours.
See the attached version.
I simplified my formula to make it self-contained and to provide a closer match to HansVogelaar's VBA solution
Worksheet formula
= MAP(Start, End, HoursWorkedλ)
HoursWorkedλ
= LAMBDA(start, end,
LET(
timeline, SEQUENCE(1 + INT(end) - INT(start), 1, INT(start)),
shiftTimes, TIME({8,12,13,17},0,0),
activeShifts, {1, 0, 1, 0},
activeDays, NETWORKDAYS(+timeline, +timeline, holidays),
shiftChange, TOCOL(timeline + shiftTimes),
intervalStart, SORT(VSTACK(shiftChange, start)),
intervalEnds, SORT(VSTACK(shiftChange, end)),
intervalDuration, DROP(intervalEnds - intervalStart, 1),
workedDurations, activeShifts * activeDays * WRAPROWS(intervalDuration, 4),
SUM(workedDurations)
)
)
- VladLockhartJul 19, 2024Copper Contributor
PeterBartholomew1 Hi, idk why, it keeps error if i change the input. But I can now use HansVogelaar vba function and it works really fine. Thanks for helping me.
- PeterBartholomew1Jul 19, 2024Silver Contributor
The problem would appear to be that you are not using Excel 365 or 2021, so dynamic arrays, and the MAP lambda helper function in particular, are not available to you.
With legacy versions of Excel (a nicer way of saying 'obsolete'), VBA is your best way forward. That probably applies to your other issue of converting comma separated lists to arrays, although there options such as Power Query or manual processing such as Text to Columns exist.