⧪ Method 1 – Taking Necessary Input
Insert the necessary input into the code. Only one input is required in this code: the name of the workbook that we’ll close.
It’s “Close Workbook Without Saving.xlsm”.
Book_Name = "Close Workbook Without Saving.xlsm"
⧪ Method 2 – Closing the Workbook
We’ll close the workbook using the Close method of VBA. To close it without saving, we’ll set the SaveChanges parameter of the Close method to False.
But there may be cases while you are trying to close the workbook in VBA, but the workbook is already closed. In those cases, VBA will raise an error.
To protect the code from any possible error, we’ll wrap it with an alternative solution and point it to move to that solution if necessary.
On Error GoTo Message
Workbooks(Book_Name).Close SaveChanges:=False
⧪ Method 3 – Alternative Solution in Case of Error
We’ll fix the alternative solution in case any error raises. The solution is a simple message stating that the workbook is already closed.
Message:
MsgBox "The Workbook is Already Closed", vbExclamation
The complete VBA code will be:
⧭ VBA Code:
Sub Close_Workbook_Without_Saving()
Book_Name = "Close Workbook Without Saving.xlsm"
On Error GoTo Message
Workbooks(Book_Name).Close SaveChanges:=False
Message:
MsgBox "The Workbook is Already Closed", vbExclamation
End Sub
Developing the Macro to Close a Workbook Without Saving Using Excel VBA
⧪ Method 1 – Opening the VBA Window
Press ALT + F11 on your keyboard to open the Visual Basic window.
⧪ Method 2 – Inserting a New Module
Go to Insert > Module in the toolbar. Click on Module. A new module called Module1 (or anything else depending on your past history) will open.
⧪ Method 3 – Putting VBA Code
Insert the given VBA code in the module. Change the name of the workbook to the one you require.
⧪ Method 4 – Running the Code
Click on the Run Sub / UserForm tool from the toolbar above.
The workbook will close without saving the last changes.
Things to Remember
- The Workbook.Close method of VBA uses a total of 3 parameters. We’ve used only the first one, the SaveChenges parameter. Along with it, it used 2 more parameters called FileName and RouteWorkbook.
Download Practice Workbook
Download this practice workbook to exercise while you are reading this article.