Excel VBA: Close Workbook Without Saving

⧪ 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"

Taking Input to Close Workbook Without Saving Using Excel VBA


⧪ 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

Handling Error to Close Workbook Without Saving Using Excel VBA

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

VBA Code to Close Workbook Without Saving in Excel VBA


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.

Inserting Module to Close Workbook Without Saving Using Excel VBA

⧪ 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.


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Rifat Hassan
Rifat Hassan

Rifat Hassan, BSc, Electrical and Electronic Engineering, Bangladesh University of Engineering and Technology, has worked with the ExcelDemy project for almost 2 years. Within these 2 years, he has written over 250 articles. He has also conducted a few Boot Camp sessions on effective coding, especially Visual Basic for Applications (VBA). Currently, he is working as a Software Developer to develop and deploy additional add-ins to enhance the customers with a more sophisticated experience with Microsoft Office Suits,... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo