+ Reply to Thread
Results 1 to 5 of 5

Calculate Start and End Time Stored as HH:MM when Shift extend past 00:00 (midnight)?

  1. #1
    Forum Contributor onmyway's Avatar
    Join Date
    09-03-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    2013
    Posts
    386

    Calculate Start and End Time Stored as HH:MM when Shift extend past 00:00 (midnight)?

    Hi all,

    I hope you can help me.

    I have a simple calculation error due to, what I believe, is a format issue.

    Detail:

    i have two fields used for capturing a start and end time - txtWorkShiftStart and txtWorkShiftEnd. The format of these two fields are HH:MM. To calculate the total shift time, one would subtract the two values (End - Start).

    Here is the code i use for the calculation:

    Please Login or Register  to view this content.
    This seems to work fine, until you have a shift starting before 00:00 (midnight) and end after 00:00 (midnight).

    Example:

    04:00 - 20:00 = 16:00 WRONG REMEMBER: it is a shift that started at 20:00 and ended 04:00 the next morning

    It should be :

    04:00 - 20:00 = 08:00 CORRECT REMEMBER: it is a shift that started at 20:00 and ended 04:00 the next morning

    i thank you in advance!
    Last edited by onmyway; 03-31-2015 at 02:30 PM.
    Sharing knowledge, can be likened to taking another person's hand, and pulling them up to a higher level -- onmyway

    If I was helpful, please remember to click on * Add Reputation below

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,962

    Re: Calculate Start and End Time Stored as HH:MM when Shift extend past 00:00 (midnight)?

    ws.Cells(rowToEdit, 138).Value = Format(CDate(Me.txtWorkShiftEnd.Value) - CDate(Me.txtWorkShiftStart.Value) + IIf(CDate(Me.txtWorkShiftEnd.Value) < CDate(Me.txtWorkShiftStart.Value), 1, 0), "HH:MM")

    should work, as long as they are time strings and don't include the date.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Calculate Start and End Time Stored as HH:MM when Shift extend past 00:00 (midnight)?

    Hi onmyway,

    Try adding one day, when the end time occurs before the start time.
    Please Login or Register  to view this content.
    Lewis

  4. #4
    Forum Contributor onmyway's Avatar
    Join Date
    09-03-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    2013
    Posts
    386

    Re: Calculate Start and End Time Stored as HH:MM when Shift extend past 00:00 (midnight)?

    Quote Originally Posted by Bernie Deitrick View Post
    ws.Cells(rowToEdit, 138).Value = Format(CDate(Me.txtWorkShiftEnd.Value) - CDate(Me.txtWorkShiftStart.Value) + IIf(CDate(Me.txtWorkShiftEnd.Value) < CDate(Me.txtWorkShiftStart.Value), 1, 0), "HH:MM")

    should work, as long as they are time strings and don't include the date.
    Hi Bernie,

    Thank you, works like a charm!

  5. #5
    Forum Contributor onmyway's Avatar
    Join Date
    09-03-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    2013
    Posts
    386

    Re: Calculate Start and End Time Stored as HH:MM when Shift extend past 00:00 (midnight)?

    Quote Originally Posted by LJMetzger View Post
    Hi onmyway,

    Try adding one day, when the end time occurs before the start time.
    Please Login or Register  to view this content.
    Lewis
    Hi Lewis,

    Thanks for helping me once again. You really know your stuff!

    In essence, both your solution and Bernie's make use of the same methodology. I decided to go with Bernie's, as it was simpler.

+ 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. [SOLVED] Text Time to Excel Format, Then Sort Time Past Midnight
    By BuntyMac in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-27-2014, 03:13 AM
  2. [SOLVED] Calculating shift totals and breaks past midnight
    By mazlou in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-16-2013, 12:49 AM
  3. [SOLVED] Current formula not able to calculate past midnight.
    By impresso in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-16-2013, 05:31 PM
  4. Replies: 4
    Last Post: 01-11-2012, 07:59 PM
  5. Calculate time which is past midnight
    By stoey in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 08-25-2011, 12:11 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