Power Query Problems

Use VBA to Update a Power Query File Path in Excel

Camila MilosovichCamila Milosovich Dec 2, 2026 868 views

Question details

Connect a Power Query to an Excel file whose path can be selected or changed by the user with VBA.

Use VBA to Update a Power Query File Path in Excel
Product
Microsoft Excel Power Query and VBA
Device & OS
not provided
Scenario
A workbook needs to import data from a source Excel file that may change.
Observed behavior
The user wants to update the source path and refresh the query to import data from the selected workbook.
Before you start

Use a version of Excel that supports Power Query and VBA, and save the workbook containing the macro as an Excel Macro-Enabled Workbook (.xlsm). Identify the sheet or table to import, and make sure replacement source files use the same name and structure.

Solution 1Recommended

Store the source path in a cell and update it with VBA

Use a worksheet table as the Power Query path source, then select a file with a VBA file picker and refresh the workbook.

1
Create a path table

Create a worksheet named Settings. Enter FilePath in A1 and the full path to an existing source workbook in A2. Select A1:A2, choose Insert > Table, confirm that the table has headers, and name the table SourcePath on the Table Design tab.

2
Create the query

Choose Data > Get Data > From Other Sources > Blank Query, then open Home > Advanced Editor. Replace the query code with: let Path = Text.From(Excel.CurrentWorkbook(){[Name="SourcePath"]}[Content]{0}[FilePath]), Source = Excel.Workbook(File.Contents(Path), null, true) in Source

3
Select the data to import

In the Power Query preview, find the required sheet or table and select its Table link in the Data column. Apply any other needed transformations, then choose Home > Close & Load. The selected item must also exist in replacement source files.

4
Add the file-picker macro

Open Developer > Visual Basic, choose Insert > Module, and paste: Sub SelectSourceFile() Dim picker As FileDialog Set picker = Application.FileDialog(msoFileDialogFilePicker) With picker .Title = "Select the source workbook" .AllowMultiSelect = False .Filters.Clear .Filters.Add "Excel files", "*.xlsx;*.xlsm;*.xlsb;*.xls" If .Show <> -1 Then Exit Sub ThisWorkbook.Worksheets("Settings").Range("A2").Value = .SelectedItems(1) End With ThisWorkbook.RefreshAll End Sub

5
Select a file and refresh

Save the workbook as .xlsm, enable macros if prompted and you trust the file, then run SelectSourceFile from Developer > Macros. Choose a source workbook; the macro updates Settings!A2 and starts Refresh All.

Store the source path in a cell and update it with VBA
Check the refreshed data: Refresh All can run connections in the background, so the macro may finish before the import does. If refresh fails, check the path in Settings!A2 and confirm that the selected workbook contains the expected sheet or table.
Free Microsoft Office alternative

Work with Excel files in WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface for opening and editing Excel files. Its format compatibility makes it easy to move everyday spreadsheet work between applications; use Excel for the Power Query and VBA workflow described above.

Open and edit Microsoft Excel .xlsx workbooks.Review and update spreadsheet data in a familiar interface.Move everyday spreadsheet work between WPS Office and Microsoft Office.Use a free, lightweight office suite.
microsoft office alternative - wps office

Frequently Asked Questions

Why does Power Query read the path from a table instead of directly from A2?

Excel.CurrentWorkbook can access named tables in the current workbook. Naming the one-column table SourcePath lets the query retrieve its first FilePath value, which is stored in A2.

What happens if I cancel the file picker?

The macro exits without changing Settings!A2 or starting a refresh. Run SelectSourceFile again from Developer > Macros when you are ready to choose a file.

Why does the query fail after I select a different workbook?

Check that the path in Settings!A2 points to an accessible file. Also check that the new workbook contains the sheet or table selected in Power Query and that its columns match the query's transformations.

Can I update the path without using VBA?

Yes. Enter the full source workbook path in Settings!A2, then choose Data > Refresh All to run the path-based query.

Why must I save the workbook as an .xlsm file?

The .xlsm format preserves the VBA macro. Use File > Save As and select Excel Macro-Enabled Workbook before saving the file.