Keep an Excel VBA Macro Running When Closing Another Workbook
Question details
Keep a long-running Excel VBA procedure running when another workbook is closed.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A VBA procedure is running while the user closes a different workbook with its window close button.
- Observed behavior
- The procedure may stop when closing the other workbook triggers events, invalidates references, or exposes an unhandled error.
Identify the workbook or add-in that contains the running macro and confirm it will remain open. In Excel, open the VBA editor with Alt+F11 so you can inspect the procedure and workbook event code.
Qualify workbook references and keep the macro host open
Use this approach when closing another workbook changes the active workbook or invalidates an object used by the macro.
In the VBA editor, use Project Explorer to locate the module containing the procedure. Keep that workbook or add-in open while the macro runs; closing the host or quitting Excel can end the procedure.
In the procedure, assign the intended workbook and worksheet to object variables, such as Set wb = ThisWorkbook and Set ws = wb.Worksheets("Sheet1"), when those are the correct targets. Replace references to ActiveWorkbook and ActiveSheet with the appropriate variables.
Check each Workbooks reference in the procedure. Complete any operations that require a workbook before it is closed, and do not access its worksheets or other objects after closing it.
Run the macro from Excel's Developer > Macros dialog, then close the other workbook. Check whether the procedure continues and whether it still writes to the intended workbook and worksheet.

Inspect close events and log VBA errors
Use this approach if explicit references do not explain why the procedure stops.
Work with Excel files in WPS Spreadsheet
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for common Excel formats, making it easy to move everyday workbook tasks to WPS. It does not fix an interruption in Microsoft Excel VBA.

Frequently Asked Questions
Why does closing another workbook affect my macro?
Closing a workbook can change ActiveWorkbook or ActiveSheet, trigger close or deactivate events, and invalidate references to the closed workbook. Use explicit object references and inspect event handlers in the VBA editor.
Can the macro continue if I close the workbook containing its code?
Do not rely on a macro continuing after its host workbook or add-in closes. Keep the host open, and make sure Excel itself does not exit while the procedure is running.
Where can I see a VBA error recorded with Debug.Print?
In the VBA editor, select View > Immediate Window. Reproduce the interruption and check the window for the error number and description printed by your error handler.
Should I disable workbook events to prevent the interruption?
First inspect Workbook_BeforeClose and Workbook_Deactivate in each workbook's ThisWorkbook module. Disabling events can prevent necessary close logic from running, so identify the cause before changing event behavior.




