When you close a workbook in VBA using .Close, if there are unsaved changes, Excel displays a confirmation message: “Do you want to save your changes?” This causes the macro to pause and wait for a user response, which prevents full automation.
In this article, I will explain how to use the SaveChanges argument of the .Close method to completely suppress this message and automatically save or discard changes.
The SaveChanges Argument of Workbook.Close
The Close method has a very important argument called SaveChanges. By setting this to True or False, you can control the saving behavior.
| Value of SaveChanges | Behavior |
| True | Automatically overwrites and saves changes, then closes without a message. |
| False | Automatically discards changes, then closes without a message. |
| Omitted | Displays the “Do you want to save changes?” confirmation (The macro pauses). |
Completed VBA Code
1. Save Changes and Close Without Message
This code automatically saves changes made to the workbook and closes it without any confirmation message.
Sub SaveAndCloseBook()
Dim targetBook As Workbook
Set targetBook = ActiveWorkbook
' Save changes and close
' Note: If this is a new workbook that has never been saved,
' the "Save As" dialog will appear.
targetBook.Close SaveChanges:=True
End Sub
2. Discard Changes and Close Without Message
This code discards all changes made to the workbook and closes it without any confirmation message.
Sub DiscardAndCloseBook()
Dim targetBook As Workbook
Set targetBook = ActiveWorkbook
' Discard changes and close
targetBook.Close SaveChanges:=False
End Sub
[Advanced] Robust Code Considering Unsaved Workbooks
While SaveChanges:=True is convenient, if you use it on a new workbook that has never been saved (e.g., “Book1”), the “Save As” dialog will appear, stopping the macro.
To avoid this, checking “if the workbook has ever been saved” before deciding the action is a safer and more reliable method.
Code and Explanation
VBA
Sub SmartCloseBook()
Dim bookToClose As Workbook
Set bookToClose = ActiveWorkbook
' Check the save history using the .Path property
If bookToClose.Path <> "" Then
' Workbook with a save history -> Save changes and close
bookToClose.Close SaveChanges:=True
MsgBox "Saved changes and closed the workbook."
Else
' New workbook (e.g., "Book1") -> Discard changes and close
bookToClose.Close SaveChanges:=False
MsgBox "Discarded changes to the new workbook and closed it."
End If
End Sub
If bookToClose.Path <> "" Then: The .Path property returns the folder path where the workbook is saved. For a workbook that has never been saved, this property is an empty string (""). We use this to determine if the workbook is new or existing.
Summary
To close a workbook without a confirmation message, use the SaveChanges argument of the .Close method properly.
- To save changes and close:
myBook.Close SaveChanges:=True - To discard changes and close:
myBook.Close SaveChanges:=False - To handle new workbooks safely: Check the
.Pathproperty to branch the logic.
By mastering the SaveChanges argument, you can fully automate VBA processes involving file opening and closing for smooth execution.
