Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Round: How to Round Numbers in Macros

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To round a number in Excel VBA, use Application.WorksheetFunction.Round to mirror the standard Excel ROUND function, or use the native VBA Round function. The primary difference is that the worksheet function follows standard mathematical rounding (0.5 rounds up), while the VBA Round function uses "Banker's Rounding," where 0.5 rounds to the nearest even number.

What You'll Need

  • Microsoft Excel installed on a Windows or Mac computer.
  • The Developer tab enabled in the Excel Ribbon.
  • Basic familiarity with opening the Visual Basic Editor (VBE).
  • A dataset containing decimal numbers that require rounding.

Step-by-Step

  1. Open the Visual Basic Editor: Press Alt + F11 on your keyboard to launch the VBE. If you are on a Mac, use Option + F11.
  2. Insert a New Module: In the VBE menu, go to Insert > Module. This creates a blank space where you can write your custom macros.
  3. Declare Your Variables: Before rounding, you must define the variables you are working with. Use Double for the original decimal number and Double or Currency for the rounded result to maintain precision. For example:
    Dim originalValue As Double
    Dim roundedValue As Double
  4. Assign a Value: Give your variable a number to test. For instance, originalValue = 12.3456.
  5. Apply the Rounding Method: Choose one of the following three common methods based on your specific needs:
    • Method A: Standard Mathematical Rounding (WorksheetFunction)
      Use this if you want 2.5 to become 3. Use the syntax: roundedValue = Application.WorksheetFunction.Round(originalValue, 2). The second argument (2) specifies the number of decimal places.
    • Method B: VBA Native Rounding (Banker's Rounding)
      Use this for financial applications where bias must be minimized. Use the syntax: roundedValue = Round(originalValue, 2). Note that in this method, 2.5 becomes 2, and 3.5 becomes 4.
    • Method C: Rounding Up or Down
      If you need to force a number to go up (Ceiling) or down (Floor), use Application.WorksheetFunction.RoundUp(originalValue, 2) or Application.WorksheetFunction.RoundDown(originalValue, 2).
  6. Output the Result: To verify the code is working, you can print the result to the Immediate Window using Debug.Print roundedValue or place the value back into a cell using Range("A1").Value = roundedValue.
  7. Run the Macro: Press F5 or click the green Play button in the toolbar to execute the code.

Common Mistakes

One of the most frequent errors is confusing the VBA Round function with the Excel ROUND function. Because they handle the number 0.5 differently, users often find their totals are slightly off when migrating a formula from a spreadsheet into a macro. If your accounting reports don't match your VBA output, this is likely the cause.

Another common mistake is using the wrong data type. Declaring a variable as an Integer before rounding will cause VBA to automatically round the number to the nearest whole integer before your rounding function even runs. Always use Double or Decimal for variables that hold floating-point numbers.

Users also frequently forget that RoundUp and RoundDown are not native to VBA; they exist only within the WorksheetFunction library. Attempting to call RoundUp(value, 2) without the Application.WorksheetFunction prefix will result in a "Sub or Function not defined" compile error.

Tips

For maximum precision in financial applications, consider using the CDec function to convert your number to a Decimal subtype. The Double type is a floating-point number and can occasionally suffer from precision errors (e.g., 1.00000000000004), which can lead to unexpected rounding results. Converting to Decimal ensures that the rounding is performed on a base-10 representation of the number.

If you are rounding a large range of cells, avoid using a loop that calls Application.WorksheetFunction in every iteration. Calling the worksheet library from VBA is slower than using native VBA functions. Instead, read the range into a Variant Array, perform the rounding using native VBA Round (if Banker's rounding is acceptable), and write the array back to the sheet in one operation.

To round to the nearest multiple (e.g., rounding to the nearest 0.05), you can use a mathematical trick: multiply the number by 20, round it to 0 decimals, and then divide by 20. Alternatively, use Application.WorksheetFunction.MRound for this specific purpose.

Troubleshooting

If you encounter a "Type Mismatch" error, check the value of the variable you are trying to round. This usually happens if the cell contains an error value (like #VALUE! or #N/A) or text. To prevent this, wrap your rounding code in an If IsNumeric(variable) Then block to ensure only numbers are processed.

If the rounding does not seem to be happening at all, check the cell formatting in Excel. Sometimes VBA rounds the value correctly in the background, but the cell is formatted to show more decimal places than the rounded result contains, or it is formatted as "General," which may display the number in scientific notation. Set the cell format to "Number" with the desired decimal places to see the actual result.

If the VBE says "Compile error: Expected: =", ensure you are not trying to use the Round function inside a declaration statement. Functions must be called within a procedure (Sub or Function) after the variables have been declared.

Frequently Asked Questions

Why does VBA Round(2.5, 0) give 2 instead of 3?

This is called Banker's Rounding. It rounds to the nearest even number when the fraction is exactly .5. This is designed to reduce cumulative rounding bias in large datasets, as rounding .5 up every time would slightly inflate the total sum of a dataset.

How do I round a number to 2 decimal places in VBA?

Use roundedValue = Round(originalValue, 2) for Banker's rounding, or roundedValue = Application.WorksheetFunction.Round(originalValue, 2) for standard mathematical rounding.

What is the difference between Round and Int?

The Round function returns the nearest value based on the specified precision. The Int function always rounds down to the nearest whole integer, regardless of the decimal value (e.g., Int(3.9) results in 3).

Can I round a number to the nearest ten or hundred?

Yes. In both the VBA and Worksheet versions of the Round function, you can use a negative number for the second argument. For example, Round(123.45, -1) will round to the nearest ten, resulting in 120.

How do I round a value to the nearest 0.25?

The best way is to use Application.WorksheetFunction.MRound(value, 0.25). This function specifically rounds a number to the nearest multiple of the specified value.