How To Pull data from a closed workbook in Excel VBA?
Tags: topulldatafromaclose, vbamacros
Published: 2021-09-29 04:55:49

---
title: How To Pull data from a closed workbook in Excel VBA?
published: true
date: 2021-09-25 14:18:07 UTC
tags: ToPullDataFromAClose,VBAMacros
canonical_url: https://geekexcel.com/how-to-pull-data-from-a-closed-workbook-in-excel-vba/
---

In this article, we are going to see how to  **pull data from a closed workbook** in Excel VBA. Let’s get into this article!! 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)**

## Example

**To pull data, you have to follow the steps given below:**

Firstly, to start with it, you need to **save two Excel files**  **Open.xls and Closed.xls in path “D:\Test Folder”**.

**To automatically save data from the “Closed.xls” file to the “Open.xls” file** “

- Firstly, you need to **create a sample data in Closed.xls file**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Sample-data-42.png)<figcaption>Sample data</figcaption>

- In the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Visual Basic**  option under the  **Code**  section.

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

- Now, in the  **VBAProject pane** , you have to  **double-click ThisWorkbook**.
- After that, you have to  **copy and paste the code**  given below in **ThisWorkbook**.

```
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
'Put in the UsedRange Address of Sheet1 Closed.xls (this workbook)' --- Comment
    Sheet2.Cells(1, 1) = Sheet1.UsedRange.Address
End Sub

```

![](https://geekexcel.com/wp-content/uploads/2021/09/BeforeSave-Code-1024x247.png)<figcaption>BeforeSave Code</figcaption>

- To **pull the data in “Open.xls”** , you need to copy the following **code** in the **Standard Module**.

```
Sub Importdata()
Dim AreaAddress As String
Sheet1.UsedRange.Clear
Sheet1.Cells(1, 1) = "= 'D:\Test Folder\" & "[Closed.xls]Sheet2'!RC"
AreaAddress = Sheet1.Cells(1, 1)
With Sheet1.Range(AreaAddress)
     .FormulaR1C1 = "=IF('D:\Test Folder\" & "[Closed.xls]Sheet1'!RC="""",NA(),'D:\Test Folder\" & _
     "[Closed.xls]Sheet1'!RC)"
      On Error Resume Next
      .SpecialCells(xlCellTypeFormulas, xlErrors).Clear
      On Error GoTo 0
      .Value = .Value
End With
End Sub

```

![](https://geekexcel.com/wp-content/uploads/2021/09/ImportData-Code-1-1024x371.png)<figcaption>ImportData Code</figcaption>

- Then, you have to copy the following **code** in **ThisWorkbook (Open.xls)**.

```
Private Sub Workbook_Open()
Run "Importdata"
End Sub

```

![](https://geekexcel.com/wp-content/uploads/2021/09/WorkbookOpen-Code.png)<figcaption>Workbook Open Code</figcaption>

- Now, you need to  **save the code**  all the above code by selecting it and then  **close the window**.
- Again, you have to go to the  **Excel Spreadsheet** , and click on the  **Developer Tab**.
- You need 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>

- Then, you have 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-49.png)<figcaption>Run the Code</figcaption>

- Finally, you will receive output as **open the file name “Open.xls”**.

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

## Closure

We hope that this short tutorial gives you guidelines on how to  **pull data from a closed workbook** in 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/) **!!**

**Sum-Up:**

- **[Count the Times a Word Appears in a Cell or a Range in Excel Office 365!!](https://geekexcel.com/count-the-times-a-word-appears-in-a-cell-or-a-range-in-excel-office-365/)**
- **[Excel Formulas to Count specific word in a range ~ Easy Tutorial!!](https://geekexcel.com/excel-formulas-to-count-specific-words-in-a-range/)**
- **[Excel Formulas to Get the Last Word of a Text String ~ Quick Method!!](https://geekexcel.com/excel-formulas-to-get-the-last-word-of-the-text-string/)**
- **[Remove the Last Word ~ Easy Tutorial!!](https://geekexcel.com/excel-formulas-to-remove-the-last-word-easy-tutorial/)**
- **[Excel Formulas to Extract the Word that Contains Specific Text ~ Easily!!](https://geekexcel.com/excel-formulas-to-extract-the-word-that-contains-specific-text/)**