Excel VBA – Import Data from another Workbook – 4 Easy Ways

Excel Import Data From Another Workbook – VBA Codes

To pull data from an external Excel file, use on of these scenarios.

  1. Closed Excel file: Using VBA Import data with Workbook object
  2. Opened Workbook: Using VBA Read Excel file.
  3. External Reference within Worksheets.
  4. ODBC Data Import from another workbook.

Excel – Pull data from another Workbook using VBA

To understand how to get data from another Excel file, lets assume these 2 workbook names.

  • Source: In this workbook, VBA code will be executed to write or import data from/to Target file.
  • Target: This workbook has the data that the Source Workbook will read or modify through VBA.

1. VBA To Update Closed Workbook

This Excel vba import data from another workbook without opening the file manually.

First we will create an Workbook object to refer the external Excel file.  And use that object to import data into our Active workbook or Source Workbook.

Let’s see the actual VBA code for this purpose. Copy paste the below code to VB Editor and execute the code by pressing F5. Make sure that the Target file exists in correct path as mentioned in Target_Path in the code, before executing the code.

2. VBA Read Excel file or Write To Open Workbook?

If a workbook is already in opened and executing, then you can reference that Excel with its name through ‘Workbooks’ collection. You have to use the workbook name itself inside the code to read or write content as mentioned in this sample code.

3. External Reference to Import Data from another Workbook

With this technique, in the Excel we pull data from another cell by using references. For example, in Cell A1 if we need to get date from Cell B1, we enter “=B1” in cell A1. This is a reference that is made within the scope of current workbook.

In our example if we need to refer the Target sheet, use the command as below.

=’D:\[sample.xlsx]Sheet1′!A2

This will fetch the data from the external workbook.

Reference from Microsoft: How to create External reference and pull data from another excel?

4. Data Import Option or ODBC in Excel VBA

This is similar to Data Import facility available in Excel. To do this, the Target workbook should be having some table defined in it.

To import the data from Target, Go to Source Workbook, Data->From Other Sources ->From Microsoft Query and Give the path of the Target Workbook or use the below code by modifying the File path and Column Header Details.

These are some of the methods that are available to import from an external workbook to current active workbook. But still, this is not the limit.

Note: To read a Plain Text file or binary file, use VBA File Handling commands as in this link. How to Read contents from a Text File using VBA?

There might be other feasible methods known to others. If there is any, please post a reference as you find.

  • rowbigred26

    Using the first VBA option, I see my data download into my target workbook however then it disappears shortly after. Any thoughts?

    • https://officetricks.com/ kumarapush

      Email me or paste the exact code that you executed in your system to debug the issue.

  • Delora Bulger

    Thoughtful discussion – my colleague yesterday located http://goo.gl/vshUfg to merge pdf . It’s kind of effortless to use and it’s convenient – I found out that they are giving a 7 day promotion currently

  • Prihlasovak

    Thank you. This post was very helpful.

    Would it be somehow possible to access a closed worksheet locked with a password?

    My idea is to have a sheet for each station at a production plant, where the attendants would have access only to some positions. The manager would have a “master file” that would download all necessary informations from the stations.

    I have a vague idea how to do it with a workaround, but accessing passworded files would be much better.

    • Prihlasovak

      Nevermind, I have found the answer. It just needs another parameter (,Password:=”Password”) in Workbooks.Open.

  • Samuel

    hi
    can u please say how t export data from working sheet which is in raw data

  • LIZZY

    Exроѕіtоrу!
    But I thіnk уоu ѕhоuld еmрlоу tо uѕе оf grарhісѕ іn еxрlаіnіng ѕоmе of thе
    ѕtерѕ tо fоllоw. Fоr іnѕtаnсе,” Tо іmроrt the dаtа frоm Tаrgеt, Gо tо Sоurсе
    Wоrkbооk, Dаtа->Frоm Othеr Sоurсеѕ ->Frоm Mісrоѕоft Quеrу аnd Gіvе thе
    раth оf thе Tаrgеt Wоrkbооk оr uѕе thе bеlоw соdе bу mоdіfуіng thе Fіlе раth
    аnd Cоlumn Hеаdеr Dеtаіlѕ” wоuld hаvе bееn еаѕіlу undеrѕtооd іf ассоmраnіеd by
    grарhісѕ еxрlаnаtіоn.

  • Paul Pascal

    Specifically with the Data Import option, this can be a real alternative to Power BI or one of the o there high level dashboard that are available on the market today. If you’re a small business owner and have metrics for instance, this is way more affordable and with just a bit of effort you can have an excel dashboard of everything you need for your KPIs.

  • Kinerd

    excellent, thank you

    • https://officetricks.com/ kumarapush

      Thank you.