Open In App

How to Round With Doubles in Excel VBA

Last Updated : 19 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The round function is used to round the floating numbers to a particular decimal value. Rounding a number, where it has a long decimal value will give a good balance between accuracy and readability. It also helps in calculation and also to get a precise result. In real life, it is useful in doing mental mathematics, calculating taxes, and also in restaurants or grocery stores. Two types of rounds are there:

  1. Round-Up: If the last digit after the decimal of a number that we are trying to round is greater than or equal to 0.6.   
  2. Round Down: f the last digit after the decimal of a number which we are trying to round is less than or equal to 0.4.

Now, let’s discuss if the decimal part is exactly 0.5. Then, in such cases, if the integer part is even round it down to the even number of the decimal. If the integer part is odd then round it up to the even number of the decimal. Now, we will discuss the steps to round with doubles in Excel VBA,

How to Apply VBA Macros to ROUND a Cell Value

Step 1: Open the VBA Box and Insert the Module

Press Alt + F11 to get the VBA box and select Insert and Module to write the code.

Clicking-module-from-insert-option

Step 2: Define a Sub-Procedure in VBA

Define a sub-procedure in VBA.

Defining-sub-procedure

Step 3: Define a Variable Number

Define a variable “number” as Double which will hold the number to be rounded.

Defining-a-variable-number

Step 4: Use Round Function

Now we will use MsgBox to display the rounded number which is rounded up to 1 decimal place with the help of the Round function.

Using-round-function

Step 5: Preview Result

Press F5 to see the output

Rounded-number-obtained

Use VBA to Round a Number to 2 Decimal Places for a Specific Sheet

Step 1: Open the VBA Window

Go to the Developer tab and open the Visual Basic Window

Screenshot-(178)

Step 2: Navigate to the Insert Tab and Select Module

Go to the Insert Tab and Select Module from the list.

Screenshot-(179)

Step 3: Write the below code in the Module

Sub Round_Numbers()

Dim number As Double

number = 879.657

MsgBox “Rounded Number” & Round(number, 2)

End Sub

Step 4: Click on Run Button

Screenshot-(187)

Step 5: Preview Result

Screenshot-(186)

How to Use RoundUp Function and RoundDown Function in Excel VBA

Similarly, we can do RoundUp and RoundDown functions. For that we have to follow further steps:-

Step 1: Declare three variables as Double

  1. One variable to store the number which is to be rounded up or down.
  2. The second variable is to store the number after rounding it up. 
  3. The third one is to store the number after rounding it down.

Declaring-three-variables

Step 2: Use RoundUp and RoundDown Functions

Now, we will use RoundUp and RoundDown functions to round the number and then store it to their respective double variables.

Using-round-up-and-round-down-functions

Step 3: Use MsgBox to Print the Values

We will use MsgBox to print the rounded-up and rounded-down values.

Printing-values

Step 4: Preview Result

Press F5 to see the output.

Rounded-number-obtainedRounded-number-obtained

Conclusion

Excel VBA provides powerful tools like Round, RoundUp, and RoundDown functions, enabling efficient handling of rounding scenarios for various applications. By following the above steps, you can confidently implement these functions in your Excel VBA projects, enhancing precision and clarity in your calculations.

FAQs

What is the purpose of rounding numbers in Excel VBA?

Rounding numbers in Excel VBA serves to strike s balance between accuracy and readability. It is particularly useful in calculations where precision is required without overwhelming users with excessive decimal places. Rounding also finds applications in various real-life scenarios such as financial calculations, tax computations, and displaying results in a user- friendly format.

How to round a floating-point number to a specific decimal place in Excel VBA?

You can use the built-in ‘Round’ Function to Round a floating-point number to a specific decimal place in Excel VBA. For example:

Dim originalNumber As Double

Dim roundNumber As Double

OrignalNUmber = 12,3456

roundnumber = Round(originalNumber, 2) ‘Rounds to 2 decimal places

How to use the Round-Up and Round-Down Functions in Excel VBA?

Use the ‘WorksheetFunction’ object to access the ‘RoundUp’ and ‘RoundDown’ functions. See the example given below:

Dim originalnumber As Double

Dim roundUpNumber As Double

Dim roundDownNumber As Double

OriginalNumber =15.67

roundUpNumber = WorksheetFunction.RoundUp(originalNumber, 0) ‘ Rounds up to the nearest integer

roundDownNumber = WorksheetFunction.RoundDown(originalNumber,0) ‘ Rounds down to the nearest integer

How to round with doubles in Excel VBA?

Follow the steps to round with doubles in Excel VBA:

Step 1: Open the VBA Editor (press Alt + F11)

Step 2: Insert a new module by clicking “Insert” and then “Module”.

Step 3: Define a Sub-procedure to encapsulate your rounding code.

Step 4: Declare a variable (e.g., ‘oringinalNumber) to type Double to hold the number to be rounded.

Step 5: Use the ‘Round’ function to round ‘originalNumber’ to a specific decimal place.

Step 6: Display the rounded number using the ‘MsgBox’ Function.

Step 7: Run the code by pressing F5.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads