Differentiate betwen Ordinary, Night, SAT, SUN shifts

laurentdes

New member
Joined
Nov 3, 2016
Messages
17
Reaction score
0
Points
0
Hi Team

Can someone assist me

I need to differentiate the type o shifts according to time in and time out
Ord = Mon to Fri 0600 to 1800
Night = Mon to Fri 1800 to 0600
SAT whole day
SUN whole day

I have attached an example with the results

can someone please assist me with getting either a excel formulae or VBA codes to compute the results
 

Attachments

  • test1.xlsx
    8.6 KB · Views: 18
Calculate ordinary and night working hours for different weekday

Can someone assist me
I have tried to do something. I do not have time, so you finished the rest by idea.
See the idea in the attachment file. You can try to combine some of the other functions.
 

Attachments

  • laurentdes-navic6943.xlsx
    11.1 KB · Views: 17
This is not an easy problem by any means and there have been several solutions offered on the web that are long and convoluted.
After trying with formulae for a while I switched to a user-defined-function routine (although I haven't given up entirely on producing a relatively easy-to-manage formula solution).
In your sheet the formula would be:
=ShiftSplit(A3,B3,C3)
entered into cells D3:G3 all at once with Crtl + Shift + Enter (not just Enter) - this is array-entering a formula, or CSE.
Then you can drag the formula down as far as you need.
The formula produces results in 4 cells in the order: ORD NIGHT SAT SUN
In the attached this has been done in the range D12:G18

This formula needs to be supported by the user defined function:
Code:
Function ShiftSplit(sd, st, et)
Dim z(0 To 3)
If Not (IsEmpty(sd) Or IsEmpty(st) Or IsEmpty(et)) Then
  ord = [{2.25,2.75;3.25,3.75;4.25,4.75;5.25,5.75;6.25,6.75}]
  night = [{2,2.25;2.75,3.25;3.75,4.25;4.75,5.25;5.75,6.25;6.75,7}]
  sat = [{0,1;7,8}]
  sun = [{1,2;8,9}]
  AllShifts = Array(ord, night, sat, sun)

  sdt = Evaluate("mod(" & CDbl(sd + st) & ",7)")
  edt = Evaluate("mod(" & CDbl(sd + et - (st >= et)) & ",7)")
  If edt < sdt Then edt = edt + 7

  For j = LBound(AllShifts) To UBound(AllShifts)
    For i = LBound(AllShifts(j)) To UBound(AllShifts(j))
      temp = Application.Max(0, Application.Min(AllShifts(j)(i, 2), edt) - Application.Max(AllShifts(j)(i, 1), sdt))
      z(j) = z(j) + temp
    Next i
  Next j
End If
For i = LBound(z) To UBound(z)
  If z(i) = 0 Then z(i) = ""
Next i
ShiftSplit = z
End Function
For your info:
sd=start date from column A
st=start time (column B)
et=end time (column C)

within the body of code:
sdt = start date and time
edt = end date and time

The workings are based on looking for overlapping blocks of time; if you have two blocks of time the overlap can be determined with:
max(0,min(end times)-max(start times))
the smaller of the end times of the blocks, minus the larger of the start times of the blocks, then taking whichever is larger: the result of the above or 0.

Block 1: 3am to 11am
Block 2: 6am to 9am

translates to:
max(0,min(11,9)-max(3,6))
which condenses to:
max(0,9-6)
to:
max(0,3)
=3


This works regardless how the 2 blocks of time overlap (or not).

The numbers in (for example) the array night ({2,2.25;2.75,3.25;3.75,4.25;4.75,5.25;5.75,6.25;6.75,7}) are in pairs where, for example 2.75 to 3.25 represents three quarters into day 2 as the start, and one quarter into day 3 as the end. Day 2 is Monday 2nd Jan 1900, so 2.75 represents Monday 2nd Jan 1900 at 6pm. The pairs in night represent all the blocks of time in one week that are night shifts. All dates passed as arguments into the function are brought down to days 0 to 7 by using the MOD function.

You don't have to enter the formula as an array formula into 4 cells at once; you can get individual results by normally-entering a formula such as:
=INDEX(ShiftSplit(A21,B21,C21),2)
to get the second value (night hours) all by itself. This has been demonstrated in the attached at cells D21:G27.

The function returns values as days and fractions of days, so you need to format the output as you wish to see them. In the attached I've used [h] as the format throughout although you could use [h]:mm if you wanted to see more information.

I've only given it reasonable testing - I'll leave thorough testing to you.

ps. the 2 lines:
Code:
temp = Application.Max(0, Application.Min(AllShifts(j)(i, 2), edt) - Application.Max(AllShifts(j)(i, 1), sdt))
z(j) = z(j) + temp
can be condensed to:
Code:
z(j) = z(j) + Application.Max(0, Application.Min(AllShifts(j)(i, 2), edt) - Application.Max(AllShifts(j)(i, 1), sdt))
 

Attachments

  • excelguru6943test1.xlsm
    29 KB · Views: 25
Last edited:
This is not an easy problem by any means and there have been several solutions offered on the web that are long and convoluted.
After trying with formulae for a while I switched to a user-defined-function routine (although I haven't given up entirely on producing a relatively easy-to-manage formula solution).
Hi p45cal
This your solution seems to well, but to me returns an error #VALUE!
Maybe it's because I'm using the Croatian standard?

OP uses a 24-hours time
I have noticed that you are rewrote ORD/NIGHT columns for date "SUN, November 6, 2016".
I think that is the result of 3, belongs to the column ORD (thus written by OP).
In the original file to a specific date in the following shall apply: 17:00 to 3:00 (which is equivalent to: from 5:00 PM to 3:00 AM)
Sunday begins at 0:01 AM after midnight and ends at 11:59 PM ie. Midnight (equivalent from 00:01 to 24:00).
The remainder of 0:01 AM to 3:00 AM belongs to Monday (Because it's the next day).
So, 17:00-24:00 is Sunday rest 00:00-03:00 is Monday
Am I in the right?

I have a different complete solution, using formulas. Maybe I did not take into consideration all combinations of "start-end time" to check all the possible options, but let's wait what will tell the OP.
Excuse the bad English, English is not my native language, so I might not understand well your explanation.
My regards
 

Attachments

  • laurentdes-navic6943-2.xlsx
    12.8 KB · Views: 16
This is how I interpret his hours:
2016-11-05_184212.jpg

rest 00:00-03:00 is Monday
Which I think are NIGHT hours since the OP says ORD start at 0600.
 
Last edited:
Hi P45Cal
Fantastic, it works perfectly . i did some random testing and by the look of it , its what i want.
i did check Friday night implications where someone starts at 16:00 and finishes on Sat morning at 3:00 am, which means that there are 3 categories, that is, 1600 to 1800 = 2 hr ordinary, 180 to 0000 6 hr night shift and 00 00 t0 300, 3 hrs sat shift. and it worked perfectly. i also did the same check for sat and sun where sun goes over to monday morning, and again it was perfect .

INOUTORDNIGHTSATSUN
Monday, October 31, 201622:004:00 6
Tuesday, November 01, 20163:008:0023
Wednesday, November 02, 20165:0011:0051
Thursday, November 03, 2016
Friday, November 04, 201617:005:00165
Saturday, November 05, 201615:003:00 93
Sunday, November 06, 201618:008:0026 6






I will try to run it on a bunch of data and will see how it goes. Thank you again.
Navic, yours also sounds very good. I was on my way to work on it .

:)
 
Hi Navic, the 3 hours belongs to Monday but considered as Night shift. May be i did not explain it properly. Apologies for that. It sounds very confusing and this has given me head ache trying to understand all these. Thanks again for your assistance , highly appreciated
 
the 3 hours belongs to Monday but considered as Night shift.
For this case, you wrote the expected result of 3 hours in the ORD column.
Never mind, it is important that you are on a positive way solutions to your problem.
 
This is how I interpret his hours:
OK, To you for the information
When I open your file in Excel 2007, it's all OK
When I open your file in Excel 2013, I have a problem. Before I click on "Enable editing' button I see the results, but after clicking the button appears error in all cells? :(
 
but after clicking the button appears error in all cells? :(
navic, I can only speak from using Excel 2010. When I open a foreign file with vba in I get asked first to Enable editing, then I separately get asked to enable macros (active content, or some such, which includes enabling macros).
Now comes the inevitable question, what errors appear in all the cells? Is it ?Name ?
I may have to ask you to do some detective work for me in Excel 2013, such as putting a STOP instruction at the beginning of the code (a) to check whether the function is being called at all and (b) if it is, to step through the code until it silently stops (as it does with functions) and tell me where it stopped.

Thanks.
 

Attachments

  • before-click-on-enableEditing.png
    before-click-on-enableEditing.png
    50.8 KB · Views: 11
  • after-click-on-enableEditing.png
    after-click-on-enableEditing.png
    58.8 KB · Views: 13
Hi @p45cal
I do not know what is happening with the forum. I can not write a personal message.
Did you get my message yesterday?
Here's a test to Excel 2016
 

Attachments

  • XL2016.png
    XL2016.png
    62.2 KB · Views: 9
post posting last message: Nope, get same '503 Service unavailable, the server is temporarily busy, try again later'
(I'm trying to get you my private email address)

Can't even edit my last message (message too short apparently!) and even having difficulty sending this one! Aaaagh!
 
Hi
Lets say i have a tab with Public holiday dates, i want to know how many hours of the ord, night , sat and sun falling on Public Holiday dates

is that something easy to do
 
is that something easy to do
Probably not.
You will need to supply a file with your new layout and new public holiday sheet/tab.
By the way, public holidays always start and finish at midnight?
 
Back
Top