VBA Round Function – Purpose, Syntax and Arguments
Description:
The Round function in VBA (Visual Basic for Applications) is used to round a given number to a specific number of decimal places. It is a mathematical function that is commonly used in programming to manipulate and format numeric values.
Purpose:
The purpose of the Round function is to simplify the process of rounding numbers in VBA. It takes a number and the desired number of decimal places as arguments, and returns the rounded value. This function can be especially useful when working with financial data or when presenting results in business or scientific applications.
Syntax:
Round (number, [NumDigitsAfterDecimal])
Arguments:
- Number: The number to be rounded.
- NumDigitsAfterDecimal (optional): The number of decimal places to which the given number should be rounded. If omitted, the number will be rounded to the nearest whole number.
Example:
Suppose we have a variable named ‘price’ with a value of 17.6243 and we want to round it to 2 decimal places. We can use the Round function in the following way:
Dim price As Double price = Round(17.6243, 2)
The value of the ‘price’ variable after using the Round function will be 17.62.
Remarks and Important Notes:
1. Negative values:
The Round function can handle both positive and negative numbers. If the number is positive, it will be rounded up, and if it is negative, it will be rounded down.
2. Halfway rounding:
When rounding a number that is equidistant between two possible rounded values, the Round function will round to the nearest even number. For example, if we use the Round function to round 2.5, it will round it to 2, but if we round 3.5, it will round it to 4.
3. Rounding to 0 decimal places:
If the ‘NumDigitsAfterDecimal’ argument is set to 0, the Round function will round the given number to the nearest integer.
4. Rounding to negative decimal places:
It is also possible to use the Round function to round a number to negative decimal places. For example, if we want to round 1234.5678 to the nearest 10, we can use the following code:
Dim roundedNum As Double roundedNum = Round(1234.5678, -1)
The value of the ’roundedNum’ variable will be 1230.
5. Rounding to a specific significance:
Instead of specifying the number of decimal places, we can also use the Round function to round a number to a specific significance. For example, if we want to round 3659.87 to the nearest hundred, we can use the following code:
Dim roundedNum As Double roundedNum = Round(3659.87, -2)
The value of the ’roundedNum’ variable will be 3700.
6. Rounding to nearest 5 cents:
We can use the Round function to round a number to the nearest 5 cents by specifying the desired number of decimal places as -2. For example, if we want to round 45.58 to the nearest 5 cents, we can use the following code:
Dim roundedNum As Double roundedNum = Round(45.58, -2)
The value of the ’roundedNum’ variable will be 45.60.
7. Rounding to nearest 10 cents:
Similarly, we can round a number to the nearest 10 cents by using the desired number of decimal places as -1. For example, if we want to round 56.42 to the nearest 10 cents, we can use the following code:
Dim roundedNum As Double roundedNum = Round(56.42, -1)
The value of the ’roundedNum’ variable will be 60.00.
In conclusion, the Round function in VBA is a handy tool for rounding numbers to a specified number of decimal places or a specific significance. It simplifies the process of rounding and can be essential in various programming applications.
Understanding VBA Round Function with Examples
Example 1: Rounding to Whole Numbers
Suppose we have a cell A1 in Excel that contains the value 12.64, and we want to round it to the nearest whole number using VBA. The code would be:
Sub RoundExample1() Dim num As Variant num = Range("A1").Value Range("A2").Value = Round(num) End Sub
- In the first line, we declare a variable ‘num’ of the Variant data type to store the value from cell A1.
- In the next line, we use the ‘Value’ property to retrieve the value from cell A1 and assign it to the variable ‘num’.
- In the third line, we use the Round function to round the value stored in ‘num’ to the nearest integer. Since we have not provided the ‘NumDigitsAfterDecimal’ parameter, the number will be rounded to its nearest whole number.
- Finally, we use the ‘Value’ property to assign the rounded value to cell A2.
The output of this code would be 13, as 12.64 is closer to 13 than to 12.
Example 2: Rounding to a Specific Decimal Place
In this example, we will round a number to a specific number of decimal places. Let us use the same code as in Example 1, but this time, we will provide the ‘NumDigitsAfterDecimal’ parameter to the Round function.
Range("A2").Value = Round(num, 1)
Here, we have specified ‘1’ as the value for the ‘NumDigitsAfterDecimal’ parameter. This means that the number will be rounded to one decimal place. So, the output of this code would be 12.6, as 12.64 rounded to one decimal place is 12.6.
Example 3: Rounding to Negative Decimal Places
The Round function in VBA can also be used to round numbers to negative decimal places. Let us take the same code as Example 2, but this time we will provide a negative value for the ‘NumDigitsAfterDecimal’ parameter.
Range("A2").Value = Round(num, -1)
Here, we have specified ‘-1’ as the value for the ‘NumDigitsAfterDecimal’ parameter. This means that the number will be rounded to the nearest 10. So, the output of this code would be 10, as 12.64 rounded to the nearest 10 is 10.
Example 4: Rounding Negative Numbers
In the previous examples, we have only dealt with positive numbers. But what if we have negative numbers that need to be rounded? Let us modify the first example to include a negative number.
Sub RoundExample1() Dim num As Variant num = Range("A1").Value Range("A2").Value = Round(num) End Sub
Assuming that cell A1 contains the value -12.64, the output of this code would be -13, as -12.64 is closer to -13 than to -12.
Example 5: Rounding Exact Midpoints
One important aspect of the Round function is how it handles numbers that are exactly halfway between two numbers after rounding. Let us consider the following code:
Range("A2").Value = Round(1.5) Range("A3").Value = Round(2.5) Range("A4").Value = Round(3.5)
The output of this code would be 2, 2, and 4, respectively. This is because the Round function uses a rounding methodology called round-to-even or banker’s rounding. This means that if the number to be rounded is exactly at the midpoint, it will be rounded to the nearest even number. In the case of 1.5 and 2.5, they are closer to 2 than to 1, and hence they are rounded to 2. And 3.5 is closer to 4 than to 3, and hence it is rounded to 4.
Example 6: Rounding Up and Down
The Round function in VBA also has another parameter called ‘RoundMode’, which can be used to specify the rounding behavior. Let us consider the following code:
Range("A2").Value = Round(2.5, 0, 0) Range("A3").Value = Round(2.5, 0, 1)
The output of this code would be 3 and 2, respectively. This is because the third parameter is used to specify the ‘RoundMode’, where:
- 0: rounds up (default behavior)
- 1: rounds down
In the first case, the number is rounded to 3, as it is closer to 3 than to 2. And in the second case, the number is rounded to 2, as it is closer to 2 than to 3.
Conclusion
The Round function in VBA is a useful tool for rounding numbers to a specified number of decimal places. It can be used to round both positive and negative numbers and has the option to specify a rounding behavior using the ‘RoundMode’ parameter. Understanding the syntax and usage of this function can make data manipulation in Excel using VBA much more efficient and accurate. I hope this post has helped you understand the Round function better, and you can now use it confidently in your VBA projects.