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.

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
120+ Professional Project Management Templates!
Save Up to 85% LIMITED TIME OFFER

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Browse All Templates
Excel VBA Project Management Templates

All-in-One Pack
120+ Project Management
Premium Templates
View Details

Essential Pack
50+ Project Management
Premium Templates
View Details
50+ Excel
Project Management
Templates Pack
View Details
50+ PowerPoint
Project Management
Templates Pack
View Details
25+ MS Word
Project Management
Templates Pack
View Details
Ultimate Project Management Template
View Details
Ultimate Resource Management Template
View Details
Project Portfolio Management Templates
View Details
By Last Updated: June 17, 2022Categories: VBATags:

Share This Story, Choose Your Platform!

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

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