+ Reply to Thread
Results 1 to 18 of 18

Count hours (time) between four time criteria

  1. #1
    Registered User
    Join Date
    08-24-2009
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    36

    Count hours (time) between four time criteria

    I am struggling to find a formula to calculate hour:minutes between the following:

    Cell
    A1= 04:00
    B2= 17:00

    I am wanting to calculate the hours between 06:00 and 16:00 and then the hours between 16:00 and 06:00 the next day

    ie: 06:00-16:00 = 12;
    16:00 - 0600 = 3

    Is this possible?
    Last edited by adhide; 08-25-2009 at 11:51 PM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Count hours (time) between four time criteria

    A1 = 4:00
    B1 = 17:00

    C1 (Night hours) =(((TIMEVALUE("6:00")-$A1)*(TIMEVALUE("6:00")>$A1))+(($B1-TIMEVALUE("16:00"))*($B1>TIMEVALUE("16:00"))))*24

    D1 (Day hours) =(B1-A1)*24-C1
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    08-24-2009
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: Count hours (time) between four time criteria

    I attempted to understand the formula, but I aint that good if statements are my limits haha

    however, using that i get a return value of c1=72 and d1=10

    A1=4:00
    B1=17:00

    C1 should = 3
    D1 should = 12

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Count hours (time) between four time criteria

    Quote Originally Posted by adhide View Post
    I attempted to understand the formula, but I aint that good if statements are my limits haha

    however, using that i get a return value of c1=72 and d1=10

    A1=4:00
    B1=17:00

    C1 should = 3
    D1 should = 12
    Check your cut/n/paste, because the formula is correct. When I use it, C1=3 and D1=10.

    You're off in your mind about the daytime hours. 6am-4pm is only 10 hours.
    Attached Files Attached Files
    Last edited by JBeaucaire; 08-24-2009 at 10:54 PM. Reason: Added sample book

  5. #5
    Registered User
    Join Date
    08-24-2009
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: Count hours (time) between four time criteria

    thankyou

    I cleared the cell, must have had something formatter there .

    Now for a twist,

    How can I condition that to count over the 24 hour period:

    ie

    A1=05:00 (Friday morning start)
    B1=02:00 (Saturday morning finish)
    C1= 10
    D1= 11

    Formatting the cell C1 to [h]:mm gives me 48

    or vise versa

    A1=14:00
    B1 = 02:00
    C1 = 2
    D1 = 10
    Last edited by adhide; 08-24-2009 at 11:05 PM.

  6. #6
    Registered User
    Join Date
    08-24-2009
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: Count hours (time) between four time criteria

    I assume then this cannot be achieved!

  7. #7
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Count hours (time) between four time criteria

    I would probably opt for

    Please Login or Register  to view this content.
    The above in both instances are time values (ie 10:00, 11:00) - if you want as integers multiply both of the above by 24.

  8. #8
    Registered User
    Join Date
    08-24-2009
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: Count hours (time) between four time criteria

    so these formula are the same but different

    to get decimal hours you commented on *24

    i get ######### when i do that, sorry, im new at this stuff, so i apologise in advance.

  9. #9
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Count hours (time) between four time criteria

    To get the time values as integer - ie 10:30 as 10.5 then * 24, so:

    Please Login or Register  to view this content.
    Time in XL is decimal - ie 24 hours = 1, 12 hours = 0.5 etc ... thus multiplying decimal time value by 24 converts to hour based integer
    (multiplying by 1440 would give minutes as integer (24*60), by 86400 would give seconds (24*60*60))

    If you use the above be sure to set format of cell to be General / Number etc and not Time

  10. #10
    Registered User
    Join Date
    08-24-2009
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: Count hours (time) between four time criteria

    Boy oh boy, "alot to learn you have young one, alot to learn", is what yoda would be saying to me right now!

  11. #11
    Registered User
    Join Date
    08-24-2009
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: Count hours (time) between four time criteria

    Now finally, i hope,

    If there are no hours in the fields, the formula locations are reporting a '-6'

    why would that be, and how to i get rid of the little thing.

  12. #12
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Count hours (time) between four time criteria

    You're getting -6 because the calculation when both values are blank is essentially

    Please Login or Register  to view this content.
    To account for this add a COUNT check

    Please Login or Register  to view this content.

  13. #13
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Count hours (time) between four time criteria

    I don't think Donkeyote's formula will work for all scenarios, e.g. try 20:00 to 06:00.

    This formula should cope with any shift less than 24 hours long

    for hours between 06:00 and 16:00

    =((A1>B1)*MEDIAN(0,B1-1/4,5/12)+MAX(0,MIN(2/3,B1+(A1>B1))-MAX(1/4,A1)))*24

    for all other hours

    =24*MOD(B1-A1,1)-C1

    Note: in the first formula 1/4 represents 06:00, 2/3 represents 16:00 and 5/12 is the difference between those 2 times (10 hours)

  14. #14
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Count hours (time) between four time criteria

    Quote Originally Posted by daddylonglegs View Post
    I don't think Donkeyote's formula will work for all scenarios...
    ...now there's a shocker...

  15. #15
    Registered User
    Join Date
    08-24-2009
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: Count hours (time) between four time criteria

    Well, you have now all successfully stumped me (confused me).

    Can someone explain this one to me?

  16. #16
    Registered User
    Join Date
    08-24-2009
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: Count hours (time) between four time criteria

    The other issue is that I will eventually need to have the time (either 6:00 or 16:00 vary depending on user input, so all of the fractions would appear not to cope at all with that situation.

    DonkeyOte... What if there were three time interval variables, like for shift work.

    THat is to say, hours between say 07:00 to 15:00, 15:00 to 23:00, and 23:00 to 07:00, is it a case of adding another min/max?

  17. #17
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Count hours (time) between four time criteria

    Quote Originally Posted by adhide View Post
    Well, you have now all successfully stumped me (confused me).

    Can someone explain this one to me?
    Hello adhide

    My suggestion is a little more complex than Donkeyote's, but I believe it will give the correct results in all circumstances.

    You might be able to use a less complex formula but that would depend on the possible start and end times you have. My suggestion will cope with all possible start and end times, assuming shifts are always less than 24 hours. If your shifts are more restricted then please can you define the limitations, I note you suggested that one shift might be 05:00 to 02:00, i.e. 21 hours?

    If you want to make the formula more flexible then you can use cell references for the start and end times, e.g. if J2 contains the start time of the range to count, e.g. 06:00 and K2 the end time, i.e. 16:00 then the formula becomes

    =((A2>B2)*MEDIAN(0,B2-J$2,K$2-J$2)+MAX(0,MIN(K$2,B2+(A2>B2))-MAX(J$2,A2)))*24

    Now you can change the values in J2 and K2 without changing the formula itself......

    Note: K2 must be greater than J2

    See attached examples, I also included a sheet where hours are calculated between 07:00 and 15:00, 15:00 and 23:00 and 23:00 t0 07:00. Again you can just change these time bands by changing the values in J2:K3. Again the start time must be earlier than the end time (the undefined range must always be the range that passes through midnight)

    If A2 and B2 are blank the formulas return zero

    I used =RAND() formula to generate random times either on the hour or half hour, just for demonstration purposes. Press F9 to make times re-calculate or enter your own times.....
    Attached Files Attached Files

  18. #18
    Registered User
    Join Date
    08-24-2009
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    36

    Thumbs up Re: Count hours (time) between four time criteria

    I dont know how you guys do it, but I next stage is to analys this, thank you so very much.
    Last edited by adhide; 08-25-2009 at 07:47 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1