1004 - Please wait until Microsoft Excel has finished refreshing the PivotTable report, and then try the command again.

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
Is there a way for VBA to wait till connection is completely refreshed and not go and execute everything?

'm getting error '1004 - Please wait until Microsoft Excel has finished refreshing the PivotTable report, and then try the command again.'
Code:
[FONT=Courier New]Sub RefreshConection()
ThisWorkbook.Connections("MainDbData").Refresh

'run this lines below only after refreshing data from database
[FONT=Courier New]    ActiveSheet.PivotTables("Top5Pivot").PivotFields("Week").ClearAllFilters
    ActiveSheet.PivotTables("Top5Pivot").PivotFields("Week").CurrentPage = "20"[/FONT]
End Sub[/FONT]

Plese help.
thanks in advance.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
In that code you could do something like the following. Its not the most elegant solution but it should work.

Code:
Sub RefreshConection()
    ThisWorkbook.Connections("MainDbData").Refresh
    
resumeWait:

    On Error GoTo errWait
        'run this lines below only after refreshing data from database
        ActiveSheet.PivotTables("Top5Pivot").PivotFields("Week").ClearAllFilters
        ActiveSheet.PivotTables("Top5Pivot").PivotFields("Week").CurrentPage = "20"
    On Error GoTo 0
    
    Exit Sub
    
errWait:
    Application.Wait (Now + TimeValue("00:00:01")) 'Ask Excel to wait for 1 second
    Resume resumeWait 'Resume the code to try again
End Sub

Basically if an error occurs the code will go into the errWait handler. This then causes Excel to wait for a second before sending the code back to try again.

Hope this helps!
 
Upvote 0
Hi Ad, thanks for quick reply. I tried the same way with Goto resumeWait instead of resume resumeWait...
is there any different between resume and goto? because it error even when i let excel wait for 15seconds.

Thanks again.
 
Upvote 0
As far as I know Resume clears the error and resets the error handling as well, whereas GoTo will just jump back to the previous line.
 
Upvote 0
Ok.thanks for clarifying on that...O tried it again but it is going into a long loop...just dont know why.
If i manually refresh it it takes around 6 seconds where are in vba it is going to 200+ seconds so i ctr + Break to stop it...
 
Upvote 0
Hmm... It could be because the Wait command is halting everything, so even the refresh is stopped. I confess I haven't had much workings with the Wait command.

Try commenting out or removing that line so that the code should run like this:
Code:
Sub RefreshConection()
    ThisWorkbook.Connections("MainDbData").Refresh
    
resumeWait:

    On Error GoTo errWait
        'run this lines below only after refreshing data from database
        ActiveSheet.PivotTables("Top5Pivot").PivotFields("Week").ClearAllFilters
        ActiveSheet.PivotTables("Top5Pivot").PivotFields("Week").CurrentPage = "20"
    On Error GoTo 0
    
    Exit Sub
    
errWait:
    Resume resumeWait 'Resume the code to try again
End Sub
 
Upvote 0
it still is going into endless loop...
i think there should be some work around...i tried googling but of no much help on this topic....
 
Upvote 0

Forum statistics

Threads
1,203,240
Messages
6,054,316
Members
444,717
Latest member
melindanegron

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