VBA & Macro Problems

Keep an Excel VBA Macro Running When Closing Another Workbook

Jason MillerJason Miller Dec 2, 2026 869 views

Question details

Keep a long-running Excel VBA procedure running when another workbook is closed.

Keep an Excel VBA Macro Running When Closing Another Workbook
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.
Before you start

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.

Solution 1Recommended

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.

1
Identify the macro host

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.

2
Assign explicit references

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.

3
Review workbook dependencies

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.

4
Test the procedure

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.

Qualify workbook references and keep the macro host open
Closing the macro host: The macro cannot be expected to continue if its host workbook or add-in is closed, or if Excel itself exits.
Free Microsoft Office alternative

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.

Open and edit common Excel formats, including XLSX and XLSUse a familiar spreadsheet interface for everyday workbook tasksKeep working with workbook data in a lightweight Office alternativeMove existing spreadsheet files into WPS without recreating them
microsoft office alternative - wps office

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.