+ Reply to Thread
Results 1 to 17 of 17

VBA Hour Calculation Between 2 dates/times

  1. #1
    Registered User
    Join Date
    06-12-2013
    Location
    Poughkeepsie, NY
    MS-Off Ver
    Excel 2003
    Posts
    36

    VBA Hour Calculation Between 2 dates/times

    Hey guys,

    I would like to write a VBA macro sub that would take two columns that contain month/day/year and time and calculate the amount of hours that have passed between the two. I would like to exclude weekends and the hours that we are not open, so the only hours that would matter would be (M-F from 7 am to 8 pm).

    The columns that I would be using in this code looks like this example: (6/7/2013 5:50:00). This seems to be the biggest problem with the code I have written so far.

    So the real question is, would it be possible to determine the amount of hours that have passed when we exclude times closed and weekends when the date is in that format?

    This is what I have so far, but it gives me a run time error:
    Please Login or Register  to view this content.
    Thanks everyone, much obliged.

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: VBA Hour Calculation Between 2 dates/times

    vbaNewbieMan,

    Does this have to be VBA? I'm pretty sure you could do this with just a formula. And so that I'm understanding you right, given this start date/time and end date/time, is the result correct?


    Start Date/Time End Date/Time Total Hours
    6/7/2013 5:50:00 6/11/2013 15:50:00 34:50:00

    So the full 13 hours were worked on 6/7, which is a Friday, because the start time of 5:50 am is prior to the work hours start time of 7 am
    6/8 and 6/9 both count as 0 because that's a weekend
    The full 13 hours were worked on 6/10
    15:50 is 3:50 pm, so 8 hours and 50 minutes were counted for 6/11

    That leaves us with 13+0+0+13+8:50 = 34:50
    If that is correct, this formula should do the trick:
    Please Login or Register  to view this content.

    [EDIT]
    Alternate formula:
    Please Login or Register  to view this content.
    Last edited by tigeravatar; 06-12-2013 at 11:25 AM. Reason: Added edit
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Registered User
    Join Date
    06-12-2013
    Location
    Poughkeepsie, NY
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: VBA Hour Calculation Between 2 dates/times

    Unfortunately, that formula does not work. I have an open date of (6/3/13 16:28) and a close date of (6/3/16:40) and the return hours passed in the column comes out as (1/1/1900 15:12). The only part(s) that are correct are the days (1/1/1900) and the minutes of the time (:12).

    If it is at all possible, I would love it if the days could be dropped and turn into hours, so that (1/1/1900)=0 hours and (1/2/1900)=13 hours. I also have no idea where the hours of (15) came up in the (15:12).

    Thank you so much for your help tigeravatar.

    EDIT: Sorry, I forgot to mention that I used the first formula, as the second one does not seem to want to work.
    Last edited by vbaNewbieMan; 06-12-2013 at 12:00 PM.

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: VBA Hour Calculation Between 2 dates/times

    vbaNewbieMan,

    Alright, it's not the prettiest formula in the world, but this should do it:
    Please Login or Register  to view this content.
    Also, format the cell with a custom format of [h]:mm

  5. #5
    Registered User
    Join Date
    06-12-2013
    Location
    Poughkeepsie, NY
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: VBA Hour Calculation Between 2 dates/times

    Tigeravatar,

    I reformatted the cells, and that took care of the day problem, but unfortunately the new formula did not work as it came up with a #NAME? error, and the old formula is still giving me very strange responses [(6/6/13 18:11) to (6/6/13 19:20) yields (05:09)]. So, I went in to check to see if the input columns were in a weird format, but they were in the standard mm/dd/yyyy h:mm form.

    Once again, I really really appreciate all of your help and your time.

  6. #6
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: VBA Hour Calculation Between 2 dates/times

    At this point I'd have to see a sample file with expected results

  7. #7
    Registered User
    Join Date
    06-12-2013
    Location
    Poughkeepsie, NY
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: VBA Hour Calculation Between 2 dates/times

    Email Received ||| Date Entered ||| Time Elapsed

    6/7/2013 17:50 ||| 6/7/2013 18:22 ||| 0:32 <--------- what I would like

    6/7/2013 17:10 ||| 6/7/2013 18:08 ||| 1/1/1900 15:58 <--------- what I get, and sometimes it gets messier

    6/5/2013 10:12 ||| 6/7/2013 10:15 ||| 1/1/1900 2:03 <----------- Messy version where I don’t know how it got that.
    Last edited by vbaNewbieMan; 06-12-2013 at 01:26 PM.

  8. #8
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: VBA Hour Calculation Between 2 dates/times

    vbaNewbieMan,

    Attached is an example workbook using the sample data you provided in the PM you sent me. As far as I can tell, the formula is working properly and returning the correct number of hours for every example (which I verified by hand). You just need to change the formatting to Custom with a format of [h]:mm
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    06-12-2013
    Location
    Poughkeepsie, NY
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: VBA Hour Calculation Between 2 dates/times

    Tigeravatar,

    That is the weirdest thing I've ever seen. It won't work when I download the file. The total hours (formula) column just returns #NAME? for each entry. Are you using excel 2003 or 2007 for this? I am using 2003, and I saw under your avatar that you have both.

  10. #10
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: VBA Hour Calculation Between 2 dates/times

    I am using 2007 currently, but the compatibility checker didn't give me any problems...
    I don't have access to my 2003 copy of Excel at the moment. I'll see if I can get someone to look at it, and there's also probably a better formula that can used too

  11. #11
    Registered User
    Join Date
    06-12-2013
    Location
    Poughkeepsie, NY
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: VBA Hour Calculation Between 2 dates/times

    It appears as though the NETWORKDAYS function is the problem when I step through the entire formula. Any way around this?

  12. #12
    Registered User
    Join Date
    06-12-2013
    Location
    Poughkeepsie, NY
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: VBA Hour Calculation Between 2 dates/times

    Tigeravatar,

    That would be so awesome if you could get someone to look at it. It didn't even cross my mind that we might have been using different versions. Thanks once again.

  13. #13
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: VBA Hour Calculation Between 2 dates/times

    vbaNewbieMan,

    Here is a version that uses the Weekday() function instead of the Networkday() function.
    Formula in P2 and copied down:
    Please Login or Register  to view this content.
    Attached Files Attached Files

  14. #14
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: VBA Hour Calculation Between 2 dates/times

    in 2003 you need the analysis toolpak add-in installed to use NETWORKDAYS
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  15. #15
    Registered User
    Join Date
    06-12-2013
    Location
    Poughkeepsie, NY
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: VBA Hour Calculation Between 2 dates/times

    tigeravatar,

    HOLY CRAP YES. Thank you so much. I really really really appreciate this. I couldn't figure this out to save my life.

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

    Re: VBA Hour Calculation Between 2 dates/times

    Now that's praise worth a Batman visual.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.
    Thanks.
    _________________
    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!)

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

    Re: VBA Hour Calculation Between 2 dates/times

    If start and end times are always within the working hours then this formula should give you the correct hours using NETWORKDAYS

    =(NETWORKDAYS(E2,F2)-1)*("20:00"-"7:00")+MOD(F2,1)-MOD(E2,1)

    or if you want to avoid NETWORKDAYS function this will give the same result

    =(SUM(INT((WEEKDAY(E2-{2,3,4,5,6})+INT(F2)-INT(E2))/7))-1)*("20:00"-"7:00")+MOD(F2,1)-MOD(E2,1)

    Either way format result cell as [h]:mm
    Audere est facere

+ 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