+ Reply to Thread
Results 1 to 9 of 9

VBA round up & down by 7 minutes on time

  1. #1
    Registered User
    Join Date
    11-17-2015
    Location
    Nitra, Slovakia
    MS-Off Ver
    O365
    Posts
    69

    VBA round up & down by 7 minutes on time

    Hi all,

    I tried to google search this macro but I cannot find anything close to what I want:

    So I have a workbook for my clock-ins and outs, and from next month we're changing format and instead of rounding up&down by 15 minutes as it was until now, it will be rounded by 7. It was easy to calculate but now I just don't wanna bother with it each day.

    So clock-ins and outs are going to be rounded the same way:

    If I clock-in / out @ 7:07 or 6:53 it will go to 7:00, but if I clock @ 6:52 that will go to 6:45 and if @ 7:08 that will go to 7:15

    Hope this is clear enough,

    Cell range is: B3:C33

    Thanks
    Last edited by Martines91; 06-14-2019 at 01:11 AM.

  2. #2
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: VBA round up & down by 7 minutes on time

    I broke down the the process in 3 steps. Get the minutes value first, then get the rounding, and finally, get the difference. See attached since it is a bit hard to explain the math.
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    11-17-2015
    Location
    Nitra, Slovakia
    MS-Off Ver
    O365
    Posts
    69

    Re: VBA round up & down by 7 minutes on time

    Yes that's exactly what I want, but instead of having multiple cells I just want to type the value to the cell and have it automatically corrected via VBA macro

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,393

    Re: VBA round up & down by 7 minutes on time

    Since VBA's only built in rounding function is the Round() function (which does basic bankers rounding to the nearest decimal place), I would probably use Excel's MROUND() function for this.

    What role does VBA play in the project? Since this is a fairly simple built in Excel function, I would probably do the whole thing in Excel and not even use VBA. If my date/time value (not text) is in A1, an MROUND() function like =MROUND(A1,7/60/24) would round that time to the nearest 7 minute increment (always rounding 3.5 minute up). The 7/60/24 converts 7 minutes to fraction of a day (Excel's base unit for storing date/time values).

    If VBA is essential to the task, the MROUND() function can be called from VBA: https://docs.microsoft.com/en-us/off...n-visual-basic
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: VBA round up & down by 7 minutes on time

    I already laid out the logic for you, you should be able to take the logic put in the VBA code. Can you show us what have you done with VBA so far?

  6. #6
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: VBA round up & down by 7 minutes on time

    With unadjusted time in B3, rounded time:
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  7. #7
    Forum Expert
    Join Date
    10-10-2016
    Location
    Sheffield
    MS-Off Ver
    365 and rarely 2016
    Posts
    3,212

    Re: VBA round up & down by 7 minutes on time

    =MROUND(b3,1/96)

    round to a 96th of a day = 15 minutes, which is actually what you are rounding to (the nearest 15 minutes)

  8. #8
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: VBA round up & down by 7 minutes on time

    As a VBA function:
    Please Login or Register  to view this content.
    And to change as you type:
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    11-17-2015
    Location
    Nitra, Slovakia
    MS-Off Ver
    O365
    Posts
    69

    Re: VBA round up & down by 7 minutes on time

    Quote Originally Posted by Olly View Post
    As a VBA function:
    Please Login or Register  to view this content.
    And to change as you type:
    Please Login or Register  to view this content.
    This works perfectly! Thank you

+ 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. Round Time to 30 minutes
    By Shiv in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-27-2014, 05:11 AM
  2. 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
  3. Round up to whole minutes
    By Rocky_bear in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-12-2013, 10:30 AM
  4. How do I round minutes up and down?
    By Agent007 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-08-2013, 10:50 PM
  5. How to covert and round up time to minutes
    By ramnadh in forum Excel General
    Replies: 3
    Last Post: 12-08-2010, 08:17 AM
  6. Converting hours and minutes in military time to minutes
    By Argile79 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 04-13-2010, 02:42 PM
  7. Round up a time to the next 15 minutes
    By Frederick Chow in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-05-2006, 06:41 AM
  8. How to round down to nearest 5 minutes, time calc?
    By StargateFanFromWork in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-18-2005, 05:05 AM

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