Close Workbook Without Saving

Maintained on

When using VBA, there are often cases where you want to retrieve data from another Excel file.

In such cases, it is common to use the Workbooks.Open method to open Excel and retrieve data, but what about closing the file?

If you only need to reference the file, it is sufficient to simply close it without saving.

Unnecessarily saving the file can lead to accidentally overwriting data.

This time, we will explain how to close a workbook without saving using VBA.

Code to Use

By specifying saveChanges as False in the Close method of the Workbook class, you can close without saving.

_Close_Workbook_Without_Saving
Workbook.close saveChanges:=False

Sample Code

Close the Active Workbook Without Saving

This can be used when you need to close a temporarily opened workbook without saving, such as when reflecting the results of a macro in another workbook.

_Close_Active_Workbook_Without_Saving
ActiveWorkbook.Close savechanges:=False

Close a Specific Excel File Without Saving

This can be used when closing a file that does not need to be saved, such as a temporary file automatically generated by a macro.

_Close_Specific_Excel_File_Without_Saving
Dim wb As Workbook
Set wb = Workbooks("Book1.xlsx")

wb.close saveChanges:=False

Close All Excel Files Without Saving

This will forcibly close all files. Note that data may be lost as changes are not saved. Save as needed during the process.

_Close_All_Excel_Files_Without_Saving
Dim wb As Workbook
For Each wb In Application.Workbooks
    wb.Close savechanges:=False
Next wb

Use Cases

This method can be useful in various situations, such as handling temporary files or avoiding saving incorrect data. For example, it can be used in the following cases:

  • When generating temporary files during macro processing
  • When you accidentally edit data while the file is open
  • When you want to assign a shortcut key to close the file without saving
#Excel #VBA