Hi all,

so i have written a vba script to update all my database links and pivot tables but am finding that the ActiveWorkbook.RefreshAll isn't finished by the time it updates the pivot tables so i'm having to run the macro twice before it yeilds the correct results.

My question is how do I add a "finish event" to the ActiveWorkbook.RefreshAll before moving on with the rest of my code?

The code is as follows:

Private Sub Update_Click()
    
    ActiveWorkbook.RefreshAll

    DoEvents

    Range("D9:F9").Select
    Selection.FillDown
    Range("B2").Select
    
    DoEvents

    Dim pt As PivotTable
    
    For Each pt In Sheets("Summary").PivotTables
      
    pt.RefreshTable
    
    Next pt

End Sub