To close a workbook with VBA, call the workbook’s Close method and state whether to save changes: for example, ThisWorkbook.Close SaveChanges:=True saves the workbook that contains the macro, while ThisWorkbook.Close SaveChanges:=False closes it without saving. Choose the workbook deliberately; ThisWorkbook and ActiveWorkbook can refer to different files.
How the Workbook Close Method Works
In Excel VBA, a workbook is an object, and Close is the method that closes that workbook. The method can be called on a workbook variable, on a workbook selected from Excel’s Workbooks collection, or directly on ThisWorkbook or ActiveWorkbook. The optional SaveChanges argument controls what happens to unsaved changes:
SaveChanges:=Truesaves changes to the workbook’s existing file before closing it.SaveChanges:=Falsediscards changes and closes the workbook without saving them.- If you omit the argument, Excel may ask whether to save when the workbook has unsaved changes.
Use a named argument such as SaveChanges:=True rather than relying on the position of an optional argument. It makes the decision visible in the code and easier to review later. Closing a workbook does not, by itself, close the Excel application or any other open workbook.
Choose the Correct Workbook Reference
The most important choice is which workbook object to close. ThisWorkbook means the workbook containing the VBA code currently running. ActiveWorkbook means the workbook that is active in Excel at that moment. They are often the same in a simple macro, but they are not guaranteed to be. For example, a macro stored in an add-in or in PERSONAL.XLSB can act on a separate workbook that the user has selected.
If the macro should close its own file, ThisWorkbook is usually the clearest reference. If it should close the workbook the user is working in, ActiveWorkbook may be appropriate, but check that it is not the macro’s controller workbook. A workbook can also be selected by its file name through the Workbooks collection. The name must match an open workbook’s name, including its file extension where applicable.
For workbooks with names that may be confused, assign the intended object to a variable and use that variable throughout the procedure. This reduces the chance that a change in the active window causes the macro to close the wrong file.
Decide Whether to Save Before Closing
Saving and closing are separate decisions. Close SaveChanges:=True saves edits to the workbook’s current location and then closes it; it is not a replacement for SaveAs and does not let you choose a new file name or folder. If the workbook has never been saved, is read-only, or cannot be saved to its current location, Excel may require another action or report a problem rather than silently saving as a new file.
Use SaveChanges:=False only when discarding edits is intended. It is useful for closing a temporary workbook that a macro opened only to read data, but it can permanently lose user changes if applied to an edited file. If you want to save first and then close, you can call Save explicitly; that makes the sequence clear and allows you to handle a save error before attempting to close.
- Use
Truewhen you intend to keep changes in the existing file. - Use
Falsewhen you have confirmed that changes should be discarded. - Leave the argument out only when you specifically want Excel’s normal save-confirmation behavior.
VBA Examples for Common Cases
The first example closes the workbook containing the macro and saves its changes. Because a macro cannot be undone with Ctrl+Z, save a copy of the workbook before running a macro, especially one that closes files or discards edits.
Sub CloseThisWorkbookAndSave()
Dim wb As Workbook
' Refer to the workbook that contains this code.
Set wb = ThisWorkbook
wb.Close SaveChanges:=True
End Sub
To close the current active workbook without saving its changes, use the following only after confirming that the active workbook is the intended target. This version deliberately discards unsaved edits.
Sub CloseActiveWorkbookWithoutSaving()
Dim wb As Workbook
' The active workbook can differ from the workbook containing this macro.
Set wb = ActiveWorkbook
wb.Close SaveChanges:=False
End Sub
If a macro stored in a separate controller workbook should close a particular open file, refer to its name. This example saves the named workbook before closing it; replace the example name with the exact name shown in Excel’s workbook window.
Sub CloseNamedWorkbookAndSave()
Dim wb As Workbook
' The named workbook must already be open.
Set wb = Workbooks("QuarterlyReport.xlsx")
wb.Close SaveChanges:=True
End Sub
If the named workbook is not open, that assignment raises an error. In a production macro, handle that possibility or search the open workbooks before calling Close. Do not use an error-suppression statement simply to hide the problem: it can also conceal unrelated errors and make it unclear whether the file actually closed.
Prompts, DisplayAlerts, and Safe Automation
When you provide SaveChanges:=True or SaveChanges:=False, Excel generally does not need to ask the ordinary save-or-discard question for that workbook. This is safer and more predictable than turning off all application alerts. The setting Application.DisplayAlerts affects prompts more broadly, not just the save question for one workbook, so suppressing alerts can cause Excel to accept a default response to another dialog.
For unattended batch processing, developers sometimes set Application.DisplayAlerts = False temporarily. If you do, preserve its previous value and restore it even when an error occurs. Keep the alert-suppression period as short as possible, and make the intended save choice explicit on each Close call. A controller macro in another workbook is a suitable place for operations that must continue after a target workbook closes.
Another misconception is that setting DisplayAlerts to False means every operation is safe. It does not validate the file name, preserve discarded edits, or guarantee a successful save. Decide what should happen to the data first, and use explicit error handling if a failed save or close needs to be reported to the user.
Close a Workbook or Quit Excel?
Workbook.Close closes one workbook. It leaves Excel running and leaves other open workbooks alone. By contrast, Application.Quit asks Excel to exit, which can affect every workbook open in that Excel application. Excel may prompt about unsaved changes in other files unless the macro has explicitly handled them or alerts have been changed.
Use Application.Quit only when the requirement is genuinely to exit Excel, not merely to close the file a macro processed. If you need to close several specific workbooks, close each one by reference and make a save decision for each. Avoid using ActiveWorkbook repeatedly in a loop if closing one file could change which workbook is active; keep explicit workbook references instead.
Run the Macro and Check Platform Limits
In Excel for Microsoft 365 on Windows, save the file in a macro-enabled format such as .xlsm if it needs to retain VBA code. Open the Visual Basic Editor with Alt+F11, put the procedure in a standard module when appropriate, and run it from the editor or from Excel’s macro interface. Macro security settings may require you to enable macros for a trusted file. A macro that closes the workbook it lives in is often easier to control when launched from a separate workbook or add-in, especially if further code must run afterward.
Excel for Mac desktop also supports VBA and the workbook Close method, although some Windows-specific automation features and interfaces differ. The basic close syntax and the distinction between ThisWorkbook and ActiveWorkbook remain relevant. Excel for the web does not run VBA macros; open the workbook in the desktop app to run VBA. For manual closing, Windows commonly uses Ctrl+W, while Mac uses Command+W; these are keyboard actions, not VBA commands.
Common Problems and How to Avoid Them
- The wrong workbook closes: Replace an ambiguous
ActiveWorkbookreference withThisWorkbookor a specific workbook variable when that better expresses the intended target. - Excel still asks about saving: Check that the macro calls
Closeon the workbook you think it does and that theSaveChangesargument is supplied as intended. - The macro reports that a workbook cannot be found: Confirm the workbook is open and that its name matches, including the extension. A path is not generally the same thing as the workbook’s collection name.
- Changes disappear: Review every use of
SaveChanges:=False. It explicitly tells Excel not to save the workbook’s current edits. - The procedure stops at the close line: Closing the workbook that hosts the running code can interrupt the workflow that depends on that file. Put follow-up logic in a separate controller workbook or another suitable automation layer.
Before running a closing macro on important work, test it on a copy with representative open workbooks and both saved and unsaved changes. This simple check can reveal a mistaken target reference or an unintended save decision before it affects the original file.
Frequently Asked Questions
How do I close a workbook without saving in VBA?
Call its Close method with SaveChanges:=False, such as ActiveWorkbook.Close SaveChanges:=False. Confirm the reference points to the intended workbook because this option discards its unsaved changes.
What is the difference between ThisWorkbook and ActiveWorkbook?ThisWorkbook is the file containing the running VBA code. ActiveWorkbook is the workbook currently active in Excel, which can be another file. Choose based on which workbook you intend to close.
Does closing a workbook with VBA close Excel?
No. Workbook.Close closes the specified workbook only. Application.Quit is the method associated with exiting Excel and can affect other open workbooks.
How can I close a workbook by its file name?
Use the open workbook’s name in the Workbooks collection, for example Workbooks("QuarterlyReport.xlsx").Close SaveChanges:=True. The workbook must be open, and its name must match exactly.
Can VBA close a workbook in Excel for the web?
No. Excel for the web does not execute VBA macros. Open the file in Excel for Windows or Mac desktop to run a VBA procedure that closes a workbook.
Related
- Excel Stop Showing Page Break Lines: Fixes
- Excel Split Cell Without Adding a Column?
- Excel Split Cell Color in Half: What Works
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA: Delete a Row If a Cell Is BlankYes. In Excel VBA, loop through the rows from the bottom upward and delete a row when the…
- Text Macro Examples for Excel: Practical VBA TasksText macro examples for Excel include cleaning extra spaces, changing text to uppercase, adding a prefix, and replacing…
- Excel VBA Case Statement: How Select Case WorksExcel VBA does not have a separate Case statement; it uses Select Case ... End Select to choose…
- Excel VBA: Find the Last Row of Data in a Column EasilyTo find the last populated row in column A with Excel VBA, use Cells(Rows.Count, "A").End(xlUp).Row, with the worksheet…
- Excel Macros Disabled: How to Enable Them SafelyTo enable a macro in Excel for Microsoft 365 on Windows, open a workbook you trust and select…
- VBA Loop Array: Examples, Bounds, and Best PracticesA VBA array loop visits array elements one at a time, usually with a For loop using LBound…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.