Data Import & Export

Import Text Files from Cell Paths in Excel VBA

John WilsonJohn Wilson Nov 22, 2026 869 views

Question details

Import external text files into Excel using file paths stored in worksheet cells instead of a fixed path in a VBA macro.

Product
Microsoft Excel
Device & OS
not provided
Scenario
A workbook contains text file paths in cells, and a VBA macro needs to import the files into a worksheet.
Observed behavior
The macro needs a valid TEXT connection and a destination range on the same worksheet as the query table.
Before you start

Put a full, accessible text file path in Sheet1 cell A1 and make sure a worksheet named Temp exists with space for the imported data. Save the workbook as a macro-enabled file and enable macros if your organization’s security settings permit them.

Solution 1Recommended

Build the Excel VBA connection from a worksheet cell

Use a fully qualified source cell and destination range to import a text file with QueryTables.Add.

The connection string must start with TEXT; followed by the file path. The destination must be on the worksheet whose QueryTables collection creates the query.

1
Open the VBA editor

In Excel, open the workbook containing Sheet1 and Temp, then press Alt+F11. Choose Insert > Module.

2
Add the import macro

Paste this code into the module: Sub ImportTextFromCell() Dim sourcePath As String Dim targetSheet As Worksheet Dim query As QueryTable sourcePath = Trim$(ThisWorkbook.Worksheets("Sheet1").Range("A1").Value) If sourcePath = "" Then MsgBox "Enter a file path in Sheet1!A1." Exit Sub End If Set targetSheet = ThisWorkbook.Worksheets("Temp") Set query = targetSheet.QueryTables.Add( _ Connection:="TEXT;" & sourcePath, _ Destination:=targetSheet.Range("A1")) query.Refresh BackgroundQuery:=False End Sub

3
Run and check the import

Place the cursor inside ImportTextFromCell and press F5. Check the imported data on Temp; if the import fails, verify the path in Sheet1!A1 and access to the file.

4
Import additional files

For each additional path, adapt the macro to read its source cell and use a separate destination area or worksheet with enough space for the imported data. Set text import properties on the QueryTable if the files require a particular delimiter.

Use qualified ranges: Avoid relying on ActiveCell for the source or destination. Both ranges should identify their worksheets explicitly, and the destination must belong to the worksheet that creates the query table.
Free Microsoft Office alternative

Work with spreadsheets in WPS Office

WPS Office is a free, lightweight alternative with a familiar interface and Microsoft Office format support, making it easier to move spreadsheet work between applications. It does not fix an Excel-specific VBA query issue.

Work with spreadsheet data in WPS SpreadsheetsOpen and save Microsoft Excel formats, including XLSXUse a familiar spreadsheet interface for everyday tasksMove existing Office documents into a lightweight suite
microsoft office alternative - wps office

Frequently Asked Questions

Why does QueryTables.Add reject my destination range?

The destination must be on the worksheet whose QueryTables collection creates the query. For example, use Worksheets("Temp").QueryTables.Add with Worksheets("Temp").Range("A1") as the destination.

What should the file path cell contain?

Enter the full path to an accessible text file in the source cell, such as Sheet1!A1. The macro adds the TEXT; prefix when it builds the connection string.

How can I import several files listed in cells?

Adapt the macro to loop through the cells containing paths and create a query for each file. Give each import a separate destination area or worksheet so the results have room.

Can Power Query use a folder instead of individual cell paths?

Yes. Select Data > Get Data > From File > From Folder, choose the folder, and use Combine & Transform Data to review and combine similarly structured files.