Calculating Time Error

andrewb90

Well-known Member
Joined
Dec 16, 2009
Messages
1,077

Hello All,


I am running a formula that will count the number of employees scheduled at a particular time. (Each formula references a different hour). In addition to what is an incredibly long formula, I now have discovered that if a shift is scheduled to end past 23:59, then the entire shift will not be counted.
Can somebody assist me in fixing this, and if you have any hints of shortening this formula, that would be greatly appreciated.

Code:
=(SUM(IF(E$87:E$111<$B87,IF(F$87:F$111>$A87,IF(F$87:F$111<$B87,F$87:F$111,$B87)-IF(E$87:E$111>$A87,E$87:E$111,$A87))))*24)+(SUM(IF(E$113:E$137<$B87,IF(F$113:F$137>$A87,IF(F$113:F$137<$B87,F$113:F$137,$B87)-IF(E$113:E$137>$A87,E$113:E$137,$A87))))*24)+(SUM(IF(E$139:E$146<$B87,IF(F$139:F$146>$A87,IF(F$139:F$146<$B87,F$139:F$146,$B87)-IF(E$139:E$146>$A87,E$139:E$146,$A87))))*24)+(SUM(IF(E$148:E$156<$B87,IF(F$148:F$156>$A87,IF(F$148:F$156<$B87,F$148:F$156,$B87)-IF(E$148:E$156>$A87,E$148:E$156,$A87))))*24)

Thanks,

Andrew
 
The problem is that if a shift runs past midnight (00:00) then the formulas completely ignore the entire shift

The problem arises because you (should) count shifts that start before the time period and end after the time period. Shifts ending after midnight do meet both conditions.

Try one of the following solutions.


A
B
C
D
E
F
G
1
Solution #1






2
Joe
8:0014:00
14:0015:001
3
Sara15:0021:00

15:0016:002
4
Bob18:00
0:30
16:0017:002
5
Sam17:0023:30
17:0018:003
6
Chad14:0019:00
18:0019:004
7




19:0020:003

<tbody>
</tbody>

Enter the following formula into G2, and copy down:

=COUNTIFS($B$2:$B$6, "<=" & E2, $C$2:$C$6, ">=" & F2)

When shifts start before midnight, the key is to enter after-midnight times as a formula, like the following in C4:

=1 + "0:30"

Note the double-quotes around the actual time. All time cells are formatted as Custom hh:mm.


A
B
C
D
E
F
G
1
Solution #2






2
Joe8:0014:00
14:0015:001
3
Sara15:0021:00
15:0016:002
4
Bob18:000:30
16:0017:002
5
Sam17:0023:30
17:0018:003
6
Chad14:0019:00
18:0019:004
7




19:0020:003

<tbody>
</tbody>

Array-enter (press ctrl+shift+Enter instead of just Enter) the following formula into G2, and copy down:

=SUM(IF($B$2:$B$6<=E2, IF($C$2:$C$6+($C$2:$C$6<$B$2:$B$6)>=F2, 1)))

Excel displays the array-entered formula with curly braces around it.

In this case, all times are entered as they appear. In particular, C4 is simply 0:30.

Note that this approach works only for shifts of less than 24 hours.
 
Last edited:
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
PS for solution #2....
Array-enter (press ctrl+shift+Enter instead of just Enter) the following formula into G2, and copy down:
=SUM(IF($B$2:$B$6<=E2, IF($C$2:$C$6+($C$2:$C$6<$B$2:$B$6)>=F2, 1)))

Better.... Normally-enter (just press Enter as usual) the following formula into G2, and copy down:

=SUMPRODUCT(($B$2:$B$6<=E3)*($C$2:$C$6+($C$2:$C$6<$B$2:$B$6)>=F3))

In this context, the multiplication (*) performs an AND operation.
 
Last edited:
Upvote 0
[QUOTE

When shifts start before midnight, the key is to enter after-midnight times as a formula, like the following in C4:

=1 + "0:30"

Note the double-quotes around the actual time. All time cells are formatted as Custom hh:mm.

.[/QUOTE]

The only issue that I have is making the user type in a formula. I'm trying to make it as computer illiterate as necessary.
 
Upvote 0
The only issue that I have is making the user type in a formula. I'm trying to make it as computer illiterate as necessary.

Understandable. I anticipated that. That's why I offered the SUMPRODUCT alternative as well (solution #2, modified by my "PS" follow-up). That does not require any special input.

PS: But hopefully the 1+"0:30" approach helps you understand what we are accomplishing with the SUMPRODUCT alternative. We are simply adding 1 (1 day; 24 hours) in the formula instead of in the data.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,179
Messages
6,129,329
Members
449,502
Latest member
TSH8125

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top