Excel VBA Round Function

VBA Round Function: The Round function in VBA rounds a number to a specified number of decimal places and returns the number.

Syntax

Round (Number, [NumDigitsAfterDecimal])

Parameter

Number (required) –This parameter represents a numeric value one wants to round.

NumDigitsAfterDecimal (optional) – This parameter represents a positive integer stating the number of decimal places that you want to round to. By default, it is set to 0.

Return

This function returns a rounded number to the specified number of decimals.

Example 1

Sub RoundFunction_Example1()
 ' Rounding the number for 0 decimal places.
 Dim round_val As Double
 round_val = Round(57.777)
 ' The variable round_val will return 58
 Cells(1, 1).Value = round_val
 End Sub 

Output

58

VBA Round Function

Example 2

Sub RoundFunction_Example2()
 ' Rounding the number for 2 decimal places.
 Dim round_val As Double
 round_val = Round(57.777, 2)
 ' The variable round_val will return 58.78
 Cells(1, 1).Value = round_val
 End Sub 

Output

58.78

VBA Round Function

Example 3

Sub RoundFunction_Example3()
 ' Rounding the number for -2 decimal places.
 Dim round_val As Double
 'for negative number digit after decimal
 round_val = Round(57.777, -2)
 ' The variable round_val will return run-time error.
 Cells(1, 1).Value = round_val
 End Sub 

Output

VBA Round Function