The VBA ROUND Function

Related Functions:


The VBA Round function rounds a numbers to a specified number of decimal places.

The syntax of the function is:

Round( Number, [NumDigitsAfterDecimal] )

Where the function arguments are:

Number-The number that you want to round.

An optional positive integer specifying the number of decimal places that you want to round to.

If the [NumDigitsAfterDecimal] argument is omitted, it takes on the default value 0 (i.e. round to the nearest integer).

VBA Round Function Examples

' Round the number 77.777 to 2 and 0 decimal places.
Dim res1 As Double
Dim res2 As Double
res1 = Round( 77.777, 2 )
res2 = Round( 77.777 )
' Now, res1 = 77.78 and res2 = 78

In the above VBA code:

VBA Round Function Errors

If the [NumDigitsAfterDecimal] argument that is supplied to the Round function is negative, you will get the error:

Run-time error '5': Invalid procedure call or argument

VBA Run Time Error 5 Message Box

If one or both of the arguments to the Round function cannot be interpreted as numeric values, the function will return the error:

Run-time error '13': Type mismatch

VBA Run Time Error 13 Message Box