Excel VBA Round Function
Round function in VBA is a mathematical function which as the name itself suggests it rounds up or round down the given number to the specific set of decimal places specified by the user, this function uses the logic of round even which means it takes 5 as the reference and any number with last digit after the decimal is below 5 then it is round down and vice versa.
You can round a number to two-digit decimal, three-digit decimal or no decimal at all. For example, if you have the number 5.8697. If you round the number to a two-digit decimal it will be 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 syntax of the Round function.
Number: This is the number we are trying to round.
[Number of Digits after Decimal]: How many digits you need after the decimal value.
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 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
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.
Difference Between Excel and VBA ROUND Function
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 both the arguments are mandatory but in VBA second argument is optional.
In VBA if you ignore the second argument it 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
This has been a guide to VBA Round Function. Here we learn how to use vba round function (up or down) along with some practical examples and downloadable excel template. Below are some useful excel articles related to VBA:-