How to Open and Close Excel Workbook using Excel VBA?
Tags: toopenandcloseexcelw, vbamacros
Published: 2021-09-24 01:49:38

---
title: How to Open and Close Excel Workbook using Excel VBA?
published: true
date: 2021-09-18 10:54:55 UTC
tags: ToOpenAndCloseExcelW,VBAMacros
canonical_url: https://geekexcel.com/how-to-open-and-close-excel-workbook-using-excel-vba/
---

In this tutorial, we will guide you on how to **open and close Excel Workbook** using Excel VBA. Let’s get them below!! Get an official version of MS Excel from the following link: **[https://www.microsoft.com/en-in/microsoft-365/excel](https://www.microsoft.com/en-in/microsoft-365/excel)**

## Open Excel workbook

- In the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** insert two Command buttons**  from  **Activexcontrol**.

![Insert Command Button](https://geekexcel.com/wp-content/uploads/2021/09/Insert-Command-Button.png)<figcaption>Insert Command Button</figcaption>

- Now, **first command button** , you have to use to **right-click to the mouse** on **command button**.

![](https://geekexcel.com/wp-content/uploads/2021/09/First-Command-Button.png)<figcaption>First Command Button</figcaption>

- After that, you have to select the  **Visual Basic**  option under the  **Code**  section.

![Select Visual Basic](https://geekexcel.com/wp-content/uploads/2021/09/Select-Visual-Basic-8-1024x180.png)<figcaption>Select Visual Basic</figcaption>

- You need to  **copy and paste the code**  given below to **open the Excel workbook**.

```
Sub sOpenWorkbook()
' define variable for file name
Dim csFileName As String
' get filename from cell A1 on Sheet1
csFileName = ThisWorkbook.Sheets("Example Open and Close").Range("A1")
' open the workbook
Workbooks.Open csFileName
MsgBox csFileName & " opened"
End Sub

```

- Now, you have to  **save the code**  by selecting it and then  **open the window**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Save-the-Code-53.png)<figcaption>Save the Code</figcaption>

- Again, you need to go to the  **Excel Spreadsheet** , and click on the  **Developer Tab**.
- You have to choose the  **Macros option**  in the Code section.

![Choose Macro option](https://geekexcel.com/wp-content/uploads/2021/09/Choose-Macro-option-11-1024x184.png)<figcaption>Choose Macro option</figcaption>

- Now, you need to make sure that your  **macro name is selected**  and click the  **Run ** button.

![](https://geekexcel.com/wp-content/uploads/2021/09/Run-the-Code-30.png)<figcaption>Run the Code</figcaption>

- Finally, you will get the **output** as **opened Excel Workbook**.

## Close Excel workbook

- In the Excel Worksheet, you have to go to the  **Developer Tab.**
- **Second command button** , you need to use to **right-click to the mouse** on **command button**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Second-Command-button.png)<figcaption>Second Command Button</figcaption>

- You have to select the  **Visual Basic**  option under the  **Code**  section.

![Select Visual Basic option](https://geekexcel.com/wp-content/uploads/2020/09/1-106.png)<figcaption>Select the Visual Basic option</figcaption>

- You need to  **copy and paste the code**  given below to **close the Excel workbook**.

```
Sub sCloseWorkbook()
' define variable for file name
Dim csFileName As String
' get filename from cell A1 on Sheet1
csFileName = ThisWorkbook.Sheets("Example Open and Close").Range("A1")
' close the workbookWorkbooks(Split(csFileName, "\")(UBound(Split(csFileName, "\")))).CloseMsgBox Split(csFileName, "\")(UBound(Split(csFileName, "\"))) & " closed"
End Sub

```

- Now, you have to  **save the code**  by selecting it and then  **close the window**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Save-the-Code1-4-1024x320.png)<figcaption>Save the Code</figcaption>

- Again, you need to go to the  **Excel Spreadsheet** , and click on the  **Developer Tab**.
- You have to choose the  **Macros option**  in the Code section.
- Now, you need to make sure that your  **macro name is selected**  and click the  **Run ** button.

![](https://geekexcel.com/wp-content/uploads/2021/09/Run-the-Code1-1.png)<figcaption>Run the Code</figcaption>

- Finally, you will get the **output** as **a Closed Excel Workbook**.

## End of Era

We hope that this tutorial gives you guidelines on how to **open and close Excel Workbook** using Excel VBA. Please leave a comment in case of any  **queries,**  and don’t forget to mention your valuable  **suggestions**  as well. Thank you so much for Visiting Our Site!! Continue learning on **[Geek Excel](https://geekexcel.com/)!! **Read more on [**Excel Formulas**](https://geekexcel.com/excel-formula/) **!!**

**Read Next:**

- **[How to use the XNPV function in Excel office 365?](https://geekexcel.com/how-to-use-the-xnpv-function-in-excel-office-365/)**
- **[Excel Formulas to Highlight the Cells that End with Given Value!!](https://geekexcel.com/excel-formulas-to-highlight-cells-end-with/)**
- **[Shortcut for “Select to End of Cell” in the Worksheet!!](https://geekexcel.com/quick-excel-shortcut-for-select-to-end-of-cell-in-a-worksheet/)**
- **[Excel Shortcuts: Delete to the End of Line in the Spreadsheet!!](https://geekexcel.com/shortcuts-for-delete-to-the-end-of-line-in-ms-excel-365/)**
- **[Formulas for Counting Cells that End with certain character or text!!](https://geekexcel.com/excel-formulas-for-counting-cells-that-end-with-certain-character-or-text/)**