Refresh Excel Queries, Formulas, and PivotTables in Order with VBA
Question details
The user needs to refresh dependent formulas, a SQL query, and PivotTables in a controlled VBA sequence.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A SQL Server query uses an Excel cell as a parameter, and other workbook objects depend on the query results.
- Observed behavior
- Background query execution can allow later refresh steps to run before the required data is available.
Identify the parameter cells, the query's QueryTable object, and the dependent PivotTable names. Open the workbook in a version of Excel that supports VBA, and confirm that its data connection and SQL parameters are configured correctly.
Calculate the parameter and refresh the QueryTable synchronously
Use this sequence when the SQL query is exposed as a QueryTable and dependent PivotTables must wait for its results.
The example calculates the parameter range first, waits for the QueryTable refresh to finish, recalculates workbook formulas, and then refreshes a PivotTable. Replace the sheet, range, query, and PivotTable names with those in your workbook.
In Excel, open Developer > Visual Basic, then choose Insert > Module. If the Developer tab is hidden, enable it through Excel's ribbon customization settings.
Paste this code into the module: Sub RefreshInOrder() Dim qt As QueryTable Dim pt As PivotTable Worksheets("Parameters").Range("A1:B3").Calculate Set qt = Worksheets("Data").QueryTables("QueryName") qt.BackgroundQuery = False If Not qt.Refresh(BackgroundQuery:=False) Then MsgBox "The query refresh was canceled." Exit Sub End If Application.Calculate Set pt = Worksheets("Report").PivotTables("PivotTable1") pt.RefreshTable End Sub
In the module, replace Parameters and A1:B3 with the sheet and cells needed to calculate the SQL parameter. Replace Data, QueryName, Report, and PivotTable1 with the names of your query and PivotTable objects. Add refresh calls for any other dependent objects in the order they need.
Save the workbook as an Excel Macro-Enabled Workbook (.xlsm), then choose Developer > Macros, select RefreshInOrder, and click Run. Check the query results and PivotTable against the current parameter value.

Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for common Excel formats, making it easy to move everyday spreadsheet work across. Excel-specific VBA and data connections may require Microsoft Excel and should be tested separately.

Frequently Asked Questions
Why does my PivotTable refresh before the query finishes?
The query may be running in the background. For a compatible QueryTable, set BackgroundQuery to False and call Refresh with BackgroundQuery:=False before refreshing the PivotTable.
How do I make sure the SQL query uses the latest cell value?
Calculate the parameter cell and any required formulas before calling QueryTable.Refresh. For example, use Worksheets("Parameters").Range("A1:B3").Calculate, and verify that the query parameter is linked to the intended cell.
What if my query is not in the worksheet's QueryTables collection?
Check how the query is loaded. A compatible query loaded to a worksheet table may be accessible through Worksheets("Data").ListObjects("TableName").QueryTable. Other connection types, including Power Query, may need a different refresh approach.
Why calculate formulas again after refreshing the query?
Formulas that depend on the returned query data need to reflect the new results before downstream objects are refreshed. The example uses Application.Calculate after the query completes and before PivotTable.RefreshTable.




