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

creating a dashboard in excel 2007developers tab excelexcel 2010 unhide all columnsexcel userform exampleshow to unhide tabs in excelhide column in excelautofilter excel 2013adding excel columnsexcel vba countspreadsheet control vbaestimate sheet templatesexcel vba choosemysql excel connectorautofit in excelhow to unlock an excel spreadsheet without the passwordexcel vba line feedexcel insert row macrovba instrvba object oriented programming tutorialunlock protected excel spreadsheetvlookup examplevba code accessexcel vba select rowsexcel vba pivot table filtervba excel basicshow to delete empty cells in exceltask planner template excelsort excel vbatechnical interview questions for it fresherscopy tabs in excelexcel vba project passwordhow to autofit in excel 2013hr questions for freshers with answers pdfexcel macro change cell colorproject dashboards templatesactivex vbaexcel macro clear cellsvb.net excel saveasexcel vba instr functionexcel vba create sheetexcel vba beginner tutorialvba controlhow to use the vlookup function in excel 2010excel macro to copy data from one workbook to anothercan you merge cells in excelwhat is vlookup in ms excelmacros in sqlhow to create a stacked bar chart in excel 2007what is vlookup exceloffset example excelvba code to close formdo until loop vbaenable macros in vbaexcel vba input boxvba excel bookexcel vba timevaluehow to use sumif in excel 2007application calculation xlcalculationmanual vbavba excel close fileexcel sumifs functionvba open csv fileexcel userformssql coding for beginnersunhide row in excelif statements in excel vbaexcel connect to oracle databaseenableevents vbamacro example in excelvba case ishighlight duplicate rows in excellookup excel 2007how to check duplicate values in excelfor vba loopsimple gantt chart excel templateaccess vba export to excelvlookup leftexcel replace in formuladcl statementsexcel button hyperlinkvba loopsvba matchvb net excel column width