Excel VBA: Close Workbook Without Saving

In this article, I’ll show you how you can use Excel VBA to close a workbook without saving.


Excel VBA: Close Workbook Without Saving (Quick View)

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


Download Practice Workbook

Download this practice workbook to exercise while you are reading this article.


An Overview to Close Workbook Without Saving Using Excel VBA (Step-by-Step Analysis)

Here, I’ve got a workbook called “Close Workbook Without Saving.xlsm”.

Workbook to Close Workbook Without Saving Using Excel VBA

Our objective is to develop a VBA code to close the workbook without saving. I am showing you the step-by-step analysis of the code for your better understanding.

⧪ Step 1: Taking Necessary Input

First, we must insert the necessary input into the code. There is only one input required in this code. That is the name of the workbook that we’ll close.

Here it’s “Close Workbook Without Saving.xlsm”.

Book_Name = "Close Workbook Without Saving.xlsm"

Taking Input to Close Workbook Without Saving Using Excel VBA

⧪ Step 2: Closing the Workbook

This is the most important step. 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.

Therefore, 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

⧪ Step 3: Alternative Solution in Case of Error

Finally, we’ll fix the alternative solution in case any error actually raises. The solution is nothing but 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

Therefore, 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

We’ve seen the step-by-step analysis of the code to close a workbook without saving using VBA.

Now let’s see how to develop a Macro to run the code.

⧪ Step 1: Opening the VBA Window

Press ALT + F11 on your keyboard to open the Visual Basic window.

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

⧪ Step 3: Putting VBA Code

This is the most important step. Insert the given VBA code in the module. Change the name of the workbook to the one you require.

⧪ Step 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. To know them in detail, you can visit this link.

Conclusion

Therefore, this is the process to close a workbook without saving using Excel VBA. Do you have any questions? Feel free to ask us. And don’t forget to visit our site ExcelDemy for more posts and updates.

Rifat Hassan

Rifat Hassan

Hello! Welcome to my profile. Here I will be posting articles related to Microsoft Excel. I am a passionate Electrical Engineer holding a Bachelor’s degree in Electrical and Electronic Engineering from the Bangladesh University of Engineering and Technology. Besides academic studies, I always love to keep pace with the revolution in technology that the world is rushing towards day by day. I am diligent, career-oriented, and ready to cherish knowledge throughout my life.

We will be happy to hear your thoughts

Leave a reply

ExcelDemy
Logo