How to Open and Close Excel Workbook by VBA Code

In this article, we will show you how to open and close an excel workbook using VBA. We use Open and Close method of VBA to close and open excel workbooks.

VBA code to Open Excel Workbook

Suppose you want to open “Example_Workbook.xlsx” which is saved at “D:\VBA Examples” location. You can use any of below given VBA commands to open an existing workbook.

Workbooks.Open "D:\VBA Examples\Example_Workbook.xlsx"

or

Workbooks.Open Filename:="D:\VBA Examples\Example_Workbook.xlsx"

Keep in mind that you need to provide full path of workbook.

VBA code to Close Excel Workbook

An already opened excel workbook can be closed in following ways.

  • The below given VBA syntax closes “Example_Workbook.xlsx”
Workbooks("Example_Workbook.xlsx").Close
  • If you want to close the active workbook, the below given VBA syntax will do that.
ActiveWorkbook.Close
  • If you want to close all opened workbooks, the below given VBA syntax will do that.
Workbooks.Close

Leave a Reply

Your email address will not be published.