+ Reply to Thread
Results 1 to 4 of 4

time duration

Hybrid View

scott micklo time duration 02-12-2018, 12:35 PM
6StringJazzer Re: time duration 02-12-2018, 01:02 PM
scott micklo Re: time duration 02-12-2018, 01:09 PM
xladept Re: time duration 02-12-2018, 01:28 PM
  1. #1
    Forum Contributor
    Join Date
    07-08-2015
    Location
    Indiana
    MS-Off Ver
    Excel 2007
    Posts
    128

    time duration

    Hi Experts

    I have a simple code that computes time duration between 2 times in minutes. For example, 10:00 am to 2:00 pm is 240 minutes. And 6:15 pm to 9:15 pm is 180 minutes.

    BUT, when I attempt to roll over to the next day ie 10:00 pm to 2:00 am, I keep getting -1200 minutes.

    I searched but can not find a quick code that will compute this correctly to 240 minutes.


    Here is my code so far:
    Private Sub CommandButton1_Click()
    Dim lr As Integer
    Dim dur As Long
    
    
    lr = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
    
    For i = 1 To lr
    
    dur = DateDiff("s", Cells(i, 1), Cells(i, 2))
    
    Cells(i, 4).Value = dur / 60
    
    
    
    
    Next
    
    End Sub

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,977

    Re: time duration

    You have to explicitly calculate the case where it crosses midnight. Here is one way to do it.

    BTW you can directly get minutes with "n" instead of getting seconds and dividing by 60.

    Private Sub CommandButton1_Click()
    
       Dim lr As Integer
       Dim dur As Long
       Dim i As Long
       
       lr = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
       
       For i = 1 To lr
       
          If Cells(i, 1) <= Cells(i, 2) Then
             dur = DateDiff("n", Cells(i, 1), Cells(i, 2))
          Else
             dur = DateDiff("n", Cells(i, 1), 1) + DateDiff("n", 0, Cells(i, 2))
          End If
          Cells(i, 4).Value = dur
       
       Next i
    
    End Sub
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor
    Join Date
    07-08-2015
    Location
    Indiana
    MS-Off Ver
    Excel 2007
    Posts
    128

    Re: time duration

    Very elegant. Just to ensure I am reading it correctly, you take the "end of the night" and add it to the "begin" of the morning.

    Thank you very much

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: time duration

    Or, if using minutes:

    Cells(i, 4).Value = IIf(dur < 0, 1440 + dur, dur)
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. time formats and time duration formula
    By kristinGersh in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 01-30-2018, 03:17 PM
  2. Replies: 5
    Last Post: 09-29-2016, 09:58 PM
  3. Replies: 2
    Last Post: 07-14-2015, 02:12 PM
  4. Displaying data as time duration rather than time of day
    By BrookeA in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-13-2014, 06:48 PM
  5. [SOLVED] How to create a real time report with on/off time and duration?
    By Why123 in forum Excel General
    Replies: 7
    Last Post: 11-21-2013, 02:01 AM
  6. [SOLVED] Split Time Duration to first complete the running hour and then go to End time
    By joogibabu in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-06-2013, 10:56 PM
  7. Calculate finish time given start time, working hours and job duration
    By swanseaexcel in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-23-2012, 03:00 PM

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