• User Defined Function To Sum And Round To Two Decimal Places

    Macro Purpose:
    • To round a Sum formula to two decimal places
    Examples of where this function shines:
    • When you are feeling really lazy, and don't want to type =Round(Sum(A1:A3),2), you can just type =Sumr(A1:A3)
    Macro Weakness(es):
    • Will calculate slower than the nested Round and Sum combination. Performance degridation most likely won't be noticed if it is only used a few times, but if hundreds of formulas are used, it may slow the calculation down noticeably.
    • Only accepts range arguments, not values.
    • Only accept one contiguous range (not multiple ranges).
    Versions Tested:
    This function has been tested with Excel 2003. It should not have any issues running from any of the Office applications from 97 or higher, but to date this has not been tested.

    VBA Code Required:
    • Place the following code in a standard module
      Code:
      Function SumR(rng As Range) As Double
      'Author       : Ken Puls (www.excelguru.ca)
      'With thanks to Remco Boom for the "\1" method
      'Macro Purpose: Rounds the Sum'd range to 2 decimals
           SumR = ((100 * Application.WorksheetFunction.Sum(rng.Cells) + 0.01) \ 1) / 100
      End Function
    How to use the code:
    • Once the code is in a standard module, use it as a regular formula in a worksheet cell, as shown below
    How it works:
    • The uses Excel's native SUM formula to sum up the range, multiplies it by 100 and adds .01. At this point it then rounds it off to no decimal places, and divides it by 100 to get the result.
    • The \1 operator rounds the amount to zero decimal places. The .01 addition in the middle of the formula corrects an issue with the rounding of this function, as it rounds 0.5 down to zero.
    The End Result:
    • The sum of the range is returned, rounded to 2 decimal places.
    An Alternative:
    • The SumRDown function, shown below, will always round the value down to 2 decimal places.
      Code:
      Function SumRDown(rng As Range) As Double
      'Author       : Ken Puls (www.excelguru.ca)
      'Macro Purpose: Rounds the Sum'd range down to 2 decimals
           SumRDown = Int(100 * Application.WorksheetFunction.Sum(rng.Cells)) / 100
      End Function
  • MVP Logo
  •  Donations

    If you like our website and would like to give something in return, you can make a donation. All donations are gratefully received and go to support the site.


    Select your preferred currency and donation amount, then click the donate button.

  • Recent Forum Posts

    Colo

    How to Rename a File from English to Other Language?

    Yeah, some massive HTML conversion is my line. In other words, most of difficult things can be done with Excel alone like this time. Well done, Excel!...

    Colo Today, 07:36 AM Go to last post
    Ken Puls

    Link a series name to a cell using Excel 2010 VBA

    Hi there,

    I recorded linking the title to a cell and it came back with the following. Does this help?

    Code:
        ActiveChart.SetElement
    ...

    Ken Puls Today, 04:06 AM Go to last post
    Ken Puls

    How to Rename a File from English to Other Language?

    Colo, that was way too easy... I was expecting some massive HTML conversion, or a huge engine to compare each character against a library of Chr codes!...

    Ken Puls Today, 04:02 AM Go to last post
    Ken Puls

    Shared file\macro & IP address

    Oh, and as for the max number of users who can access the file in the shared folder at once...

    • For reading, I believe it's unlimited. (The second and
    ...

    Ken Puls Today, 03:59 AM Go to last post
    Ken Puls

    Shared file\macro & IP address

    ibrahimaa,

    There is no one-line way to get your IP address the way you are getting the username. So you're going to need more code than...

    Ken Puls Today, 03:56 AM Go to last post