Excel VBA Number Rounding: A Practical How-To Guide
In this article
Excel VBA rounds numbers with a handful of built-in functions, and the one you pick changes the result. The two you will use most are Round(), which rounds to a set number of decimal places, and WorksheetFunction.Round(), which applies Excel's own rounding rules inside your macro. For money, use the WorksheetFunction version or a custom half-up formula, because VBA's native Round() uses banker's rounding and will surprise you on exact halves. Everything below works in any modern Excel on Windows or Mac; the VBA editor and function names are the same on both, though menu positions can differ slightly by version.
What You'll Need
- Microsoft Excel with macros enabled (the desktop app on Windows or Mac; Excel for the web cannot run VBA).
- Access to the VBA editor, opened with Alt+F11 on Windows or Fn+Option+F11 on many Macs, or via the Developer tab if it is shown.
- A workbook saved as .xlsm (macro-enabled) if you want to keep the code, or .xlsb if you prefer a binary format.
- A test sheet with a few sample numbers so you can check results before running anything on real data.
Step-by-Step
- Open the VBA editor and insert a module. Press Alt+F11, then in the editor choose Insert > Module. A blank code window appears. Modules hold your procedures and are visible in the Project Explorer on the left. If you cannot find the Insert menu on a Mac, use the editor's menu bar at the top of the screen; the wording is the same.
- Decide your rounding rule before writing code. Ask one question: how should exact halves behave? For 2.5, should the answer be 3 (half up) or 2 (banker's)? For prices, tax, weights on invoices and anything a human will check by hand, most people want half up. For statistical or engineering work, banker's rounding is often preferred because it avoids upward bias across many values. Write this down first, because it determines which function you use.
- Round with VBA's native Round() when banker's rounding is acceptable. Its syntax is
Round(number, numdecimalplaces). For exampleMsgBox Round(2.5, 0)returns 2, andMsgBox Round(3.5, 0)returns 4. The second argument is optional and defaults to 0. The catch: exact halves round to the nearest even digit, so 0.5 becomes 0, 1.5 becomes 2, 2.5 becomes 2. This is correct behaviour, not a bug, but it is not what most spreadsheet users expect. - Round with Excel's rules using WorksheetFunction.Round. Write
MsgBox WorksheetFunction.Round(2.5, 0)and you get 3. This calls the same engine the ROUND function in a cell uses, which rounds halves away from zero. This is the safer default for business work. You can also useWorksheetFunction.RoundUp,WorksheetFunction.RoundDown,WorksheetFunction.MRound(round to a multiple) andWorksheetFunction.CeilingorFloorif you need to push values up or down to a step. - Write a reusable function for half-up rounding. If you want a clean helper, put this in your module:
Function RoundHalfUp(ByVal n As Double, ByVal d As Long) As DoublethenRoundHalfUp = WorksheetFunction.Round(n, d)thenEnd Function. Now any procedure can callRoundHalfUp(2.565, 2). Keeping it in one place means if your rule ever changes, you edit one line. - Round to whole numbers, tens or hundreds. For whole numbers use
Round(x, 0). For tens, useRound(x / 10, 0) * 10; for hundreds,Round(x / 100, 0) * 100. Alternatively,WorksheetFunction.MRound(1234, 100)returns 1200 and is easier to read. MRound rounds to the nearest multiple of the second argument. - Apply rounding across a range. Loop the cells rather than rounding in your head. A compact pattern is
For Each c In Range("B2:B100"), thenc.Value = WorksheetFunction.Round(c.Value, 2), thenNext c. Test on a copy of the sheet first, because writing back to cells overwrites the originals and Undo does not work after a macro runs. - Beware of the double-precision trap. A value that displays as 2.565 may be stored as 2.56499999 internally, causing an unexpected round down. Rounding the displayed number does not fix the stored one. If two-decimal accuracy matters, round at the point where the value is created, not only at the end.
- Round inside a formula you write to a cell. Set
Range("C2").Formula = "=ROUND(B2,2)"to get a live formula instead of a static value. The difference matters: a formula recalculates when B2 changes, while a value written by your macro stays put until the macro runs again. - Save as .xlsm and test with a fixed set of cases. Include 0.5, 1.5, 2.5, negative halves like -2.5, and values such as 1.005 where binary storage is awkward. Run each through your chosen function and compare against what Excel's cell formula produces. If they match, you are done.
Common Mistakes
Assuming Round() matches the ROUND() cell function. This is the single biggest source of errors. They disagree on exact halves. Use WorksheetFunction.Round when you want the spreadsheet behaviour.
Forgetting that rounding changes totals. If you round each line item to two decimals and then sum, the total can differ by a cent or two from rounding the unrounded total. Decide whether to round line by line or only at the summary, and say so in your sheet notes.
Rounding a number without assigning the result. Round(x, 2) on its own line does nothing to x. You must write x = Round(x, 2). VBA does not modify arguments in place.
Using Integer or Long for decimals. Those types drop the fractional part and round nothing; they truncate. Keep values as Double or Currency while calculating. Currency is often the better choice for money because it avoids many floating-point surprises.
Rounding negative numbers without checking. Round and ROUND both treat -2.5 and 2.5 by magnitude, but RoundDown moves toward zero while RoundUp moves away, which produces results people often misread as errors. Test your negatives.
Running a macro on live data with no backup. Macros overwrite values and cannot be undone. Copy the sheet or save a version before looping through real figures.
Tips
- Use
WorksheetFunction.Round(value, digits)as your default for anything a person will read and check. - Keep rounding rules in one clearly named helper function so the whole workbook behaves the same way.
- Set
Application.Calculationcarefully if your macro writes formulas; screen updating and calculation can be switched off for speed, but switch them back on at the end or the workbook appears frozen. - When displaying numbers, separate the format from the value. Round the value in code, then apply number formatting for looks. Formatting alone leaves the underlying value full precision and will make totals look wrong.
- Add
Option Explicitat the top of every module; it catches misspelled variable names that otherwise silently become empty values. - Comment the rounding rule next to the function so a future reader knows it is deliberately half up and not a mistake.
Troubleshooting
The macro returns an error such as "Unable to get the Round property of the WorksheetFunction class". This usually means a non-numeric value, an empty cell, or text in the range. Check with IsNumeric before rounding, and skip blanks in your loop.
Results differ by one in the last digit. Compare your function against a cell formula side by side. If VBA's Round disagrees with ROUND in a cell, you are almost certainly hitting the banker's rounding difference on an exact half. Switch to WorksheetFunction.Round.
Nothing happens when you run the code. Check the procedure is a Sub in a standard module, that macros are enabled, and that the workbook is not in a state where events block execution. Also confirm the cell references point at the right sheet; an unqualified Range refers to the active sheet, which may not be the one you expect.
The macro seems slow or Excel freezes. Large loops over many cells can take time. Wrap the loop with screen updating and automatic calculation switched off, then restore both at the end. If it still stalls, limit the range to the used area rather than whole columns.
Values revert after reopening the file. If you saved as .xlsx, the code is stripped out. Save as .xlsm instead and check that macros are allowed in your Trust Center settings.
If none of that works, rebuild a small test case: one cell, one line of rounding code, one message box. Once that behaves, add your real range back a few rows at a time until the failure reappears, which identifies the offending value.
Frequently Asked Questions
Does VBA's Round() use the same rule as Excel's ROUND()?
No. VBA's Round uses banker's rounding, so exact halves go to the nearest even digit. The ROUND function in a cell rounds halves away from zero. Use WorksheetFunction.Round to match the cell function.
How do I round up to the next whole number in VBA?
Use WorksheetFunction.RoundUp(value, 0) or WorksheetFunction.Ceiling(value, 1). Both move away from zero, so be careful with negative numbers.
How can I round to the nearest 5 or 10?
Use WorksheetFunction.MRound(value, 5) or WorksheetFunction.MRound(value, 10). The second argument is the step you want to land on.
Why does a number that looks like 2.565 round down to 2.56?
Binary storage means the true value is slightly less than 2.565. Excel rounds the stored value, not the displayed one. Round earlier in the calculation or add a small correction before rounding.
Can I round a whole column at once?
Not with a single built-in call. Loop through the cells in a macro, or write a ROUND formula into an adjacent column so Excel does the recalculation for you.
Should I use Currency instead of Double for money?
Often yes. Currency stores values in a fixed-point format that avoids many floating-point oddities, which makes cent-level rounding more predictable.