+ Reply to Thread
Results 1 to 14 of 14

Counting Only Specific Hours Between Two Times

Hybrid View

  1. #1
    Registered User
    Join Date
    09-08-2011
    Location
    Bergen, Norway
    MS-Off Ver
    Excel 2003
    Posts
    9

    Counting Only Specific Hours Between Two Times

    What i need help with here is to find out how many hours (if any) are "Night hours" and/or "Weekend hours".
    "Night" hours are from 21:00 to 06:00 any day.
    "Weekend" hours are from Saturday 18:00 to Monday 06:00
    It seems that the Total Hours part is working ok, i hope
    But i just cant seem to find out how to get the Night and Weekend part to work
    Iv looked high and low everywhere to try to find a salution to this, but just cant seem to find one
    Iv attached a excel file incase that helps to understand what im looking for.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor khamilton's Avatar
    Join Date
    10-08-2009
    Location
    IL
    MS-Off Ver
    Excel 2007
    Posts
    345

    Re: Counting Only Specific Hours Between Two Times

    didn't try it in 2003 but give it a shot. Pondus-Night&Weekend_Time_Calculation.xls
    Please acknowledge the response you receive, good or bad. If your problem is solved, please say so clearly, and mark your thread as Solved: Click the Edit button on your first post in the thread, Click Go Advanced, select [SOLVED] from the Prefix dropdown, then click Save Changes. If more than two days have elapsed, the Edit button will not appear -- ask a moderator to mark it.

  3. #3
    Registered User
    Join Date
    09-08-2011
    Location
    Bergen, Norway
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Counting Only Specific Hours Between Two Times

    wow, that was just what i was looking for, thanks alot
    iv tested it alittle now, and from what i can see, it had a few small "errors"
    it looks like the "Night" calculations are working great so far. the only thing iv noticed there is that if "Start" and Finish" are left empty, it shows 6 hours in the "Night" section.
    it would be great if it is left empty, or as 0 so i can add a automatic sum for amount of "Night" hours to it when its done.
    on the "Weekend" part, it seemed to work on not counting monday to saturday 18:00, but when on saturday, no matter what time i enter as start time, it counts how many hours from 18:00 to "Finish" time. (if i enter 20:00 - 21:00, it still counts from 18:00 - 21:00)
    and if "Start" and "Finish" are empty, "Weekend" shows #'s, but this is only on the line with Saturday.
    also, if "Finished" does not have a time of 18:00 or higher, it also shows #'s on "Weekend" for Saturday.
    on sunday, it looks like the "Weekend" does not end at 06:00 monday, it just keeps counting all the hours.

    i have tried to do alittle fixing on this myself, but it seems it is alittle over my head atm to do by myself

  4. #4
    Registered User
    Join Date
    09-08-2011
    Location
    Bergen, Norway
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Counting Only Specific Hours Between Two Times

    oh, and i was thinking, it isnt really needed to check if the day is saturday or sunday, i was thinking i could just add the "Weekend" code to where there is a weekend.
    that might make it alittle easier i hope

  5. #5
    Valued Forum Contributor khamilton's Avatar
    Join Date
    10-08-2009
    Location
    IL
    MS-Off Ver
    Excel 2007
    Posts
    345

    Re: Counting Only Specific Hours Between Two Times

    I will try tinkering around with it some time this weekend after work, so probably sunday at some point.

  6. #6
    Registered User
    Join Date
    09-08-2011
    Location
    Bergen, Norway
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Counting Only Specific Hours Between Two Times

    cool, thank you very much

  7. #7
    Valued Forum Contributor khamilton's Avatar
    Join Date
    10-08-2009
    Location
    IL
    MS-Off Ver
    Excel 2007
    Posts
    345

    Re: Counting Only Specific Hours Between Two Times

    Okay here you go, i'm sure there must be a better way but it was the best i could come up with.


    Pondus-Night&Weekend_Time_Calculation.xls

  8. #8
    Registered User
    Join Date
    09-08-2011
    Location
    Bergen, Norway
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Counting Only Specific Hours Between Two Times

    thanks alot, again
    iv hade a quick look at it now, it seems there are a few errors still
    i dont want to bother you and take up more of your time, so ill see if i can understand how the code works and play around with it myself
    thank you again very much, you have helped me out alot getting to what i wanted. 2 thumbs up

  9. #9
    Registered User
    Join Date
    09-08-2011
    Location
    Bergen, Norway
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Counting Only Specific Hours Between Two Times

    i thought id reuse this thread insted of starting a new one...
    iv tried to play around with these codes, but i just cant seem to understand them or get them to work 100% the way i want
    like i posted at the start of the thread, i need to be able to count specific hours on specific days, between specific times, if any.
    i need to count the hours, if any, every day between 21:00 and 06:00 in one cell
    and
    i need to count the hours, if any, between saturday 18:00 and monday 06:00 in another cell

    this is what i got so far:

    column A is the day/date, formated dddd dd.mm.مممم (i have a norweigian windows Win XP Home SP3, and a english MS Office 2003, مممم=yyyy)
    column B is the start time, formated tt:mm
    column C is the end time, formated tt:mm
    column D is the total hours between B and C, with the code: =IF(C3="";"";IF(B3>C3;C3+1-B3;C3-B3))
    this seems to be working like i want it

    column E is the total hours, if any, between 21:00 and 06:00, with the code: =IF(C3="";"";IF(C3<B3;MIN(C3;1/4)+(1-MAX(B3;7/8));IF(B3<1/4;MIN(1/4;C3)-B3;0)+IF(C3>7/8;C3-7/8;0)))
    this seems to be working like i want it

    now this is where i am having problems to get it to work 100%
    column F is the total hours, if any, between saturday 18:00 til monday 06:00, with the code: =IF(C3="";"";IF(WEEKDAY(A3)=2;IF(C3<B3;MIN(C3;1/4)+(1-MAX(B3;7/8));IF(B3<1/4;MIN(1/4;C3)-B3;0)+IF(C3>7/8;C3-7/8;0));IF(WEEKDAY(A3)=1;IF(B3>C3;C3+1-B3;C3-B3);IF(WEEKDAY(A3)=7;IF(C3<B3;MIN(C3;1/4)+(1-MAX(B3;3/4));IF(B3<1/4;MIN(1/4;C3)-B3;0)+IF(C3>3/4;C3-3/4;0))))))

    it seems that it does not count the hours right on monday and saturday atleast.
    the start and end times, can be just about anything, so it has to work for all times, start and end.

    if anyone has any salution to this it would be great, and if anyone could explain the codes in detail, that would also be great, id like to know how everything works, the help file didnt help me much

  10. #10
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,697

    Re: Counting Only Specific Hours Between Two Times

    Try this formula for hours between 21:00 and 06:00

    =IF(C3="";"";MAX(0;C3-B3+(C3<B3)*3/8-MEDIAN(C3;7/8;1/4)+MEDIAN(B3;7/8;1/4)))

    and this one for hours between 18:00 Saturday and 06:00 Monday

    =IF(C3="";"";MEDIAN(0;2.5-MOD(B3+1/4;1)-WEEKDAY(A3+B3+1/4);MOD(C3-B3;1)))

    In both cases format result cell as t:mm

    I tested these extensively, both appear to work as required, please test.

    Note: 2nd formula now edited.....
    Audere est facere

  11. #11
    Registered User
    Join Date
    09-08-2011
    Location
    Bergen, Norway
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Counting Only Specific Hours Between Two Times

    thanks for the reply
    i tried the second code, both the first one, and the edited one, but i got an error on both as soon as i pressed enter after pasting the code in the cell.
    after i pressed enter, and clicked ok on the error msg, it marked 2.5 automaticly, dont know if that ment that that is where it had a problem, but that seemed locigal atleast it marked 2.5 at the end of this:
    =IF(C3="";"";MEDIAN(0;2.5-

    and the formula for hours between 21:00 and 06:00 that i hade, seemed to be working, so i havent tested the one you posted. did you see/find any errors in the one i had?

  12. #12
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,697

    Re: Counting Only Specific Hours Between Two Times

    OK, I think it's the notation, I didn't convert correctly for your region, for your version 2.5 should be 2,5.....or better to stick to fractions as I have those elsewhere, i.e.


    =IF(C3="";"";MEDIAN(0;5/2-MOD(B3+1/4;1)-WEEKDAY(A3+B3+1/4);MOD(C3-B3;1)))


    see attached file with random dates and times that I used for testing. Press F9 to generate different dates and times


    The 21:00 to 06:00 formula I suggested will pick up all times in that period, even at both ends of the day, e.g. if the shift is 05:00 to 01:00 (next day) it will give a result of 5:00 (1 hour at the start of the day and 4 at the end). You may not have any shifts like that......
    Attached Files Attached Files
    Last edited by daddylonglegs; 12-19-2011 at 09:44 PM.

  13. #13
    Registered User
    Join Date
    09-08-2011
    Location
    Bergen, Norway
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Counting Only Specific Hours Between Two Times

    thanks again for another test
    i looked at your file, and from what i saw, i still got errors here. dont know if it is something with my version of windows or office, or just the language of both. im still getting errors on my file, it seems that the options i need are very hard to cover 100%
    ill add i file to show what im working with, maybe that can help,
    also got a new error, saying that "Negative dates or times are displayed as ########"
    we work on every possible times, so we can start and finish at any time, with any time length, anything from 1 hour jobs to 16 or more hour jobs, even going from 1 day to the next. dont need to be able to calculate over 24 hours though
    Attached Files Attached Files

  14. #14
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,697

    Re: Counting Only Specific Hours Between Two Times

    In your workbook you have "Transition formula evaluation" set which was messing up the formulas. I changed to it like this


    Tools > Options > Transition > untick "transition formula evaluation"


    see attached where I corrected that
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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