I have some vb code that will refresh my pivot tables for me, is there a way to loop this so I can refresh pivot tables 1 to 20 say? Rather than listing 20 lines of code below Eg
ActiveWorkbook.PivotTables(1).PivotCache.Refresh
ActiveWorkbook.PivotTables(2).PivotCache.Refresh
ActiveWorkbook.PivotTables(3).PivotCache.Refresh
Etc..
Many thanks in advance!
As an example: If you want to refresh all pivot caches in your workbook, you can do something like this:
Sub RefreshPivotCache()
Dim ws As Worksheet
Dim PT As PivotTable
For Each ws In ActiveWorkbook.Worksheets '<~~ Loop all worksheets in workbook
For Each PT In ws.PivotTables '<~~ Loop all pivot tables in worksheet
PT.PivotCache.Refresh
Next PT
Next ws
End Sub
You can use this, this will also go through each worksheet in the workbook, and each pivot table in each sheet and refresh & update the table.
Sub RefreshPivotData()
Dim pvotCht As PivotTable, Sheet As Worksheet
For Each Sheet In ThisWorkbook.Worksheets
For Each pvotTbl In Sheet.PivotTables
pvotTbl.RefreshTable
pvotTbl.Update
Next
Next
ThisWorkbook.Save
End Sub
try this:
Option Explicit
Sub RefresfPivotTables()
Sheets("YOUR_SHEET").Select
Dim i As Integer
For i = 1 To 20
ActiveSheet.PivotTables("PivotTable" & i).PivotCache.Refresh
Next i
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.