Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Workbook Close Event: How to Use It

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

The Workbook Close event in Excel VBA runs automatically just before a workbook closes, letting you prompt the user to save, clean up temporary data, or cancel the close entirely. To use it, open the Visual Basic Editor (Alt+F11), double-click ThisWorkbook in the Project Explorer, and choose Workbook from the left drop-down and BeforeClose from the right drop-down above the code window. Then write your code inside the generated Private Sub Workbook_BeforeClose(Cancel As Boolean) procedure. Note that menu labels and the exact wording of prompts can differ slightly by Excel version and region.

What You'll Need

  • Microsoft Excel (any modern desktop version) with macros enabled.
  • A workbook saved as .xlsm or .xlsb, because .xlsx files cannot store VBA code.
  • Access to the Developer tab, or the ability to press Alt+F11 to open the Visual Basic Editor.
  • Basic familiarity with VBA syntax (If...Then, MsgBox, ThisWorkbook).

Step-by-Step

  1. Save the workbook in a macro-enabled format. Go to File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). If you skip this, Excel will warn you that macros cannot be saved and your event code will be lost.
  2. Open the Visual Basic Editor. Press Alt+F11 (or go to Developer > Visual Basic). If the Developer tab is hidden, right-click the ribbon and choose Customize the Ribbon to enable it.
  3. Locate ThisWorkbook. In the Project Explorer pane (usually on the left), expand VBAProject and double-click ThisWorkbook. This is the code module that holds workbook-level events.
  4. Insert the event stub. At the top of the code window, use the left drop-down to select Workbook and the right drop-down to select BeforeClose. Excel inserts:
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    End Sub
  5. Add your logic. A common pattern is to check whether the workbook has unsaved changes and ask the user. For example:
    If Not ThisWorkbook.Saved Then
        If MsgBox("Save before closing?", vbYesNoCancel) = vbCancel Then
            Cancel = True
        End If
    End If
  6. Test the event. Close the workbook normally (File > Close or the X button). Your message box should appear. If it does not, make sure macros are enabled and the code is in ThisWorkbook, not a standard module.
  7. Handle cancellation correctly. Setting Cancel = True stops the close. If you want to allow the close after a prompt, leave Cancel as False (the default).
  8. Save and close the editor. Press Ctrl+S in the VBE, then Alt+Q to return to Excel. The event will now fire every time the workbook is closed, including when closed via VBA or by another workbook.

Common Mistakes

  • Putting the code in a standard module. Workbook events only work in the ThisWorkbook module. Code in Module1 will never fire.
  • Using Workbook_Close instead of Workbook_BeforeClose. There is no Workbook_Close event; the correct name is BeforeClose, and it receives the Cancel argument.
  • Forgetting to set Cancel = True. If you want to prevent closing, you must explicitly set Cancel to True inside the event.
  • Saving as .xlsx. Macro-enabled events require .xlsm or .xlsb; otherwise the code is stripped on save.
  • Assuming the event fires on Excel shutdown. BeforeClose fires when the workbook closes, which can happen during Excel exit, but it does not fire if Excel crashes or is force-quit.
  • Creating infinite loops. If your BeforeClose code calls ThisWorkbook.Close, it can re-trigger the event. Use a module-level flag to guard against re-entry.

Tips

  • Use ThisWorkbook.Saved to check for unsaved changes before prompting.
  • Keep the event code short; long operations can make closing feel slow or trigger timeouts.
  • If you need to run cleanup only when the user actually saves, consider the BeforeSave event instead.
  • Test with macros disabled to see how the workbook behaves for users who block macros; the event simply won't run.
  • Remember that ThisWorkbook refers to the workbook containing the code, not necessarily the active workbook.

Troubleshooting

If the event does not fire, first confirm the workbook is saved as .xlsm and macros are enabled (File > Options > Trust Center > Trust Center Settings > Macro Settings). Check that the code is in ThisWorkbook and that the procedure name is exactly Workbook_BeforeClose with the Cancel parameter. If you see a compile error, verify there are no duplicate procedure names. If the workbook closes without prompting, add a simple MsgBox at the top of the event to confirm it runs. If Excel hangs on close, look for code that calls Close recursively or performs lengthy operations; add a guard flag or move heavy work to a separate procedure. If the event fires when you open a copy, remember it runs for every close of that workbook, including programmatic closes from other macros.

Frequently Asked Questions

Can I cancel the close event?

Yes. Set the Cancel argument to True inside Workbook_BeforeClose. The workbook will remain open.

Does BeforeClose run when Windows shuts down?

It may run if Excel receives a normal close request, but forced shutdowns or crashes can skip it. Do not rely on it for critical data persistence.

Can I use BeforeClose to save the workbook automatically?

Yes, you can call ThisWorkbook.Save inside the event, but be careful: saving during close can trigger BeforeSave and may prompt the user again. Test thoroughly.

Why doesn't my BeforeClose code run when I close Excel entirely?

It should run for each open workbook as Excel closes them, unless the process is terminated abruptly. If it doesn't, check for macros being disabled or the workbook being read-only.

Can I have multiple BeforeClose procedures?

No. Each workbook can have only one Workbook_BeforeClose procedure in ThisWorkbook. Use a single procedure with conditional logic.

Does the event fire when the workbook is closed by another macro?

Yes, programmatic closes via Workbook.Close also trigger BeforeClose, unless the Close method is called with SaveChanges:=False and the workbook has no unsaved changes in some edge cases. Always test your specific scenario.