To close an Excel workbook with VBA and discard its unsaved changes, call the workbook’s Close method with SaveChanges:=False, as in ActiveWorkbook.Close SaveChanges:=False. Use a specific workbook reference when possible: it helps ensure the macro closes the intended file rather than whichever workbook happens to be active.
What SaveChanges:=False Does
Excel normally asks whether to save when you close a workbook that has unsaved changes. The SaveChanges argument tells the Workbook.Close method what to do with those changes. Set it to False to discard them; set it to True to save them. You can also omit the argument, in which case Excel may prompt the user if a save decision is needed.
This is a close operation, not an undo operation. It closes the selected workbook and discards its unsaved edits, including changes made by the macro before the close call. It does not revert the workbook to a particular earlier version while leaving it open. If you need to keep the workbook open and reverse selected edits, use appropriate undo logic or restore a saved copy instead.
The argument affects the workbook being closed. It does not automatically save or close other open workbooks, and it does not change the global save behavior of Excel. If the workbook has no unsaved changes, there is nothing to discard; the workbook simply closes.
Choose the Workbook You Mean to Close
The most common source of mistakes is using the wrong workbook reference. ActiveWorkbook means the workbook currently in the foreground. That can change when a macro opens another file, activates a different window, or performs other actions. A macro that uses ActiveWorkbook.Close may therefore close a workbook other than the one its author intended.
ThisWorkbook means the workbook that contains the VBA code. It is often the right choice for a macro stored in the workbook it should close. But if the code is in an add-in or PERSONAL.XLSB, ThisWorkbook refers to that file, not the report the user is working on.
For a known open file, refer to it by its workbook name, including the extension where applicable. For more complex automation, keep a Workbook object variable pointing to the file when you open it, then close that same object. This is generally more dependable than relying on whichever workbook is active at the end of a procedure.
Basic VBA Examples
This macro closes the active workbook and discards its unsaved changes. Before running it, save a copy of the workbook you are working with. Macros cannot be undone with Ctrl+Z, and closing without saving can permanently discard work that has not been saved elsewhere.
Sub DiscardActiveWorkbook()
Dim target As Workbook
' Capture the active workbook before closing it.
Set target = ActiveWorkbook
If target Is Nothing Then Exit Sub
target.Close SaveChanges:=False
End Sub
To close a workbook by name, use a name that matches the file shown in Excel’s title bar. This example checks whether the named workbook is open before trying to close it:
Sub DiscardNamedWorkbook()
Dim target As Workbook
' Look for this workbook among the open workbooks.
On Error Resume Next
Set target = Workbooks("QuarterlyReport.xlsx")
On Error GoTo 0
If target Is Nothing Then
MsgBox "That workbook is not open.", vbInformation
Exit Sub
End If
target.Close SaveChanges:=False
End Sub
If the procedure is stored in the workbook it closes, execution ends as that workbook closes. Do not put important follow-up instructions after the close call and expect them to run reliably. If later steps are required, place the controlling procedure in a different workbook or add-in, and make sure the target reference is still correct.
Closing Several Workbooks
For a batch task, decide explicitly which workbooks should close. Closing every open workbook is risky: a user may have unsaved work in another file, and Excel may also have hidden or utility workbooks open. A safer design uses a known list of target workbook names or closes only files opened by the macro.
When a macro opens a workbook for processing, retain the returned object rather than searching for it again later. For example, the result of Workbooks.Open can be assigned to a declared Workbook variable, and that variable can then be closed with SaveChanges:=False if the processing was meant to be temporary. This also avoids depending on workbook activation order.
If you do need to close a collection of workbooks, plan for the collection to change as files close. A loop that removes items while iterating through the same collection can behave unexpectedly. Close a carefully selected set, and test the procedure with copies before using it on important files.
Why Excel May Still Show a Prompt
When the correct workbook’s Close method is called with SaveChanges:=False, that argument is the direct instruction to discard changes. If a prompt still appears, first confirm that the code is closing the workbook you think it is. A prompt may come from a different workbook, a different operation, or code that calls Close without the argument.
Also check whether the macro is closing one workbook while another workbook has unsaved changes. The explicit argument applies only to the workbook on which the method is called. Excel can still ask about other files when they are closed manually or by later code. A workbook displayed in multiple windows can also affect how the close argument is handled, so avoid relying on a particular window when the same workbook is open in more than one window.
Finally, consider whether the prompt is actually a save prompt. Links, compatibility notices, or other Excel messages can appear during a workflow and are not necessarily controlled by SaveChanges. Identify which workbook and operation trigger the message before changing application-wide settings.
Should You Use DisplayAlerts = False?
Usually, no. Application.DisplayAlerts = False suppresses certain Excel alerts, but it is broader than the close method’s explicit save instruction. It can affect other operations performed while the setting is off, and Excel may choose a default response to a suppressed prompt. That can be dangerous when a macro also saves files, replaces files, or performs other actions that require a user’s decision.
For discarding changes on one workbook, prefer target.Close SaveChanges:=False. If a separate workflow genuinely requires alerts to be disabled, store the previous value of Application.DisplayAlerts and restore it afterward, including when an error occurs. Leaving alerts disabled can change how later code or a user’s actions behave in the same Excel session.
Do not use alert suppression as a substitute for selecting the right workbook. It can hide a warning without correcting a mistaken target, and it can make a destructive macro harder to notice.
Safety Checks Before Running the Macro
Closing without saving is intentionally destructive to unsaved edits. A short review before running a macro can prevent losing the wrong work:
- Save a separate copy of any workbook whose contents matter. Ctrl+S saves the active workbook on Windows; on Mac, use Command+S.
- Confirm the target reference: active workbook, code-containing workbook, or a specifically named workbook.
- Check that the workbook has the expected name and is open before closing it.
- Test the macro on a disposable copy, especially if it runs automatically or closes several files.
- Keep important work after the close call in a different workbook or in a procedure that does not depend on the closing workbook remaining open.
To edit VBA in desktop Excel for Microsoft 365 on Windows, open the Visual Basic Editor with Alt+F11, then insert or edit a procedure in a standard module as appropriate. On Mac, the Visual Basic Editor is available through the Developer tab or Tools > Macro > Visual Basic Editor; the keyboard shortcut can vary with the keyboard and function-key settings. Macros must be enabled for the code to run, and files containing macros should be saved in a macro-capable format such as .xlsm when the code needs to remain in the workbook.
Alternatives and Common Misconceptions
If you want to keep the edits, use Close SaveChanges:=True or save the workbook before closing it. If you want Excel to ask the user, omit the argument. These choices are different from setting SaveChanges:=False, which tells Excel to discard unsaved changes for that close operation.
You may also see code that sets Workbook.Saved = True before closing. That property changes Excel’s view of whether the workbook has unsaved changes; it does not write the edits to disk. It can suppress a save prompt, but it is easier to misunderstand and may cause changes to be lost without an explicit close instruction. For a deliberate discard, Close SaveChanges:=False communicates the intent more clearly.
Closing a workbook without saving is also different from deleting the file. The close method ends the open workbook session; it does not remove the workbook from its folder. Likewise, it does not delete an earlier saved version. If you need to recover overwritten or discarded work, check available backups or version history rather than expecting the close command to create a recovery copy.
Frequently Asked Questions
What is the exact VBA command to close a workbook without saving?
Use workbookVariable.Close SaveChanges:=False, replacing workbookVariable with a reference to the workbook you intend to close. For the active workbook, the short form is ActiveWorkbook.Close SaveChanges:=False.
Does this close the workbook without saving any changes made by the macro?
Yes. If those changes are unsaved when the workbook closes, SaveChanges:=False discards them along with other unsaved edits in that workbook. Save a copy first if you may need the changes.
How do I close the workbook that contains the macro?
Use ThisWorkbook.Close SaveChanges:=False when the VBA code is stored in the workbook you want to close. If the macro is in an add-in or PERSONAL.XLSB, ThisWorkbook points to that file instead.
Why does Excel still ask whether to save?
Check that the close call includes SaveChanges:=False and that it targets the workbook producing the prompt. The argument does not apply to other open workbooks or to unrelated alerts.
Can I close a workbook without saving it and keep it open?
No. Closing ends that workbook session. To keep it open, do not call Close; instead, use an appropriate undo or restore method for the changes you want to reverse.
Related
- Microsoft Excel Protect Sheet: Steps, Limits, and Tips
- Microsoft Excel Named Ranges: How They Work and Help
- Microsoft Excel CSV Import: Steps, Settings, and Fixes
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- 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…
- VBA Loop Through a PivotTable: Examples and TipsTo loop through a PivotTable in Excel VBA, assign it to a PivotTable variable and use a For…
- Excel Macros in Google Sheets: What Works?No: Google Sheets cannot run Excel VBA macros, even when you open an Excel workbook that contains them.…
- Excel VBA Copy: How to Copy Cells, Sheets, and FilesTo copy cells with Excel VBA, use Range.Copy with a destination, such as Worksheets("Sales").Range("A1:D20").Copy Destination:=Worksheets("Summary").Range("A1"). This copies the…
- VBA Error Handling: On Error, Cleanup, and Debugging TipsVBA error handling lets a macro respond when a statement fails instead of stopping unexpectedly. Use On Error…
- Excel VBA Resize: Change a Range’s Size with VBA CodeIn Excel VBA, use the Resize property to refer to a range with a different number of rows…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.