How to Fix Broken Excel References After VBA Inserts Rows
Question details
The user needs to prevent formulas from breaking or updating incorrectly when inserting rows into related sections using a VBA macro.

- 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 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.
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.
Update your VBA script to insert the required number of rows into all related sections without copying or pasting the formulas yet.
Store your header ranges and target areas in explicit VBA variables to keep track of the layout as it expands.
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.

Optimize VBA Code with Explicit References
Avoid reference errors caused by dynamic selection by hardcoding specific worksheets and ranges in your VBA script.
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. Open your workbook in WPS: Launch WPS Office and open your .xlsm file containing the VBA macros.
- 2. Access the Developer tools: Navigate to the 'Developer' tab on the top ribbon to access Macro settings.
- 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.

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.




