To close an Excel workbook without a save prompt, specify what should happen to unsaved changes: use Workbook.Close SaveChanges:=False to discard them, or Workbook.Close SaveChanges:=True to save them. The explicit argument is usually clearer and safer than suppressing Excel alerts globally.
Why Excel asks whether to save
Excel normally asks whether to save when a workbook has changed since its last save. Changes can include editing cells, changing formatting, refreshing data, or making certain workbook-level changes. The prompt gives the person closing the file a chance to keep or discard those edits.
A macro can make that choice for the user. The SaveChanges argument on the workbook’s Close method tells Excel which action to take, so it does not need to ask the usual save question. This is different from disabling every alert in Excel: it gives a specific instruction for one workbook close operation.
Closing and saving are separate decisions. A workbook can be closed without saving, saved and then closed, or left open after saving. Choosing the wrong option can lose work, so decide whether the macro is meant to discard changes or preserve them before using a silent close.
Close a workbook and discard its changes
In Excel for Microsoft 365 on Windows, place a macro like this in a standard VBA module. It closes the active workbook and tells Excel not to save changes:
Sub CloseActiveWorkbookWithoutSaving()
Dim wb As Workbook
Set wb = ActiveWorkbook
If wb Is Nothing Then Exit Sub
' Discard unsaved changes and close this workbook.
wb.Close SaveChanges:=False
End Sub
ActiveWorkbook means the workbook currently in the foreground. That may not be the file you intended to close, especially if several workbooks are open or the macro runs from an add-in. For a macro that always targets a particular open file, identify it by name instead:
Sub CloseNamedWorkbookWithoutSaving()
Dim wb As Workbook
' The named workbook must already be open.
Set wb = Workbooks("Monthly Report.xlsx")
wb.Close SaveChanges:=False
End Sub
Change the filename to the exact workbook name, including its extension. If that workbook is not open, the lookup raises an error. A production macro can check the Workbooks collection first and report that the file is unavailable rather than stopping unexpectedly.
Save changes and close without asking
If the goal is to keep the latest edits, pass True instead. Excel saves the workbook and closes it without showing the ordinary save-changes prompt:
Sub SaveAndCloseActiveWorkbook()
Dim wb As Workbook
Set wb = ActiveWorkbook
If wb Is Nothing Then Exit Sub
' Save changes before closing.
wb.Close SaveChanges:=True
End Sub
Use this when the macro is responsible for saving the user’s work, such as after generating a report. If you need to control the save location or filename, save explicitly with an appropriate method before closing; SaveChanges:=True is not a substitute for a deliberate Save As operation. A workbook that cannot be saved normally, for example because of its location or permissions, may still require handling for that separate issue.
Leaving out SaveChanges is not a dependable way to silence the prompt. When the workbook has unsaved changes, Excel may ask the user what to do. State the intended outcome explicitly so the macro behaves consistently.
When to use Application.DisplayAlerts
Application.DisplayAlerts = False suppresses many Excel alerts while VBA runs, but it is broader than setting SaveChanges. Excel can choose a default response when an alert is suppressed, and that response is not always the choice a person would make. For a simple close, prefer wb.Close SaveChanges:=False or wb.Close SaveChanges:=True.
There are situations where a macro already needs to suppress other Excel alerts. If so, save the previous setting and restore it, including when an error occurs. This example targets the active workbook and discards its unsaved changes:
Sub CloseWithoutAlerts()
Dim wb As Workbook
Dim previousAlerts As Boolean
Dim errorNumber As Long
Dim errorDescription As String
Set wb = ActiveWorkbook
If wb Is Nothing Then Exit Sub
previousAlerts = Application.DisplayAlerts
On Error GoTo CloseFailed
Application.DisplayAlerts = False
wb.Close SaveChanges:=False
Application.DisplayAlerts = previousAlerts
Exit Sub
CloseFailed:
errorNumber = Err.Number
errorDescription = Err.Description
Application.DisplayAlerts = previousAlerts
Err.Raise errorNumber, "CloseWithoutAlerts", errorDescription
End Sub
Restoring the earlier value matters because other macros or code that runs afterward may depend on alerts being enabled. The explicit save choice still does the main work here; turning alerts off is not a universal fix for every dialog, add-in window, or custom form that might interrupt automation.
Choose the right workbook object
Many accidental closes happen because code targets the wrong workbook. ActiveWorkbook is whichever workbook is active at that moment. ThisWorkbook is the workbook that contains the running VBA code. They are often different when a macro is stored in Personal.xlsb, an add-in, or a separate controller workbook.
- Use
ActiveWorkbookwhen the user’s currently selected workbook is intentionally the target. - Use
ThisWorkbookwhen the file containing the macro is the target. - Use a workbook name or a stored workbook reference when the macro must close a specific open file.
For example, a macro stored in the workbook it closes can use ThisWorkbook.Close SaveChanges:=False. But it is usually easier to manage a process when the controlling macro lives in a different workbook or add-in and closes the target only after its work is complete. That reduces the risk of closing the code host before the procedure has finished its cleanup.
Common reasons a close still does not happen
Passing SaveChanges handles the standard decision about saving workbook edits. It does not guarantee that every workbook will close under every condition. Check these common causes if the file remains open or automation stops:
- A close event cancels the operation. A workbook’s
BeforeCloseevent can set itsCancelargument toTrue. Review event code if the workbook refuses to close. Disabling events can bypass workbook event procedures, but it may also skip important cleanup; do not switch events off casually. - The macro targets the wrong file. Check whether the intended workbook is
ActiveWorkbook,ThisWorkbook, or another open workbook. - The file is read-only or has a save-location issue. Discarding changes with
Falseshould not require saving them. Saving withTruecan run into permissions, path, or file-format problems that are separate from the save prompt. - A different dialog is appearing. Excel alerts, an add-in prompt, a custom form, and a workbook event are not all the same thing. Identify the actual interruption before trying to suppress it.
A workbook may also have more than one window open. Closing a workbook object closes that workbook, not merely the visible window. If the task is to hide or close only a particular window, use a window-level approach rather than treating it as a workbook close.
Safety, AutoSave, and lost changes
SaveChanges:=False discards changes that have not been saved to the workbook. It does not necessarily undo changes that were already saved earlier. In Microsoft 365, a file stored in OneDrive or SharePoint may have AutoSave enabled, so some edits could already have synchronized before the macro closes the workbook. A silent close is not a reliable way to roll back those saved edits.
Before running a macro that discards work, save a copy of the workbook. VBA actions cannot be undone with Ctrl+Z, and closing a workbook without saving can remove edits that are not recoverable from another copy or version. This is especially important when testing code: try it on a disposable copy first, and confirm the target workbook before running it on important files.
If the purpose is to close a workbook after exporting data, check that the export succeeded before discarding changes. If the purpose is to close a temporary workbook created by the macro, make that temporary file’s status clear in the code. A clear target and an explicit save decision are more dependable safeguards than relying on a hidden prompt.
Windows, Mac, and Excel for the web
The VBA Workbook.Close method and its SaveChanges argument are also used in Excel for Mac, although the VBA editor and some interface details differ from Windows. The examples here do not depend on a Windows-only keyboard shortcut. Excel for the web does not run VBA macros, so run this code in the desktop Excel application; opening a macro-enabled workbook in a browser does not execute its VBA.
Frequently Asked Questions
How do I close a workbook without saving in VBA?
Use wb.Close SaveChanges:=False, where wb refers to the workbook you want to close. This discards unsaved changes and avoids the standard save prompt.
How do I save and close a workbook without a prompt?
Use wb.Close SaveChanges:=True. Make sure the workbook can be saved to its existing location, and use an explicit save operation first if you need to control the filename or destination.
What is the difference between ThisWorkbook and ActiveWorkbook?ThisWorkbook contains the running macro. ActiveWorkbook is the workbook currently in the foreground. They can refer to different files, so select the one that matches the intended target.
Can Application.DisplayAlerts replace SaveChanges?
It can suppress many alerts, but it is broader and may cause Excel to choose a default response. For the save decision when closing a workbook, explicitly use SaveChanges:=False or SaveChanges:=True.
Why does a workbook stay open after I call Close?
Check for a BeforeClose event that cancels closing, a wrong workbook reference, or a separate dialog or add-in interruption. The SaveChanges argument only resolves the normal save-changes question.
Related
- OneDrive Excel File Corrupted? Practical Repair Steps
- Excel Shortcut Keys: Essential Windows and Mac Keys
- Multi-Level Dropdown List in Excel: How to Create One
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Find Position in a String with InStr ExamplesIn Excel VBA, use InStr to find the position of one string inside another: InStr(1, textValue, searchText, vbTextCompare)…
- Open Workbook VBA: Excel Methods and ExamplesTo open an Excel workbook with VBA, call Workbooks.Open and provide its full file path. For example, assign…
- VBA MsgBox New Line: How to Add Line BreaksTo add a new line to a VBA MsgBox message, join the text with vbCrLf (or vbNewLine). For…
- Excel VBA: Copy an Array to a Worksheet Range EasilyTo copy a two-dimensional VBA array to cells, assign it to a worksheet range whose size matches the…
- VBA Find Exact Match: Use Find with xlWholeTo find a cell whose entire contents match a value in Excel VBA, use Range.Find with LookAt:=xlWhole. For…
- VBA Code to Delete a Row in ExcelTo delete a worksheet row with VBA, use Rows(5).Delete for row 5. In a macro, qualify the row…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.