How can I get VBA to round values to 2 decimal places?

In order to round values to two decimal places in VBA, you can use the Round function. The basic syntax of this function is Round(number, num_digits), where number is the value you wish to round, and num_digits is the number of decimal places you wish to round to. For example, Round(3.14159, 2) would return 3.14. You can also use the Format function to format the number to two decimal places, such as Format(3.14159, “0.00”).


You can use the following methods to round values to 2 decimal places using VBA:

Method 1: Round One Value to 2 Decimal Places

Sub RoundTwoDecimals()
    Range("B2") = WorksheetFunction.Round(Range("A2"), 2)
End Sub

This particular example will round the value in cell A2 to two decimal places and display the rounded value in cell B2.

Method 2: Round All Values in Range to 2 Decimal Places

Sub RoundTwoDecimals()
    Dim i As Integer

    For i = 2 To 9
        Range("B" & i) = WorksheetFunction.Round(Range("A" & i), 2)
    Next i
End Sub

This particular example will round each value in the range A2:A9 to two decimal places and display the rounded values in the range B2:B9.

The following examples show how to use each method in practice.

Example 1: Round One Value to 2 Decimal Places

We can create the following macro to round the value in cell A2 to two decimal places and display the result in cell B2:

Sub RoundTwoDecimals()
    Range("B2") = WorksheetFunction.Round(Range("A2"), 2)
End Sub

When we run this macro, we receive the following output:

Notice that the value 15.248 in cell A2 has been rounded to two decimal places (15.25) in cell B2.

Example 2: Round All Values in Range to 2 Decimal Places

We can create the following macro to round each of the values in the range A2:A9 to two decimal places and display the rounded values in the range B2:B9:

Sub RoundTwoDecimals()
    Dim i As Integer

    For i = 2 To 9
        Range("B" & i) = WorksheetFunction.Round(Range("A" & i), 2)
    Next i
End Sub

Notice that each value in the range A2:A9 has been rounded to two decimal places and the rounded values are shown in the range B2:B9.

Note: You can find the complete documentation for the VBA Round method .

x