VBA & Macro Problems

How to Fix Broken Excel References After VBA Inserts Rows

Camila MilosovichCamila Milosovich Nov 11, 2026 869 views

Question details

The user needs to prevent formulas from breaking or updating incorrectly when inserting rows into related sections using a VBA macro.

How to Fix Broken Excel References After VBA Inserts Rows
Product
Excel
Device & OS
not provided
Scenario
Running a VBA macro to insert rows into multiple data sections where formulas in one section reference cells in another.
Observed behavior
Excel automatically shifts row numbers during insertion, causing existing formulas to reference incorrect rows, even if automatic calculation is disabled.
Before you start

Before modifying and testing your VBA code, save a backup copy of your workbook to ensure you do not permanently alter your data if the macro executes incorrectly.

Solution 1Recommended

Insert Rows First and Apply Formulas Afterwards

Prevent automatic reference shifting by establishing the final spreadsheet layout before populating formulas via VBA.

When VBA inserts rows into a worksheet, the application automatically adjusts existing cell references to account for the shift. If your macro copies rows that already contain formulas, these references can become skewed as the row structure changes.

The most reliable method is to restructure your macro to separate the row insertion process from the formula generation process.

1
Modify VBA to insert rows

Update your VBA script to insert the required number of rows into all related sections without copying or pasting the formulas yet.

2
Define ranges using variables

Store your header ranges and target areas in explicit VBA variables to keep track of the layout as it expands.

3
Use FillDown for formulas

After all rows are inserted and the final layout is established, use the Range.FillDown method or write the formulas directly into the new ranges across both sections.

Insert Rows First and Apply Formulas Afterwards
Verify Intended References: Always verify that your formula references align with your new layout. For example, if a section now starts at row 9 instead of 8, ensure your macro writes the formula to reflect the new starting row.
WPS Spreadsheet Macro Support

Handle Macros and Data Flawlessly with WPS Spreadsheet

WPS Office provides robust support for VBA and Macros, allowing you to automate tasks and manage complex data structures efficiently. Its highly compatible Spreadsheet tool ensures your formulas and scripts run smoothly without referencing issues.

  1. 1. Open your workbook in WPS: Launch WPS Office and open your .xlsm file containing the VBA macros.
  2. 2. Access the Developer tools: Navigate to the 'Developer' tab on the top ribbon to access Macro settings.
  3. 3. Edit and run macros: Click on 'Visual Basic' or 'Macros' to edit your row-insertion scripts using the recommended structural fixes, then run the code securely.
Fully compatible with Microsoft Excel (.xlsx, .xlsm) formats and macros.Built-in Macro editor for writing, editing, and debugging VBA scripts.Lightweight architecture that processes complex macros and large datasets quickly.Free to use with a familiar, user-friendly interface.
QA img-9

Frequently Asked Questions

Why does disabling automatic calculation not stop references from changing?

Disabling calculation stops formulas from recalculating their mathematical results, but Excel still structurally updates cell references behind the scenes whenever rows or columns are inserted to maintain data relationships.

Can I use the INDIRECT function to prevent broken references?

Yes, the INDIRECT function allows you to lock references as text strings (e.g., =INDIRECT("A8")). This prevents the application from changing the row number when rows are inserted, though overusing it can slow down performance on large sheets.

How do I fix formulas that already show a #REF! error after running a macro?

If a macro has already broken the references, you will need to restore your workbook from a backup or undo the macro action if possible. Then, correct your VBA code to establish the layout first before filling the formulas.