totn Excel Functions

MS Excel: How to use the ROUND Function (VBA)

This Excel tutorial explains how to use the Excel ROUND function (in VBA) with syntax and examples.

Description

The Microsoft Excel ROUND function returns a number rounded to a specified number of digits.

The ROUND function is a built-in function in Excel that is categorized as a Math/Trig Function. It can be used as a VBA function (VBA) in Excel. As a VBA function, you can use this function in macro code that is entered through the Microsoft Visual Basic Editor.

Please read our ROUND function (WS) page if you are looking for the worksheet version of the ROUND function as it has a very different syntax.

It is very important to note that the VBA ROUND function behaves a little peculiar and uses something commonly referred to as bankers rounding. So before using this function, please read the following:

The ROUND function utilizes round-to-even logic. If the expression that you are rounding ends with a 5, the ROUND function will round the expression so that the last digit is an even number.

For example:

Round(12.55, 1)
Result: 12.6   (rounds up)

Round(12.65, 1)
Result: 12.6   (rounds down)

Round(12.75, 1)
Result: 12.8   (rounds up)

In these cases, the last digit after rounding is always an even number. So, be sure to only use the ROUND function if this is your desired result.

Syntax

The syntax for the ROUND function in Microsoft Excel is:

Round ( expression, [decimal_places] )

Parameters or Arguments

expression
A numeric expression that is to be rounded.
decimal_places
Optional. It is the number of decimal places to round the expression to. If this parameter is omitted, then the ROUND function will return an integer.

Returns

The ROUND function returns a numeric value.

Applies To

  • Excel for Office 365, Excel 2019, Excel 2016, Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000

Type of Function

  • VBA function (VBA)

Example (as VBA Function)

In Excel's VBA environment, the ROUND function returns a number rounded to a specified number of decimal places. As a reminder, the ROUND function uses something commonly referred to as bankers rounding. So please be careful before using this function.

The ROUND function can be used in VBA code in Microsoft Excel. Here are some examples of what the ROUND function would return:

Round(210.665, 2)
Result: 210.66  'example of bankers rounding

Round(210.67, 1)
Result: 210.7

Round(210.67, 0)
Result: 211

Round(210.67)
Result: 211

For example:

Dim LNumber As Double

LNumber = Round(210.67, 1)

In this example, the variable called LNumber would now contain the value of 210.7.

Tip to Avoid Bankers Rounding:

If you want to avoid bankers rounding, you can create your own custom function as follows:

' This function overcomes the bankers Rounding that occurs in the
' built-in VBA Round function to provide true (symmetric) numeric Rounding
' Created by TechOnTheNet.com
Public Function StandardRound(pValue As Double, pDecimalPlaces As Integer) As Variant

    Dim LValue As String
    Dim LPos As Integer
    Dim LNumDecimals As Long
    Dim LDecimalSymbol As String
    Dim QValue As Double

    ' Return an error if the decimal places provided is negative
    If pDecimalPlaces < 0 Then
       StandardRound = CVErr(2001)
          Exit Function
    End If

    ' If your country uses a different symbol than the "." to denote a decimal
    ' then change the following LDecimalSymbol variable to that character
    LDecimalSymbol = "."

    ' Determine the number of decimal places in the value provided using
    ' the length of the value and the position of the decimal symbol
    LValue = CStr(pValue)
    LPos = InStr(LValue, LDecimalSymbol)
    LNumDecimals = Len(LValue) - LPos

    ' Round if the value provided has decimals and the number of decimals
    ' is greater than the number of decimal places we are rounding to
    If (LPos > 0) And (LNumDecimals > 0) And (LNumDecimals > pDecimalPlaces) Then

        ' Calculate the factor to add
        QValue = (1 / (10 ^ (LNumDecimals + 1)))

        ' Symmetric rounding is commonly desired so if the value is
        ' negative, make the factor negative
        ' (Remove the following 3 lines if you require "Round Up" rounding)
        If (pValue < 0) Then
            QValue = -QValue
        End If

        ' Add a 1 to the end of the value (For example, if pValue is 12.65
        ' then we will use 12.651 when rounding)
        StandardRound = Round(pValue + QValue, pDecimalPlaces)

    ' Otherwise return the original value
    Else
        StandardRound = pValue
    End If

End Function

And then call the StandardRound function instead of using the Round function.

Frequently Asked Questions

Question: I read your explanation of how the ROUND function works in VBA using the round-to-even logic. However, I really need to round some values in the traditional sense (where 5 always rounds up). How can I do this?

Answer: You could always use the following logic:

If you wanted to round 12.65 to 1 decimal place in the traditional sense (where 12.65 rounded to 1 decimal place is 12.7, instead of 12.6), try adding 0.000001 to your number before applying the ROUND function:

Round(12.45+0.000001,1)

By adding the 0.000001, the expression that you are rounding will end in 1, instead of 5...causing the ROUND function to round in the traditional way.

And the 0.000001 does not significantly affect the value of your expression so you shouldn't introduce any calculation errors.