• # 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
<img src="/images/VBA06-01.jpg">

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
<img src="/images/VBA06-02.jpg">

I'm afraid that you must be logged in to comment or leave a testimonial. I wish it could be otherwise, but I'm trying to keep my site spam free for everyone's benefit. If you don't yet have an account it's completely free to sign up, and a very quick process. Simply click here to Register. Not only can you post a comment here, but it gives you full access to posts questions in our forum as well!

If you already have an account, and just haven't logged in yet, what are you waiting for? Login Now!

1. Rick Rothstein -
I have another (somewhat shorter) alternate for your SumR function..
Code:
Function SumR(rng As Range) As Double  SumR = Evaluate("SUM(ROUND(" & rng.Address & ",2))")
End Function
1. Rick Rothstein -
I see my function code did not layout correctly. I'll try again...
Code:
Function SumR(rng As Range) As Double

SumR = Evaluate("SUM(ROUND(" & rng.Address & ",2))")

End Function
1. Bob Phillips -
I would suggest the precision is an optional parameter

Code:
Function SumR(rng As Range, Optional ByVal precision As Long = 2) As Double

SumR = Evaluate("SUM(ROUND(" & rng.Address & "," & precision & "))")

End Function
1. Rick Rothstein -
That is a good suggestion Bob, especially given how easy it is to incorporate into my function. In my defense, I was responding to the post code which only wanted to round to 2 decimal places, so unthinkingly I just did the same thing.
1. Bob Phillips -
No defence needed Rick, just thought that as you had taken the trouble to suggest the alternative, I might as well suggest an enhancement to make it a tad more useful - couldn't resist .t

Good to see you here BTW. It is not the busiest site, but you will be a good addition.
• ### Recent Forum Posts

#### Help with time duration calculation

See the first formula here: https://exceljet.net/formula/get-wor...ates-and-times with explanations.
You'll probably need a bit more...

p45cal 2022-05-13, 02:48 PM

#### Help with time duration calculation

Hello All,

Please I need some assistance with a formula to calculate the time duration between 2 dates with the following conditions:...

TN0410 2022-05-13, 10:05 AM

#### Add data to and existing table using named ranges.

Thanks for coming back to me.

After receiving your reply I did a bit of playing around to see if I could get it right and figured it out....

NWVR 2022-05-11, 12:58 PM

#### Dependant drop list data validation Indirect formula

The data validation is not calculated until you press the dropdown, which is why I asked when exactly you see the message. If you see it after entering...

JoePublic 2022-05-11, 08:34 AM

#### Dependant drop list data validation Indirect formula

Well, basically at every entry.
The real file has 10 more sheets (1-calc, 2-calc, 3-calc and so on) that have manual input (D2, d3 and so on) column....

Zinc 2022-05-10, 08:20 PM