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

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

In Visual Basic for Applications (VBA), there are various methods to round numerical values to a specific number of decimal places. One common approach is to use the “Round” function, which allows for the rounding of numbers to a specified number of decimal places. This can be achieved by passing the number to be rounded and the desired number of decimal places as arguments to the function. Additionally, the “Format” function can also be used to round numbers to a specific number of decimal places by specifying the appropriate format code. By using these methods, programmers can easily manipulate numerical values in VBA to meet their specific requirements for precision and accuracy.

VBA: 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

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 .

Cite this article

stats writer (2024). How can I round values to 2 decimal places in VBA?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-round-values-to-2-decimal-places-in-vba/

stats writer. "How can I round values to 2 decimal places in VBA?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-round-values-to-2-decimal-places-in-vba/.

stats writer. "How can I round values to 2 decimal places in VBA?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-round-values-to-2-decimal-places-in-vba/.

stats writer (2024) 'How can I round values to 2 decimal places in VBA?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-round-values-to-2-decimal-places-in-vba/.

[1] stats writer, "How can I round values to 2 decimal places in VBA?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I round values to 2 decimal places in VBA?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top