VBA BeforeClose Event: Stop Closing With Unsaved Entries
The VBA beforeclose event lets a macro run the moment before a workbook actually closes — and, critically, lets it cancel the close entirely. That is the difference between a reconciliation file that silently loses unposted entries and one that refuses to.

ACA | FMVA® | 19 Years in Finance
This guide is part of the FinDataPro Excel VBA Workbook Events series.
What Is Workbook_BeforeClose?
Paste this into the ThisWorkbook module:
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim wsRec As Worksheet
Set wsRec = ThisWorkbook.Sheets("Reconciliation")
' Named cell "PostStatus" is "Posted" once entries are cleared
If wsRec.Range("PostStatus").Value <> "Posted" Then
If MsgBox("This file has unposted entries. Close anyway?", _
vbYesNo + vbExclamation) = vbNo Then
Cancel = True
End If
End If
End SubSet Cancel = True and Excel behaves as if the user never clicked Close. Nothing closes, nothing is lost.
Download the Example Workbook — Free
The working .xlsm file with this Workbook_BeforeClose guard already wired up — open it, inspect the code in ThisWorkbook, and adapt the status check to your own file.
Where the Code Goes
Like every workbook-level event, Workbook_BeforeClose only fires from the ThisWorkbook object, never a standard module. It pairs naturally with Workbook_Open — one bookends the file's startup, the other its shutdown.
Finance Use Case: Guarding Unposted Journal Entries
A month-end reconciliation file passed between two or three preparers is exactly where this matters. Someone finishes their section, gets pulled into a meeting, and closes the file without posting. The next person opens a file that looks complete but is not. A status flag checked in BeforeClose turns that silent gap into an explicit prompt at the one moment it can still be fixed — before the file closes.
The alternative — hoping every preparer remembers to check a status cell before closing — fails precisely when it matters most, in the rushed final hour before a close deadline. A hard-coded check inside the event itself does not rely on anyone remembering anything: it fires automatically on every single close attempt, whether the file has been open for five minutes or five hours, and whether the person closing it is the original preparer or someone covering for them at short notice.
Cancel the Close vs. Force a Save
Two different problems need two different fixes. If unposted work must never leave unsaved, set Cancel = True and stop the close. If the goal is simply to guarantee a save happens, call ThisWorkbook.Save directly inside the event instead — the close still proceeds, but nothing is lost.
Common Mistakes
- Forgetting that
Cancelis a parameter, not a return value — you must assign it, not just check a condition. - Blocking every close unconditionally, which trains users to disable macros to escape the file.
- Not testing what happens when Excel itself is closed with the file open, not just the file's own Close button.
- Relying on
Application.DisplayAlerts = Falseto hide your own MsgBox — it only suppresses Excel's built-in prompts, not custom ones.
Related Events on This Series
If the same file also needs to react to live data entry, see Worksheet_Change for flagging cells as they are edited, or GetOpenFilename for a safer way to import the bank statement in the first place.
Full reference: Microsoft Learn Workbook.BeforeClose event reference.
Skip the manual reconciliation checklist entirely
FinDataPro AutoBankRec matches your bank statement against the ERP bank book in under 10 seconds, so there is nothing left unposted to guard against at close.
See AutoBankRecFrequently Asked Questions
What does Cancel = True actually do?
It stops the close entirely — Excel behaves as if the user never clicked Close. The workbook stays open and no further close events fire.
Does BeforeClose fire if I quit Excel entirely?
Yes — closing the file, quitting Excel with the file open, or closing via Workbooks.Close from another macro all trigger it. Cancel = True blocks all three.
Can I force a save instead of blocking the close?
Yes — call ThisWorkbook.Save inside the event before the close continues, rather than cancelling it outright.
Will DisplayAlerts suppress my own custom warning too?
No. Application.DisplayAlerts only suppresses Excel's built-in prompts. A MsgBox you write yourself still displays.
Conclusion
Workbook_BeforeClose is the one place a shared finance file can catch an incomplete close before it happens. Add the status check to ThisWorkbook, decide whether to cancel or force-save, and pair it with Workbook_Open so the file protects itself on both ends. If reconciliation itself is the bottleneck, see AutoBankRec.

Prashant Panchal is a Chartered Accountant (ACA) and Financial Modelling & Valuation Analyst (FMVA®) with 19 years of experience in finance, FP&A, and financial modelling across the GCC region. He is the founder of FinDataPro.
Discussion
Leave a Comment
Comments are moderated and appear once approved.
