+ Reply to Thread
Results 1 to 11 of 11

Round time to nearest 5 mins

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-15-2011
    Location
    UK
    MS-Off Ver
    Excel 2013
    Posts
    163

    Round time to nearest 5 mins

    Hello all,

    I have a column of time values hh:mm:ss

    I am trying to format the time to hh:mm and round the time values to the nearest 5 mins. Anybody know how to do this with vba?
    Last edited by rtcwlomax; 07-02-2015 at 03:55 PM.

  2. #2
    Forum Contributor
    Join Date
    11-15-2011
    Location
    UK
    MS-Off Ver
    Excel 2013
    Posts
    163

    Re: Round time to nearest 5 mins

    I'm also thinking to incorporate an input box that asks user to input the nearest whole minute value that the vba should round up or down to if possible.

    Sub f_time()
    '
    ' format_time Macro
    '
    
    Dim timeValue As Variant
    timeValue = InputBox("Input time")
    
    
        Cells.Find(What:="Time", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
            :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False).Activate
        Cells.FindNext(After:=ActiveCell).Activate
        Range(Selection, Selection.End(xlDown)).Select
        Selection.NumberFormat = "h:mm"
        
    '//////////////////////////////////////////////////////////
        'Missing vba to round to nearest timeValue in mins
    '//////////////////////////////////////////////////////////
        
    End Sub

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Round time to nearest 5 mins

      Dim cell As Range
      Dim iMin As Long
      
      iMin = 5
      
      For Each cell In Selection.Cells
        cell.Value = Round(cell.Value2 * 1440 / iMin, 0) * iMin / 1440
      Next cell
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Contributor
    Join Date
    11-15-2011
    Location
    UK
    MS-Off Ver
    Excel 2013
    Posts
    163

    Re: Round time to nearest 5 mins

    Thanks shg,

    Returned error in the code. Type mismatch.

    I have attached WB with Time column and macro. Would appreciate if you could take a look

    Sub f_time1()
    
    Dim cell As Range
    Dim iMin As Long
    
    iMin = InputBox("Input time")
    
    
        Cells.Find(What:="Time", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
            :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False).Activate
        Cells.FindNext(After:=ActiveCell).Activate
        Range(Selection, Selection.End(xlDown)).Select
        
        
        For Each cell In Selection.Cells
        cell.Value = Round(cell.Value2 * 1440 / iMin, 0) * iMin / 1440
      Next cell
    
    Selection.NumberFormat = "h:mm"
    
    End Sub
    Attached Files Attached Files
    Last edited by rtcwlomax; 07-02-2015 at 04:43 PM.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Round time to nearest 5 mins

    What happens when you step through the code, what are the relevant cell values?

    Time to spend some quality time at http://www.cpearson.com/excel/DebuggingVBA.aspx

  6. #6
    Forum Contributor
    Join Date
    11-15-2011
    Location
    UK
    MS-Off Ver
    Excel 2013
    Posts
    163

    Re: Round time to nearest 5 mins

    it come up with error on following line:

    cell.Value = Round(cell.Value2 * 1440 / iMin, 0) * iMin / 1440

  7. #7
    Forum Contributor
    Join Date
    11-15-2011
    Location
    UK
    MS-Off Ver
    Excel 2013
    Posts
    163

    Re: Round time to nearest 5 mins

    it come up with error on following line:

    cell.Value = Round(cell.Value2 * 1440 / iMin, 0) * iMin / 1440

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Round time to nearest 5 mins

    And the cell value is ...?

  9. #9
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Round time to nearest 5 mins

    Hi,

    You could use a standard Excel formula. e.g.

    Formula: copy to clipboard
    =MROUND(A1*1440,5)/1440


    where A1 is the cell with the time.

    In VBA you could loop down the column and use the formula above in an Application.WorksheetFunction.MROUND....etc instruction and write the value back to the cell.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  10. #10
    Forum Contributor
    Join Date
    11-15-2011
    Location
    UK
    MS-Off Ver
    Excel 2013
    Posts
    163

    Re: Round time to nearest 5 mins

    Thanks for suggestions and thanks shg for forcing me to do some debug which I have never tried before.

    Here is my solution. I'm not sure of any other way to select the cells below my search value but it works

    Sub f_time1()
    
    Dim cell As Range
    Dim iMin As Long
    
    
    iMin = InputBox("Input time")
    
    
        Cells.Find(What:="Time", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
            :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False).Activate
        Cells.FindNext(After:=ActiveCell).Activate
        Selection.Offset(1, 0).Select
        Range(Selection, Selection.End(xlDown)).Select
        
        
    For Each cell In Selection.Cells
        cell.Value = Round(cell.Value2 * 1440 / iMin, 0) * iMin / 1440
      Next cell
    
    Selection.NumberFormat = "h:mm"
    
    End Sub

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Round time to nearest 5 mins

    Good job.

    You will learn that it is almost never necessary to select cells or worksheets to operate on them, and doing so slows your code. But in the meanwhile, watching your code operate and, especially, learning to debug, are much more important.
    Last edited by shg; 07-02-2015 at 08:24 PM.

+ 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. Sum time then round to nearest 5 minutes
    By Trig79 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-27-2013, 11:02 AM
  2. Excel - round time EXACTLY to nearest second
    By TheRobsterUK in forum Excel General
    Replies: 5
    Last Post: 08-22-2012, 07:52 AM
  3. round time to nearest 15mins e.g. 16:12 to 16:15
    By simjambra in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 08-28-2009, 11:13 AM
  4. round up or down time by 5 mins in excel
    By janger-heli in forum Excel General
    Replies: 4
    Last Post: 05-07-2009, 01:08 PM
  5. Round time to nearest quarter hr
    By John in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-16-2005, 06:06 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