logo
search
VBA & Macro Problems

How to Use VBA to Wrap Selected Excel Formulas in ROUND

Huda QurayshiHuda Qurayshi Oct 22, 2026 868 views

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.

How to Use VBA to Wrap Selected Excel Formulas in ROUND
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press the Alt + F11 keys on your keyboard to open the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

In the VBA editor menu, click on 'Insert' and select 'Module' to create a blank script window.

3
Paste the VBA Code

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

4
Run the Macro on Selected Cells

Close the VBA editor. In your worksheet, highlight the cells you want to modify, press Alt + F8, select 'RoundFormulas', and click 'Run'.

Use a VBA Macro to Wrap Formulas in a Selected Range
Adjusting Decimal Places: To round to two decimal places instead of zero, change the ",0)" at the end of the script string to ",2)".
Advanced Spreadsheet Automation

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. 1. Open Your Workbook: Launch WPS Spreadsheet and open the .xlsm or .xlsx file containing the formulas you want to round.
  2. 2. Access the Macro Editor: Navigate to the 'Tools' tab on the top ribbon and click on 'Macro', then select 'Visual Basic Editor'.
  3. 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.
Fully compatible with Microsoft Excel .xlsx and .xlsm formatsBuilt-in Visual Basic Editor for writing and executing macros seamlesslyLightweight application that runs smoothly even on older devicesFamiliar user interface with virtually no learning curve
microsoft office alternative - wps office

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)".