Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA: Refresh a Specific Power Query

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You can refresh a single Power Query from VBA by calling the query's Refresh method on the workbook's Queries collection, or by refreshing the QueryTable that loads it to a worksheet. The shortest working line is ThisWorkbook.Queries("QueryName").Refresh, but that only works if the query's output is loaded to a worksheet or the Data Model and the connection is not set to refresh in the background. If you need to wait for the refresh to finish before the next line runs, set BackgroundQuery = False on the underlying QueryTable or ListObject first. This guide covers the reliable method, the common error messages, and how to refresh one query without touching the others.

What You'll Need

  • Excel for Windows or Excel for Mac with Power Query (built in from Excel 2016 onward; on older versions it was a separate add-in called Power Query).
  • A workbook that already contains at least one Power Query, with the query output loaded to a worksheet table or to the Data Model.
  • The Developer tab enabled: File > Options > Customize Ribbon > tick Developer in the right-hand list. On Mac: Excel > Preferences > Ribbon & Toolbar.
  • Macro security set to allow macros in this workbook, or the file saved as .xlsm. A plain .xlsx cannot store VBA.

Step-by-Step

  1. Find the exact query name. Open the Data tab and click Queries & Connections. The name shown in that pane is the name VBA needs. It is case-sensitive in the sense that a typo throws a "Subscript out of range" error, so copy it carefully. Note whether the query loads to a worksheet table or only to the Data Model ("Connection Only").
  2. Open the VBA editor. Press Alt+F11 (on Mac, Fn+Option+F11 or use the Developer tab > Visual Basic). In the Project Explorer, right-click your workbook and choose Insert > Module.
  3. Write the basic refresh macro. Paste this and change MyQuery to your query name:
    Sub RefreshOneQuery()
        ThisWorkbook.Queries("MyQuery").Refresh
    End Sub
    This is the simplest form. It starts the refresh but does not necessarily wait for it to finish before the macro ends.
  4. Make the refresh synchronous (recommended). If your macro does anything after the refresh — saving, copying results, sending an email — you need it to wait. The Queries().Refresh method does not expose a wait flag, so target the QueryTable instead:
    Sub RefreshOneQueryAndWait()
        Dim qt As QueryTable
        Set qt = ThisWorkbook.Worksheets("Sheet1").QueryTables("MyQuery")
        qt.BackgroundQuery = False
        qt.Refresh BackgroundQuery:=False
    End Sub
    Replace Sheet1 with the sheet that holds the query output. If the query loads to a table (ListObject), use ThisWorkbook.Worksheets("Sheet1").ListObjects("TableName").QueryTable instead.
  5. Handle queries loaded only to the Data Model. These have no worksheet QueryTable, so the Queries().Refresh method is the way in. It refreshes the connection and the model. If you need to wait, follow it with a DoEvents loop or refresh the model connection via ThisWorkbook.Connections("Query - MyQuery").OLEDBConnection.BackgroundQuery = False before calling Refresh.
  6. Run the macro. Press F5 with the cursor inside the Sub, or assign it to a button via Developer > Insert > Button (Form Control) and point it at your macro.
  7. Check the result. The query's output table should update. If nothing appears to happen, the query may be set to refresh in the background and the macro ended before the data arrived — see Troubleshooting.

Common Mistakes

  • Using the wrong name. The query name in Queries() is the name in Queries & Connections, not the worksheet table name. They are often different.
  • Assuming Refresh waits. It does not. Code after it can run on stale data unless you set BackgroundQuery = False.
  • Refreshing QueryTables on the wrong sheet. The QueryTable lives on the sheet where the output sits, not on the sheet that was active when the query was created.
  • Forgetting to save as .xlsm. Macros in an .xlsx are stripped on save with no warning beyond the initial prompt.
  • Refreshing a connection-only query via QueryTables. That collection is empty for Data Model-only queries, so you get "Subscript out of range". Use Queries() or Connections() for those.

Tips

  • Wrap the refresh in Application.StatusBar messages or set Application.ScreenUpdating = False around it so the screen does not flicker during a long refresh.
  • If the query uses parameters from cells, change the cell values before calling Refresh; the query picks up the new values on refresh.
  • To refresh several named queries in order, call Queries("A").Refresh then Queries("B").Refresh sequentially with BackgroundQuery = False on each QueryTable so dependencies resolve correctly.
  • Add On Error GoTo handling so a failed refresh (bad credentials, missing source file) does not leave the workbook in a half-updated state.
  • If the query uses an external source that requires sign-in, the macro runs under your credentials; other users opening the file may be prompted.

Troubleshooting

  • "Subscript out of range". The query name or sheet name is wrong, or the query loads only to the Data Model and you used QueryTables. Recheck the exact name in Queries & Connections.
  • Macro finishes but data is stale. The refresh is running in the background. Set BackgroundQuery = False on the QueryTable, or add a wait loop checking qt.Refreshing.
  • "Refresh failed" or credentials prompt. The data source needs sign-in or the file path has moved. Refresh manually once from the Data tab to confirm the query works outside VBA.
  • Nothing happens at all. Check that macros are enabled (File > Options > Trust Center > Macro Settings) and that the macro is in a standard module, not a sheet module.
  • Refresh is very slow. The query may be pulling more data than needed, or the source is remote. Test a manual refresh to compare; if manual is fast and VBA is slow, the issue is usually the background setting or a wait loop that is polling too aggressively.

Frequently Asked Questions

Can I refresh just one query without refreshing the others?

Yes. Target that query by name with ThisWorkbook.Queries("Name").Refresh or its specific QueryTable. Avoid ThisWorkbook.RefreshAll, which refreshes every connection in the workbook.

Why does my macro end before the data updates?

Because the refresh is asynchronous by default. Set BackgroundQuery = False on the QueryTable before calling Refresh, or poll qt.Refreshing until it returns False.

Does this work on Excel for Mac?

Yes, with the same object model, but the keyboard shortcut to open the VBA editor differs and some older Mac builds handle background refresh differently. Test the wait behaviour on your build.

Can I refresh a query that loads only to the Data Model?

Yes, using Queries("Name").Refresh or the matching Connections entry. There is no worksheet QueryTable for these, so the QueryTable approach will fail.

How do I refresh a query on a schedule?

Use the Workbook_Open event in ThisWorkbook to run your refresh macro when the file opens, or use Windows Task Scheduler to open the workbook. Excel itself has no built-in timer for VBA.

Will the refresh work if the source file is closed?

It depends on the source. A closed workbook on a local or network drive usually refreshes fine; some sources require the file to be open or require credentials. Test a manual refresh first.