View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
AFSSkier AFSSkier is offline
external usenet poster
 
Posts: 94
Default Code to Refresh inbound data sheets, then Refresh Pivot Tables

I'm trying 1st refresh the data sheets connected to an outside database.
Then refresh all of the end user's custom pivot tables.

I've tried a few different types of code. But I only get the data sheets to
refresh & not the pivot tables. If I seperate the code into 2 different
CmdButtons, I can get them to work. But I only what one CmdButton.

Private Sub CmdRefreshAll_Click()
'Refresh data sheets for outside data sources
ActiveWorkbook.RefreshAll
'or something else here to refresh only inbound data sheets.

MsgBox "All Data Sheets are updated, click ok to update Pivot tables"

'Refresh Pivot tables
Dim Wks As Worksheet
Dim pvtTable As PivotTable

Application.ScreenUpdating = False

For Each Wks In ActiveWorkbook.Worksheets
For Each pvtTable In Wks.PivotTables
pvtTable.PivotCache.Refresh
Next pvtTable
Next Wks

MsgBox "All Data Sheets & Pivot Tables are updated"

End Sub

I also tried calling a 2nd macro:

Private Sub CmdRefreshAll_Click()
'Refresh data sheets for outside data sources
ActiveWorkbook.RefreshAll
MsgBox "All Data Sheets are updated, click ok to update Pivot tables"
Call RefreshPivots
End Sub

Private RefreshPivots()
'Refresh Pivot tables
Dim Wks As Worksheet
Dim pvtTable As PivotTable

Application.ScreenUpdating = False

For Each Wks In ActiveWorkbook.Worksheets
For Each pvtTable In Wks.PivotTables
pvtTable.PivotCache.Refresh
Next pvtTable
Next Wks
MsgBox "All Data Sheets & Pivot Tables are updated"
End Sub

I'd also like to add the date the sheets were refreshed, in the same code.
I assume it would go something like this.

dt.Range("Z1") = "Refresh date: " & Format(Now(), "mm/dd/yyyy")

--
Thanks, Kevin