VBA Close Excel File – Workbook Example Code

Home/VBA/VBA Close Excel File – Workbook Example Code

VBA code to close the excel file example will help us to close the excel file. We can use Close method of Workbook to close the file. In this example we will see how to close the Active Excel Workbook using VBA. VBA code for closing Active Workbook macro should work for all the version of Microsoft Excel 2003, Excel 2007, Excel 2010, and Excel 2013.

PREMIUM TEMPLATES LIMITED TIME OFFER

ON SALE80% OFF

BROWSE ALL TEMPLATES

50+ Project Management Templates Pack
Excel PowerPoint Word

VIEW DETAILS

Advanced Project Plan & Portfolio Template
Excel Template

VIEW DETAILS

Ultimate Project Management Template
Excel Template

VIEW DETAILS

20+ Excel Project Management Pack
Excel Templates

VIEW DETAILS

20+ PowerPoint Project Management Pack
PowerPoint Templates

VIEW DETAILS

10+ MS Word Project Management Pack
Word Templates

VIEW DETAILS


Close Excel File

VBA code to close the excel file


Here is the Example VBA syntax and Example VBA Macro code to close the Excel File. This will help you to how to close active Excel workbook using VBA.

VBA Close Excel File: Syntax


Following is the VBA Syntax and sample VBA code to close the Excel File using VBA. We are using the Close method of the Excel Workbook object.


Workbook.Close

Here workbook can be ActiveWorkbook, ThisWorkbook or a workbook assigned to an object.
ActiveWorkbook.Close will Close the currently active Excel Workbook. And ThisWorkbook.Close will close the workbook which we have written our VBA code. And we can set the workbook to any object and we can close it using WorkbookObject.Close method as shown below:

Dim wb as Workbook
Set wb=Thisworkbook
Wb.Close

Here is explained VBA code:
‘Declaring the wb object as Workbook, in the next statement we will assign the workbook to this object.
Dim wb As Workbook

‘Assigning Thisworkbook to workbook object i.e.; wb.
Set wb = ThisWorkbook

‘And closing the workbook using workbook object which we have created above and Close method of the workbook.
wb.Close

Like this we can assign any workbook to an object. And then we can close the Excel Workbook using Object in VBA.

Close Excel File using VBA : Examples


The following VBA code is to Close the Excel File. This code will close the Workbook which we have this VBA procedure.

Sub sbVBS_To_CloseExcelFile()
    ThisWorkbook.Close
End Sub

Instructions to run the VBA code to close the excel file


Please follow the below steps to execute the VBA code to close the excel file.
Step 1: Open any existing Excel workbook
Step 2: Press Alt+F11 – This will open the VBA Editor
Step 3: Insert a code module from then insert menu
Step 4: Copy the above code and paste in the code module which have inserted in the above step
Step 5: Do some changes and save the file
Step 6: Now press F5 to execute the code

Now you can observe that your Excel workbook is closed with the changes.

Explained VBA Code to Close the Excel File :


Starting procedure to write VBA code to Close Excel File using VBA.
Sub sbVBS_To_CloseExcelFile_C()

‘To close ThisWorkbook, i.e the workbook with the code module and this procedure. We are using Close method of the workbook to close the excel file.
ThisWorkbook.Close

‘Ending sub procedure to close the Excel workbook
End Sub

Here ThisWorkbook object represent the workbook with this code module. And Close is the Workbook method to Close the Excel File. We will see the other examples to close the Workbook in different situations in the following examples.

VBA to Close Active Excel Workbook: Examples


The following VBA code is to Close the Active Excel Workbook. This code will close the Workbook which is currently active.

Sub sbVBS_To_CloseActiveExcelWorkbook()
    ActiveWorkbook.Close
End Sub

VBA to Close any Workbook: Examples


The following VBA code is to Close any Excel Workbook. This code will close the Workbook usin object..

Sub sbVBS_To_CloseExcelWorkbook()
 Dim wb as Workbook
Set wb=ThisWorkbook ‘here you can set any workbook
    wb.Close
End Sub

VBA to Save and Close Excel Workbook: Examples


The following VBA code is to Save and Close any Excel Workbook. This code will close the Workbook usin object.

Sub sbVBS_To_SaveCloseExcelWorkbook()
 Dim wb as nWorkbook
Set wb=ThisWorkbook ‘here you can set any workbook
    Wb.Save
    wb.Close
End Sub
LIMITED TIME OFFER - Get it Now!
Advanced Project Plan Excel Template

 
Related Resource External VBA Reference
By |April 22nd, 2014|VBA|2 Comments

About the Author:

PNRao is a passionate business analyst and having close to 10 years of experience in Data Mining, Data Analysis and Application Development. This blog is his passion to learn new skills and share his knowledge to make you expertise in Data Analysis (Excel, VBA, SQL, SAS, Statistical Methods, Market Research Methodologies and Data Analysis Techniques).

2 Comments

  1. Vignesh September 17, 2015 at 3:32 PM - Reply

    Hi All,

    I need to protect the VBA Coding except using the Excel VBA Protection method, because there are many tools in online to unlock the VBA Project.

    Please help.

    Thanks in Advance,
    Vignesh Raja

    • PNRao September 19, 2015 at 1:16 AM - Reply

      Hi Vignesh,
      Yes! as you said, there are many tools to unprotect the VBA Project. As per my understanding, no tools can protect the VBA code. We recommend you to go for COM based Add-ins.

      Thanks-PNRao!

Leave A Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.