• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA wait for the queries to refresh before refreshing pivots

Cele

Member
Hi,
Please see macro written below. There an additional line of code I can write? To make the code wait for the queries to refresh before refreshing pivots. If so, what could it be? Please advise.

Sub Refresh_PQs_PTables()

' Macro to update my Power Query script(s)

Dim lTest As Long, cn As WorkbookConnection
On Error Resume Next
For Each cn In ThisWorkbook.Connections
lTest = InStr(1, cn.OLEDBConnection.Connection, "Provider=Microsoft.Mashup.OleDb.1", vbTextCompare)
If Err.Number <> 0 Then
Err.Clear
Exit For
End If
If lTest > 0 Then cn.Refresh
Next cn

'Step 1: Declare you Variables
Dim ws As Worksheet
Dim pt As PivotTable


'Step 2: Loop through each sheet in workbook
For Each ws In ThisWorkbook.Worksheets


'Step 3: Loop through each pivot table
For Each pt In ws.PivotTables
pt.RefreshTable
Next pt
Next ws

ActiveWorkbook.Worksheets(1).Activate

End Sub
 
Does your connection/query have BackgroundQuery turned on? If so turn it off and process should wait till query finishes.
 
Back
Top