VBA Round Function
Round function in Excel VBA round up or round down to the specific set of decimal places. For the example of you have the number 5.8697 we can round to two digit decimal three digit decimal or no decimal at all. If you round the number to a two digit decimal it will 5.87 if you round to three digits then it will round to 5.870, if you wish to round to zero then it will 6.
In banking numbers, all the decimal places which are less than 0.5 will be rounded down to the previous integer value and all the decimal places which are greater than or equal to 0.5 will be rounded up to the next integer value.
I hope you have used the ROUND function in the worksheet. In VBA too we can use this function but we have a difference with these functions. We will see the difference between these two functions later in this article.
Take a look at the formula of ROUND function in VBA.
Number: This is the number we are trying to round.
[Number of Digits after Decimal]: How many digits you need after the decimal value.
Examples of ROUND Function in VBA
Take a look at a few examples to understand the excel VBA ROUND function practically. Assume you have the number 4.534 and you want to round to two digits. Follow the below steps.
Step 1: Declare the variable as Variant.
Sub Round_Example1() Dim K As Variant End Sub
Step 2: For this variable “k” assign the value through VBA ROUND function.
Sub Round_Example1() Dim K As Variant K = Round( End Sub
Step 3: Number is nothing but what is the number we are trying to round, in this case, the number is 4.534.
4.6 (247 ratings)
Sub Round_Example1() Dim K As Variant K = Round(4.534, End Sub
Step 4: How many digits we need to round, in this case, we need to round to 2 digits.
Sub Round_Example1() Dim K As Variant K = Round(4.534, 2) End Sub
Step 5: Now show the variable “k” value in the message box VBA.
Sub Round_Example1() Dim K As Variant K = Round(4.534, 2) MsgBox K End Sub
Run this code and see what we get.
We got the result as 4.53 when we rounded to 2 digits.
Now I will change the number from 4.534 to 4.535. Look what happens now.
Sub Round_Example1() Dim K As Variant K = Round(4.535, 2) MsgBox K End Sub
Now run the code and see what the result is.
We got the result as 4.54 one decimal higher than the previous value of 4.53. This is because in this example we have supplied the number as 4.535, so after the number 3 next number is 5 so it is rounded to the next number so 3 becomes 4.
Now I will supply the number as 2.452678 and I will try to round to 3 digits.
Sub Round_Example2() Dim K As Variant K = Round(2.452678, 3) MsgBox K End Sub
Run this code to see the result.
The result is 2.453.
2.452678 Here numbers after the 2nd decimal place are 2678. After number 2, the next number is 6 which is greater than or equal to 5 so it is rounded up the next decimal number.
Now I will use the same number to round to zero and see what happens.
Sub Round_Example3() Dim K As Variant K = Round(2.452678, 0) MsgBox K End Sub
Run the code and see what we get.
Since I have used the round to zero we got the result as 2.
The reason why we got the result as 2 because here the decimal first number is 4 which is less than the 0.5 so it is rounded down.
Differences Between VBA ROUND and Excel ROUND
After all these you must be thinking of what difference between VBA Round and Excel Round is. There are mainly 2 differences.
#1 – Syntax of Both the Functions: If you look at the syntax of both the function we have a difference here.
Excel Round Syntax: Round (Number, Number of Digits After Decimal)
VBA Round Syntax: Round (Number, [Number of Digits After Decimal])
In Excel VBA ROUND both the arguments are mandatory but in VBA Round second argument is optional.
In VBA if you ignore the second argument VBA takes the default argument as zero so we will get the whole number.
#2 – Results: The result given by these two functions are different. Below are a few examples
You can download this VBA Round Function Excel here. VBA Round Function Excel Template
This has been a guide to VBA Round Function. Here we learn how to use Round Function in Excel VBA (up or down) along with some practical examples and downloadable excel template. Below are some useful excel articles related to VBA:-