How to Use VBA to Wrap Selected Excel Formulas in ROUND
Question details
The user needs to use a VBA macro to add the ROUND function around existing formulas within a selected range of Excel cells.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating the process of rounding formula results in multiple cells without having to manually edit each individual formula string.
- Observed behavior
- Existing formulas calculate exact unrounded values, and the user requires them to be wrapped in ROUND to specific decimal places while preserving the original formula structure.
Ensure that you have enabled the Developer tab in your spreadsheet application to access the VBA editor. Always save a copy of your workbook before running new macros.
Use a VBA Macro to Wrap Formulas in a Selected Range
This macro loops through the currently selected cells and wraps any existing formula with the ROUND function.
This solution iterates over each cell in your selection. By checking if the cell contains a formula, it safely modifies only calculated cells, avoiding static text or numerical inputs.
Press the Alt + F11 keys on your keyboard to open the Visual Basic for Applications (VBA) editor.
In the VBA editor menu, click on 'Insert' and select 'Module' to create a blank script window.
Copy and paste the following code into the module: Sub RoundFormulas() Dim cell As Range For Each cell In Selection If cell.HasFormula Then cell.Formula = "=ROUND(" & Mid(cell.Formula, 2) & ",0)" End If Next cell End Sub
Close the VBA editor. In your worksheet, highlight the cells you want to modify, press Alt + F8, select 'RoundFormulas', and click 'Run'.

Wrap the Formula in the Active Cell Only
Use a simplified macro snippet if you only need to wrap the formula in a single currently selected cell.
Use Macros to Automate Tasks in WPS Spreadsheet
WPS Spreadsheet provides robust VBA and Macro support, allowing you to run custom scripts like wrapping formulas in ROUND just as you would in Microsoft Excel.
- 1. Open Your Workbook: Launch WPS Spreadsheet and open the .xlsm or .xlsx file containing the formulas you want to round.
- 2. Access the Macro Editor: Navigate to the 'Tools' tab on the top ribbon and click on 'Macro', then select 'Visual Basic Editor'.
- 3. Insert and Run the Code: Insert a new module, paste your VBA rounding script, highlight the target cells, and run the macro directly within WPS Spreadsheet.

Frequently Asked Questions
Why isn't my VBA macro modifying the selected cells?
Ensure that macros are enabled in your spreadsheet's Trust Center settings. Additionally, verify that the selected cells actually contain formulas. The provided script specifically checks for 'HasFormula', so plain text or static numbers will be ignored.
Can I use this macro to wrap formulas with ROUNDUP or ROUNDDOWN?
Yes. You can easily modify the macro string from "=ROUND(" to "=ROUNDUP(" or "=ROUNDDOWN(" within the VBA code to achieve your desired rounding logic.
Will this script damage cells containing plain values instead of formulas?
No, the script includes an 'If cell.HasFormula Then' condition. This ensures that the macro only applies changes to cells that start with an equals sign and evaluate a formula, keeping your static data completely safe.
How can I round the values to three decimal places?
In the macro code line, simply change the ",0)" argument to ",3)" at the end of the text string. For example: cell.Formula = "=ROUND(" & Mid(cell.Formula, 2) & ",3)".




