+ Reply to Thread
Results 1 to 18 of 18

Rounding Decimals 2 places

  1. #1
    Registered User
    Join Date
    05-07-2013
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    55

    Rounding Decimals 2 places

    Is there a faster way to round decimals to 2 decimal places?

    I am currently using the code below and works just fine; however, with the amount of data I work with I could have over 50,000+ rows of data and some values may not need rounding. My overall goal is to really identify only those values that exist where there are more than 2 number past the decimal and round it so that it doesn't take so long to run. With the amount of data I am using it is taking several minutes using the code below. I have attached an example. Any help is greatly appreciated. If identifying only those that need rounding isn't possible, any updates or faster way of doing this would help

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by matrix2280; 02-23-2015 at 08:55 PM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,865

    Re: Rounding Decimals 2 places

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    05-07-2013
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    55

    Re: Rounding Decimals 2 places

    Fixed the reference of code. Sorry, and thanks

  4. #4
    Registered User
    Join Date
    05-07-2013
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    55

    Re: Rounding Decimals 2 places

    Hi everyone, any ideas on a faster way to perform this task?

  5. #5
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,814

    Re: Rounding Decimals 2 places

    Does it have to be VBA? I know there is a "performance penalty" whenever VBA has to talk to Excel. The fastest approach I could readily see would be to use Excel's =ROUND(D2,2) function in a helper column. You could then, if needed, copy - paste special - as values over the original data, if needed.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

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

    Re: Rounding Decimals 2 places

    Here's an alternative you can try cautiously.

    Please Login or Register  to view this content.
    The reason for caution is that it will round every constant value (not formula results) in the workbook to the value that's displayed. The setting gets turned off, but the change will be permament for the cells affected when the macro runs.
    Last edited by shg; 03-13-2015 at 04:11 PM.
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Rounding Decimals 2 places

    Hi matrix

    You might try this...
    Please Login or Register  to view this content.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  8. #8
    Registered User
    Join Date
    05-07-2013
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    55

    Re: Rounding Decimals 2 places

    Unfortunately, Yes, it has to be VBA as I have been doing this process manually using the =Round(D2,2) and copying down to all rows, then copy/paste values over the originals.

  9. #9
    Registered User
    Join Date
    05-07-2013
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    55

    Re: Rounding Decimals 2 places

    I've tried the Rng.NumberFormat = "0.00" but it will still show as more than two decimals when I export to CSV / convert to text. It needs to truly round. The code I have does that, but just take too long to run when I have 10,000+ rows of values to parse through.

    The screenshot below uses column B, but same concept from my original (having to use the same code for multiple projects, but regardless will always be just the single column)

    2015-03-13_16-13-35.jpg

  10. #10
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Rounding Decimals 2 places

    Hi matrix

    Just guessing...try this
    Please Login or Register  to view this content.

  11. #11
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,602

    Re: Rounding Decimals 2 places

    0.109 sec (50K rows):
    Please Login or Register  to view this content.
    Ben Van Johnson

  12. #12
    Registered User
    Join Date
    05-07-2013
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    55

    Re: Rounding Decimals 2 places

    protonLeah - I think that will work just fine That is really fast!

    Thanks protonLeah, and everyone else who responded. It is always greatly appreciated.

  13. #13
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,602

    Re: Rounding Decimals 2 places

    You're welcome

  14. #14
    Registered User
    Join Date
    05-07-2013
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    55

    Re: Rounding Decimals 2 places

    ProtonLeah
    For some reason when it gets to row 44753, everything from this row forward is showing up as #N/A instead of rounding. Everything from 44752 prior is working fine. The Total rows for this particular file is 110228 rows (again that number could be more / less depending on the client I am working with).

    I was trying to upload the actual file but it is 4MB. Below is the code. The only thing I had changed was the Sheets Name. I changed it back to Sheet1 as you had it, but still did same thing.

    Please Login or Register  to view this content.

    datasample.jpg

    Thanks
    Last edited by matrix2280; 03-15-2015 at 01:38 PM.

  15. #15
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,517

    Re: Rounding Decimals 2 places

    Try this
    Please Login or Register  to view this content.

  16. #16
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,602

    Re: Rounding Decimals 2 places

    Post a workbook with several (12...) of the #N/A rows

  17. #17
    Registered User
    Join Date
    05-07-2013
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    55

    Re: Rounding Decimals 2 places

    Thanks Jindon,

    This worked. For some reason I have to run the Macro twice for it to round; however, I put a loop to run it twice and works great.

    Thanks again everyone

  18. #18
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,517

    Re: Rounding Decimals 2 places

    Quote Originally Posted by matrix2280 View Post
    Thanks Jindon,

    This worked. For some reason I have to run the Macro twice for it to round
    That's odd....

+ 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. Macro doesnt read 2 decimals places after decimal when it is a .00
    By betboy25 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-27-2012, 03:57 PM
  2. [SOLVED] 0;-0;;@ - format to show as two decimals places
    By johnmitch38 in forum Excel General
    Replies: 3
    Last Post: 09-05-2012, 09:34 AM
  3. convert decimals to only 2 places for whole range
    By mitta23p in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-16-2011, 11:06 AM
  4. convert decimals to only 2 places
    By TechRetard in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-14-2011, 09:51 AM
  5. Change a # w/decimal places to no decimals, 13 char's, w/leading 0
    By Mary in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-30-2006, 01:15 PM

Tags for this Thread

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