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
Ultimate Project Management Template
20+ Excel Project Management Pack
20+ PowerPoint Project Management Pack
10+ MS Word Project Management Pack
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
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() 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.
‘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
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
|Excel VBA Reference||Project Management Reference|