
Originally Posted by
sflemings
Is it possible to take the Math.Round() VB formula and stick it in as a macro? Would that make any difference or would it just be the same result.
It would give the same result.
The ROUND function is not broken! It is doing exactly as it is supposed to, it is rounding using conventional rulings.
What you are wanting though is BANKERS ROUNDING, which is not what the ROUND function does, hence you get the 'wrong' result.
Thats why I had to generate a custom formula for you.
If you want it in VBA format, (and also don't want to need the Analysis toolpak), then paste the following code into a VBA module in your workbook:
Option Explicit
Function BankersRound(num_to_round As Double, decimal_places As Long)
' Will perform bankers rounding on the number passed.
If Right(Round(num_to_round, decimal_places + 1), 1) = "5" Then
BankersRound = Round(num_to_round / (2 * 10 ^ -decimal_places), 0) * (2 * 10 ^ -decimal_places)
Exit Function
Else
BankersRound = Round(num_to_round, decimal_places)
Exit Function
End If
End Function
You can then use it in your worksheet like:
or
=BankersRound(0.123456789, 7)
Or you can use it in your vba code:
Dim result as double
double = BankersRound(Range("A1").Value, 7)
or
Dim result as double
double = BankersRound(0.123456789, 7)
Try it and let me know how you get on please
Bookmarks