VBA Embedded SQL and Pivot Refresh

anniemalec

Board Regular
Joined
Jul 9, 2010
Messages
53
Hello

I'm using the following VBA script to refresh the 2 embedded queries and also a pivot table in the report I am creating however I'm experiencing the report refreshing the pivot before the SQL refreshes have executed.

The script I'm using is:

Option Explicit
Public Sub refreshSheets()
Dim wbk As Workbook, wks As Worksheet, qry As QueryTable, pvt As PivotTable
Application.Calculation = xlManual
Set wbk = Application.ThisWorkbook
For Each wks In wbk.Worksheets
wks.Visible = xlSheetVisible

wks.Activate
If (wks.Name = "Raw Data") Then
For Each qry In wks.QueryTables
qry.Refresh
Next qry

wks.Visible = xlSheetHidden
End If

Next wks
For Each wks In wbk.Worksheets
wks.Activate
If (wks.Name = "Report") Then
For Each pvt In wks.QueryTables
pvt.RefreshTable
Next pvt
End If
Next wks
Sheets("Report").Select
Application.Calculation = xlAutomatic
MsgBox "Refresh complete", vbInformation, "Wrap & No Answer"
End Sub

Any suggestions would be appreciated.

Cheers.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
I don't know the answer to this one for definite but if you don't get any other suggestions then maybe you could try this.

Before refreshing the pivot table, wait for Excel to finish all its calculations. This may do the job:-
Code:
do until xlcalculationstate=xldone
  doevents
loop
No guarantees!
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,854
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top