VBA: How to Round Values to 2 Decimal Places


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

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

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 here.

Additional Resources

The following tutorials explain how to perform other common tasks in VBA:

How to Round Down Values in VBA
How to Round Up Values in VBA

Leave a Reply

Your email address will not be published. Required fields are marked *