Refresh unknow amount of Pivot Tables /30secs

MarkCBB

Active Member
Joined
Apr 12, 2010
Messages
497
Hi there,

I am trying to write some code that will refresh an unknown about of Pivot tables every 30 seconds. The Pivot tables are on different worksheets but all are in the same workbook. the name/numbering (PivotTable1,2,3) of the pivots are not contiguous.
I have just started with VBA (reading my first book) this is what I have got so far.
Code:
Sub RefPVT()
 ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
 ActiveSheet.PivotTables("PivotTable2").PivotCache.Refresh
 ActiveSheet.PivotTables("PivotTable3").PivotCache.Refresh
 ActiveSheet.PivotTables("PivotTable3").PivotCache.Refresh
 
End Sub

Sub Macro1()
Application.OnTime Now + TimeValue("00:00:30"), "RefPVT"
End Sub

As you can see there, this is not working according to the above mentioned parameters.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Have you tried just refreshing everything

Code:
ActiveWorkbook.RefreshAll
 
Upvote 0
I haven't tested the below code, but something like this should work:

Code:
Sub RefPVT()

Application.ScreenUpdating = False

Dim WS As Worksheet
Dim PT As pivottable

For Each WS In ThisWorkbook.Worksheets

    For Each PT In WS.PivotTables
        PT.PivotCache.Refresh
    Next PT
    
Next WS

Application.ScreenUpdating = True

End Sub
 
Upvote 0
Ummm, I thought i know how to make it run every 30sec. but the code above only worked once. then I tried tthe Activeworkbook.RefreshAll and i placed it in a Onchange sub, amd it crashed.
 
Upvote 0
The refreshall option may not be available in version of excel before 2007, I can't check so perhaps you should stick with Neils suggestion.

As for the ontime method you need to call the name of the macro that contains the Ontime from the procedure it runs..In air code

Sub ontime

ontime 30secs, "Procedure"

End Sub

Sub Procedure

Do stuff

Call Ontime

End Sub

Etc
 
Upvote 0
Hi Dave,

Using 2010. its working perfectaly now. thanks you. Can you recommend some books for me to read. I am very new to VBA but would like to be alot better.

Thank-you.
 
Upvote 0

Forum statistics

Threads
1,215,433
Messages
6,124,863
Members
449,195
Latest member
MoonDancer

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