+ Reply to Thread
Results 1 to 5 of 5

Time between 2 dates and times

Hybrid View

  1. #1
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Time between 2 dates and times

    Hi, I have a col with pick dates (K) and a col with pick times(J) I want to find the how much time it took the picker to pick all the orders in a week.

    Trying this:Getting a type mismatch error

    
    StartDay = Cells(22, "K") 'small date
      stime = Cells(22, "J")    'start time
    EndDay = Cells(lrow, "K") 'large date
      etime = Cells(lrow, "J") ' end time
     
    Results = DateDiff("h", StartDay & " " & stime, EndDay & " " & etime)
    Can someone advise, thank you Mike
    Last edited by realniceguy5000; 02-17-2012 at 12:03 PM.

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: Time between 2 dates and times

    Hi Mike,

    You could use:
    DateDiff("h", StartDay + stime, EndDay + etime)
    However that would include all hours of the day, every day. So Feb 16, 2012 8:00 AM through Feb 18, 2012 10:00AM would result in 50. If you only wanted to count a max of 8 hours per day, you could probably do something along the lines of:
    DateDiff("h", StartDay + stime, EndDay + etime) - (DateDiff("d", StartDay, EndDay) * 16)
    There are probably other alternatives for dealing with specific hours (e.g. 8am-5pm, or multiple shifts, etc).

  3. #3
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Time between 2 dates and times

    Thanks Paul, It appears to work as you said. However thinking this through a bit more since I dont know for sure what hours they will be working each day I decided to loop through the range and add up all the working minutes they work.

    The problem I have now is I am returning the number of minutes but I want to place how many hours and minutes back into the cell as hh:mm.

    I'm trying this after I get the minutes total(myresults) in this case its 2971 minutes which is 49 hours and 31 minutes
    Hours = Int(myresults / 60) 'get hours from myresults (49)
    Minutes = myresults Mod 60  'get minutes from myresults (31)
    Now I want to place 49:31 back in the cell, This is the part that isnt working not sure which method is correct to use or if both are wrong because it appears results is a text value "49:31" and not a time value, but when I change the format of the cell it becomes 01:31

    Can someone advise which is the corret method or another method?

    Thank You, Mike

    results = Hours & ":" & Minutes
    Cells(8, "B") = results
    Cells(8, "B").NumberFormat = "hh:mm"
    or
    results = Format(Hours, "0#") & ":" & Format(Minutes, "0#")
    Cells(8, "B") = results
    Cells(8, "B").NumberFormat = "hh:mm"

    Here is how it's layed out in the script which seems to work till I change the format of the cell?

    
        Range("K21:K" & lrow).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range( _
            "O21"), Unique:=True
        dlrow = Cells(Rows.Count, "O").End(xlUp).Row
            Range("A21:N21").AutoFilter
            For i = 22 To dlrow
                
                Range("A21:N21").AutoFilter Field:=11, Criteria1:=Cells(i, "O").Value
                    With wks.AutoFilter.Range
                    On Error Resume Next
                    Set rng2 = .Offset(1, 0).Resize(.Rows.Count - 1, 1) _
                    .SpecialCells(xlCellTypeVisible)
                    On Error GoTo 0
                    End With
                        If rng2 Is Nothing Then
                    Stop
                    
                        Else
                        Set rng = wks.AutoFilter.Range
                        With rng
                            Lrnglrow = Cells(Rows.Count, 1).End(xlUp).Row
                            frngrow = .Offset(1, 0).SpecialCells(xlCellTypeVisible).Row
                            Cells(frngrow, "P").FormulaR1C1 = "=SUM(RC[-5],RC[-6])"
                            Cells(frngrow, "P").NumberFormat = "mm/dd/yyyy hh:mm:ss"
                            Cells(Lrnglrow, "P").FormulaR1C1 = "=SUM(RC[-5],RC[-6])"
                            Cells(Lrnglrow, "P").NumberFormat = "mm/dd/yyyy hh:mm:ss"
                            
                            myresults = myresults + DateDiff("n", Cells(frngrow, "P"), Cells(Lrnglrow, "P"))
       
                        End With
                                                         
                End If
            Next i
            Stop
                  
            
            Range("A21:N21").AutoFilter
            Cells(6, "B") = Cells(22, "P")
            Cells(6, "B").NumberFormat = "[$-F400]h:mm:ss AM/PM" 'returns First start time
            Cells(7, "B") = Cells(lrow, "P")
            Cells(7, "B").NumberFormat = "[$-F400]h:mm:ss AM/PM" ' Returns last end time
            
            Hours = Int(myresults / 60) 'get hours from myresults 
            Minutes = myresults Mod 60  'get minutes from myresults
    
        
            results = Hours & ":" & Minutes
            results = Format(Hours, "0#") & ":" & Format(Minutes, "0#")
            
            
            Cells(8, "B") = results
            Cells(8, "B").NumberFormat = "hh:mm"

  4. #4
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Time between 2 dates and times

    You need:
    Cells(8, "B").NumberFormat = "[hh]:mm"
    to display more than 24 hours as hours.
    Good luck.

  5. #5
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Time between 2 dates and times

    Oh Good Grief...my bad...

    Thanks for pointing that out.

    Mike

+ 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