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.



50+ Project Management Templates Pack
Excel PowerPoint Word

Advanced Project Plan & Portfolio Template
Excel Template

Business Presentations Templates Pack
PowerPoint Slides

20+ Excel Project Management Pack
Excel Templates

20+ PowerPoint Project Management Pack
PowerPoint Templates

10+ MS Word Project Management Pack
Word Templates

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.


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

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.

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()
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.

‘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()
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
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
End Sub
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).


  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.


Leave A Comment

Related pages

resource allocation template excel freeif iserrorexcel vba hlookupmicrosoft word macro examplesexcel macro userformvba excel checkboxshortcut key to insert a row in excelconditional statement in excellistbox in accessvba custom message boxhow to create a command button in excelvba delete sheetexcel macros tutorial pdfexcel vba insert rowexcel vba is nothingaccess vba sql queryworksheet functionsv lookup functionproject management templates excelvba columnsvba connection string sql serverexcel advanced filter criteriavlookup in excel tutorialexcel vba continue forvba loophow to sort in ascending order in exceldatevalue in excelexcel vba macros examplesformat string vbaclear combobox vbaexcel password protection removalinstr vba functiondashboard examples excelfor loop vbamake a drop down list in excel 2007workbooks.savewhat is vba programmingadodb parameterized queryvba inputsave excel file vbacategories of sql commandshow to hide and unhide rows in excelexcel vba returnrefresh pivot table vbaautomatic hyperlink excelhow to protect spreadsheet with passwordexcel vba insert pictureexcel colorindexexcel yearly schedule templatedynamic excel chartsunhide columnhow to record a macro in excel 2007remove duplicate data in excellock hidden columns in excelhow to insert multiple row in excelvba entirerowrow in excel definitionconstruction estimate template excelsaving excel as csvdelete row in excelhow to enable data analysis in excel 2007sql identify duplicatestimeline examples in excelmicrosoft excel 2007 vbaoffset meaning in hindiheaders in excelmultiple if and statements in excelrange cells vbavba case functionvba set workbook namedelete filtered rows in excelclassification of sql commandsvb complete reference pdfvbscript programs examplesdelete sheet in excelhow to insert a comment in exceladodb.connection vbaexcel sendmail