Use VBA to Update a Power Query File Path in Excel
Question details
Connect a Power Query to an Excel file whose path can be selected or changed by the user with VBA.

- 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.
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.
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.
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.
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
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.
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
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.

Change the path manually and refresh Power Query
Use the same path table without a macro when you only need to change the source occasionally.
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.

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.




