+ Reply to Thread
Results 1 to 6 of 6

Calculate Total Time when Dates/Times Overlap

  1. #1
    Registered User
    Join Date
    02-16-2008
    Posts
    10

    Calculate Total Time when Dates/Times Overlap

    I have four columns: StartDate, StartTime, EndDate, and EndTime. This represents the timed duration of several washing machines used in a continuing fashion. I want to calculate the total time the washers are in use so I can figure out the total time they were not in use. This eludes me because some of the dates/times overlap. Can someone help with me calculate to total in use time?

    ColumnA ColumnB ColumnC ColumnD
    StartDate EndDate EndDate EndTime
    1/21/2008 10:00 1/22/2008 9:30
    1/21/2008 10:20 1/22/2008 10:30
    1/23/2008 17:00 1/25/2008 9:30
    1/23/2008 17:05 1/28/2008 9:15
    1/24/2008 12:00 1/24/2008 13:00
    1/25/2008 9:29 1/25/2008 11:52
    1/25/2008 15:00 1/29/2008 6:30
    1/25/2008 15:15 1/25/2008 16:30
    1/30/2008 12:15 2/1/2008 16:45
    1/31/2008 16:15 1/31/2008 17:30
    2/4/2008 14:30 2/5/2008 12:45
    2/4/2008 14:30 2/4/2008 15:45
    2/5/2008 12:30 2/7/2008 13:15
    2/8/2008 12:15 2/8/2008 12:50
    2/11/2008 8:00 2/11/2008 13:10
    2/11/2008 13:30 2/11/2008 14:30

  2. #2
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650
    Hi,You could try this:-
    The code will return the time differences in (Hrs) in column "E"
    I note that you date format is mm/dd/yyyy which is not the country setting for me. (me = dd/mm/yyyy)
    I found it returned odd results for dates such as (1/1/2008), because of this, you will have to see.
    It should be allright if your dates are in your natural settings.
    Give it a try
    Please Login or Register  to view this content.
    Regards Mick
    Last edited by MickG; 02-17-2008 at 11:43 AM.

  3. #3
    Registered User
    Join Date
    02-16-2008
    Posts
    10
    Thanks Mick. I looked at the code. Not sure what I do with it (pasted into VB editor and stood back but nothing happened). I might not have made my problem clear. Let me break it down over a couple of rows:

    1/21/2008 10:00 1/22/2008 9:30.......Washer ran for 23 hours and 30 minutes

    1/21/2008 10:20 1/22/2008 10:30......Since the washer above was going when this one started (some time is overlapping), this just extends the time by one hour for a total of 1 day and 30 minutes (23:30 + 1:00). The incorrect answer would be to add the row 2’s running time (23:30) and row 3’s running time (24:10) which would be 1 day 23 hours and 40 minutes.

    1/23/2008 17:00 1/25/2008 9:30.....No overlap exists so now this washer was running for 1 day, 16 hours and 30 minutes. Add that to the 24 hours and 30 minutes for a total of 2 days and 17 hours

    1/23/2008 17:05 1/28/2008 9:15....Now there is more overlapping time so you add time from the stop date/time of the washer before (1/25/2008 9:30) and stop at the end of this washer's cycle adding 2 days, 23 hours and 45 minutes of running time. The total time now is 5 days 16 hours and 45 minutes(2 days and 17 hours) + (2 days, 23 hours and 45 minutes) .

    Does this make it clearer and reveal the complexity?

  4. #4
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650
    Mr Excel_Steve Gilley _Calculate Total Time when Dates/Times Overlap
    ----
    Hi, Steve
    After reading you reply I have enclosed a new bit of code. On the end of which you will find some information to get it working.
    I have tried this bit of code with the dates & times you sent me and our answers seem to tally (136.75 Hrs).
    I suggest you test it by inserting the 4 rows of dates & times(as per your reply) in a new Worksheet starting in row (2). Column ("A")
    I've left row (1) for your Headers.
    NB:- You must have all the ranges filled from your start Date/Time to you Finish Date/time
    The Resulting Sub total will be shown in column ("E"). The grand total, In "Hrs" will be shown in
    The last row after the end of the list, and also in a Msgbox.
    NB:_ In the lines of code you will see two lines starting with the word rem . If you remove those two words, when you run the code you will find columns "F" & "G" pasted, agaist the rows,with the Total Start and Finish times in Number Form representing those dates /times in Hrs.
    See if you can get it working !!
    Please Login or Register  to view this content.
    Regards Mick

  5. #5
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650
    Inserting a Command Button in worksheet and Allocating Code

    Open your workbook at the Sheet Number with your Data that you want to Manipulate.
    Click View, Tools, Control Box,---- Control Box Menu Appears on sheet.
    Slide you cursor over the Tool Box until you find a Command Button.
    Click It then click Somewhere on the sheet.-- Command Button appears on sheet.
    The Command Button will have handles round its edge, This is so you can Position it on the sheet.
    Click the command Button "Hold the Mouse Click Down" and Drag the Command Button to where you want it.
    Double Click the Command Button, The VB Editor window will appear.
    You can also open the Editor By clicking Alt + F11, but if you double click the Command Bar the editor will open in the procedure relating specifically to your Command Bar.
    If the VB Editor window has two panes the right pane is where you must paste your code.
    The left pane can be "Project Window" or "Properties Window, Click "Ctrl+R" if not showing.
    Paste your code just under the words "Private Sub CommandButton1_Click() " in the Right hand pane.
    If you have done this correctly. Scroll to the bottom of the code and you should see the words "End Sub"
    To ensure you code is Running, Paste the bit of code below into your Code ,between the lines "Next cl" And "End Sub"
    On the VB Toolbar you will see a Green Triangular shapes icon,.
    This is to change the VB Editor mode from "Run Mode" to "Design Mode "
    Click this Icon, The Small blue square to its left will change from light blue to dark blue, or Vice Versa. Before you close the Editor make sure this Square is "Dark Blue" i.e. (Reset)
    Close the Editor. Select the Command Button and Click it.
    (Steve this bit about the Msgbox does not apply to you)When the code Runs the Msgbox should appear With The Message " "Transfer Complete" ,if this doesn't happen Open The VB Editor "Alt + F11" and click the "Reset ( Blue Square) and or The "Green Triangle" on the Tool bar . The Blue Square should be "Dark Blue ", in order to run the code..
    NB:- If you want to get back into this specific code through the Command Button.
    Get the Control Box menu back on the screen, Click the green triangle, When you slide the cursor over your Command button , The cursor shape will change to a "Arrow Headed Cross" , you will then be able to double click it to view your code.
    If you prefer, you can forgo all this hassle by putting the code in an MT macro, with a key combination (Like Ctrl+"A") to run it.
    Don't be daunted by all this, when you get the hang of it, it will seem quite simple
    Please Login or Register  to view this content.
    Any problems call back.
    Regards Mick

  6. #6
    Registered User
    Join Date
    02-16-2008
    Posts
    10
    Nice code Mick! Your solution makes such a clean interface. You know, it is so strange that I am an Access Programmer but lose my way around Excel so easy. Even making a button. I forgot I had to turn the security setting to low to make the button run (which is why I got confused with your initial answer). I try to stay out of Excel if I can help it but your instructions were like liquid courage. I feel brave now (Freedom!).

    Unfortunately, when I do the math by hand, I get 12 days or 288 hours. Below is the plot of actual times where at least one washer is running. Oddly enough, when I paste what's below in the spreadsheet and use your code it nails it with the right anwer (288 hours). Wonder why it doesn't work with the columns as shown at the beginning? Anyhow, your time to help me is most appreciated.

    Start Date StartTime Stop Date StopTime
    1/21/2008 10:00 1/22/2008 10:30
    1/23/2008 17:00 1/29/2008 6:30
    1/30/2008 12:15 2/1/2008 16:45
    2/4/2008 14:30 2/7/2008 13:15
    2/8/2008 12:15 2/8/2008 12:50
    2/11/2008 8:00 2/11/2008 13:10
    2/11/2008 13:30 2/11/2008 14:30

+ 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