Smart Systems for Home

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

Excel VBA: Refresh a Power Query Table

· 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 Power Query table from VBA with a single line: ActiveWorkbook.Connections("Query - YourQueryName").Refresh, or by calling ThisWorkbook.RefreshAll if you want every connection and pivot cache updated at once. The connection name is not the table name — it is the query name prefixed with Query - — which is the single biggest reason refresh macros fail. The steps below show how to find the real name, write a refresh routine that waits for the load to finish, and avoid the common traps.

What You'll Need

  • Excel on Windows or Mac with Power Query (built in from Excel 2016 onward; as an add-in in 2010 and 2013).
  • A workbook that already contains a Power Query query loaded to a table or to the Data Model.
  • The Developer tab enabled: File > Options > Customize Ribbon > tick Developer. On Mac: Excel > Preferences > Ribbon & Toolbar.
  • Macro-enabled file format (.xlsm) if you want the code to persist.

Menu labels can differ slightly by Excel version, platform and region, so treat the paths below as a guide rather than an exact script.

Step-by-Step

  1. Find the connection name. Open the workbook and go to Data > Queries & Connections. In the panel, hover over the query or click it once. The name shown there is the query name; the connection object VBA sees is that name prefixed with Query - . If your query is called SalesData, the connection is Query - SalesData. You can confirm by opening the Immediate Window (Ctrl+G in the VBA editor) and typing ? ThisWorkbook.Connections(1).Name then Enter.
  2. Open the VBA editor. Press Alt+F11 (Fn+Option+F11 on Mac). In the Project pane, right-click your workbook and choose Insert > Module. A blank module opens.
  3. Write the refresh routine. Paste a procedure like this, replacing the name with yours:
    Sub RefreshOneQuery()
        Dim conn As WorkbookConnection
        On Error Resume Next
        Set conn = ThisWorkbook.Connections("Query - SalesData")
        On Error GoTo 0
        If conn Is Nothing Then
            MsgBox "Connection not found. Check the name."
            Exit Sub
        End If
        conn.Refresh
    End Sub
    The On Error Resume Next block is deliberate: it lets you show a friendly message instead of a run-time error 9 when the name is wrong.
  4. Refresh everything instead (optional). If you want all queries, pivot tables and data connections updated, replace the body with ThisWorkbook.RefreshAll. This is simpler but slower and will also refresh things you may not need.
  5. Wait for the refresh to finish. conn.Refresh is asynchronous for some connection types, so the next line of code may run before the data arrives. Add a wait loop:
    conn.Refresh
    Do While conn.OLEDBConnection.Refreshing
        DoEvents
    Loop
    For Data Model connections, check conn.OLEDBConnection.Refreshing the same way. If the property is not available for your connection type, fall back to a timed loop with Application.Wait Now + TimeValue("00:00:02") and re-check the table's row count.
  6. Run it. Place the cursor inside the procedure and press F5, or assign the macro to a button via Developer > Insert > Button (Form Control) and pick the macro from the list.
  7. Save as .xlsm. Use File > Save As and choose Excel Macro-Enabled Workbook. A plain .xlsx will strip the code on save.

Common Mistakes

  • Using the table name instead of the connection name. Connections("SalesData") fails; it must be Connections("Query - SalesData").
  • Assuming the refresh is synchronous. Code that reads the table immediately after Refresh often reads stale or empty rows. Always wait.
  • Refreshing a query loaded to the Data Model with table-only methods. A query loaded only to the Data Model has no ListObject, so ListObjects("SalesData").QueryTable.Refresh throws an error. Use the connection object instead.
  • Hard-coding names that get renamed. Renaming a query in the Queries & Connections pane breaks the macro silently. Loop over ThisWorkbook.Connections and match on InStr(conn.Name, "SalesData") to make it resilient.
  • Forgetting Application.ScreenUpdating = False. Large refreshes flicker badly; toggle it off at the start and back on in a cleanup block.
  • Saving as .xlsx. The macro disappears without warning.

Tips

  • Wrap the routine in Application.DisplayAlerts = False and restore it in an error handler so background refresh prompts do not block the macro.
  • If the workbook is shared or on OneDrive, refresh can fail while another user has it open; check for that before blaming the code.
  • For a dashboard, refresh only the queries that feed it rather than calling RefreshAll — it is noticeably faster.
  • Log the refresh time to a cell with Range("A1").Value = Now so you can see at a glance whether the data is current.
  • Keep the connection name in a named range or constant at the top of the module; it makes future renames a one-line edit.

Troubleshooting

Run-time error 9 (Subscript out of range). The connection name is wrong. Open the Immediate Window and print every connection name with a short loop over ThisWorkbook.Connections; copy the exact string.

Run-time error 1004. Usually the query failed to load — a broken source path, a deleted file, or credentials that expired. Refresh the query manually via Data > Refresh All and read the error message in the Queries & Connections pane.

The macro runs but the table is empty. The refresh had not finished. Add the wait loop shown in step 5, or check whether the query is set to refresh in the background (Query Properties > uncheck Enable background refresh) so it completes before the next line runs.

Nothing happens at all. Macros may be disabled. Check File > Options > Trust Center > Trust Center Settings > Macro Settings, and confirm the file is in a trusted location.

Prompts appear mid-refresh. A privacy-level or credential dialog is blocking it. Set the workbook's privacy level in Data > Get Data > Query Options > Privacy, and store credentials so they are not requested each time.

Refresh is very slow. The query may be pulling far more rows than needed. Filter at the source, remove unused columns, and disable background refresh for large loads.

Frequently Asked Questions

Can I refresh a single Power Query table without refreshing the whole workbook?

Yes. Reference the specific connection object, as in step 3, rather than calling RefreshAll.

Why does my macro say the connection does not exist when the query is clearly there?

Because the connection name includes the Query - prefix. Use the exact string from the Queries & Connections pane.

Does this work on Excel for Mac?

Yes, the connection object model is available on Mac, though some older connection types and the Data Model behave differently. Test the routine on the machine that will run it.

How do I refresh on open automatically?

Put the refresh call in the Workbook_Open event in the ThisWorkbook module. Note that this runs every time the file opens, including when others open it.

Can I refresh a query loaded only to the Data Model?

Yes — use the connection object. There is no table to target, so ListObjects methods will not work.

Why does the table show old data right after the macro finishes?

The refresh is still running in the background. Wait on the Refreshing property or disable background refresh for that query.