VBA round() function and worksheetFunction.Round() returning different results

gtwo

New Member
Joined
Jan 14, 2020
Messages
48
Office Version
  1. 2016
  2. 2010
Platform
  1. Windows
Does anyone know why the following happens?

In vba...

MsgBox Round(0.25, 1) 'shows 0.2
MsgBox worksheetFunction.Round(0.25, 1) 'shows 0.3

wtf is happening here?
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
With one exception, all rounding in VBA, whether explicit (such as the Round function) or implicit (such as assigning a floating point number to an integer variable) use what is known as Banker's Rounding (if the rounding position is exactly a 5, then it is rounded to the nearest even number (in your case, the 2 is the nearest even number). Remember I said there was one exception.... it is the Format function which rounds like Excel does (5's always round up). So, instead of using this...

MsgBox Round(0.25, 1)

use this...

MsgBox Format(0.25, "0.0")
 
Last edited:
Upvote 0
Solution
Thank you for the quick answer, I didn't know vba Round function worked like that, and that is a very important thing for what I'm doing. I'll remember that, thank you again!
 
Upvote 0
You are welcome. By the way, I probably should have mentioned that you can call out directly to Excel's ROUND function using the WorksheetFunction object (but I think the Format function is slightly more efficient) like this...

MsgBox WorksheetFunction.Round(0.25, 1)

Doing it this way is a little more self-documenting as you "know" you are deliberately rounding... that might not be as clear to others reading your code if you use the Format function to do the rounding (although I guess you could add a comment saying so).
 
Upvote 0
I just replaced everything from " Round(" to " WorksheetFunction.Round(", that way it works as I need it, and it was easier to replace everything for sure, I had almost 50 lines of code with with the VBA Round function, and that could have caused some problems. Even though it is only me who currently work in code, maybe one day another person might touch it, and the "Format( n, "0.0")" might look like something else for someone who is not aware of the Banker's Rounding, at least That's what I think.
 
Upvote 0

Forum statistics

Threads
1,215,038
Messages
6,122,798
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top