Fix Excel Workbook Corruption After Running a Macro
Question details
The user needs to prevent Excel from crashing and corrupting the workbook when running a macro that creates, copies, and deletes drill-down sheets.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Running an Excel macro that generates drill-down sheets from pivot tables, copies them to a new workbook, and deletes the temporary sheets.
- Observed behavior
- Excel crashes during the sheet deletion process, and the saved destination file gets corrupted, reporting 'removed query table XML parts' upon opening.
Before modifying your VBA code, ensure you have created a secure backup copy of your original macro-enabled workbook to prevent accidental data loss during troubleshooting.
Optimize VBA Code and Explicitly Define Variables
Clean up your macro code to prevent memory conflicts and unexpected active-object errors during runtime.
Macros generated by the macro recorder often rely on 'Active' objects and generic selections. These can lead to instability and crashes, especially when moving data between multiple workbooks.
At the very top of your VBA module, type 'Option Explicit'. This forces you to declare every variable you use, catching typos and preventing unhandled memory references.
Search your code for 'Select', 'Selection', 'ActiveWorkbook', and 'ActiveSheet'. Replace these with explicitly declared Workbook and Worksheet variables.
When your code creates a new drill-down sheet, assign it to a worksheet variable on the very next line (e.g., 'Set DetailsSheet = ActiveSheet'). Use this variable for all subsequent operations.

Handle Cross-Workbook Sheet Deletions Safely
Avoid crashes caused by Excel getting confused when multiple workbooks contain similarly named sheets.
Remove Power Query Connections from Copied Sheets
Fix the 'removed query table XML parts' corruption error by disconnecting inherited queries.
Use WPS Office for a Highly Stable Spreadsheet Experience
If Excel frequently crashes or struggles with file corruption during complex macro executions or pivot table operations, consider switching to WPS Office. It provides a lightweight, highly compatible spreadsheet environment that handles data efficiently without the bloated overhead.
- 1. Download and Install: Visit the official WPS website to download the free WPS Office suite and run the installer.
- 2. Open Your Workbooks: Launch WPS Spreadsheet and open your existing Excel workbooks directly; no format conversion is required.
- 3. Manage Data Easily: Use the familiar ribbon interface to manage your pivot tables, macros, and data connections smoothly.

Frequently Asked Questions
What causes the 'removed query table XML parts' error in Excel?
This error typically occurs when a copied worksheet retains broken Power Query data connections or external data links. When Excel attempts to save and validate the XML structure, it detects the broken query tables and automatically removes them to prevent total file failure. Disconnecting these queries before saving the file resolves the issue.
Why does my Excel macro crash when deleting worksheets?
Excel can crash during worksheet deletion if you are using generic references like 'ActiveSheet.Delete' while multiple open workbooks contain sheets with the exact same name. Excel loses track of which workbook has the active focus. Explicitly defining the target workbook variable before deletion prevents this crash.
How do I unlink a pivot table drill-down sheet from its source data?
If you want the drill-down sheet to contain only static data, you can copy the entire sheet, use 'Paste Special' > 'Values' to overwrite the formulas, and delete any associated QueryTables or ListObjects via VBA before saving the new workbook.




