Hi,
Apologies as I'm still a bit new to all this, but can someone have a look at my script and let me know if it's possible to optimise it a bit so that the data all refreshes much quicker.
As you can see it it refreshes 10 different pivot tables (the source data is about 130,000 rows and 60 columns).
I have put a bit in to put manual calcs on, then auto calcs when all the pivots have been updated.
Any advice would be greatly appreciated.
Many thanks
Chris
Apologies as I'm still a bit new to all this, but can someone have a look at my script and let me know if it's possible to optimise it a bit so that the data all refreshes much quicker.
As you can see it it refreshes 10 different pivot tables (the source data is about 130,000 rows and 60 columns).
I have put a bit in to put manual calcs on, then auto calcs when all the pivots have been updated.
Any advice would be greatly appreciated.
Many thanks
Chris
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("C2:L8")
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
Application.Calculation = xlManual
Application.StatusBar = "Hi " & Application.UserName & "! Your report will be ready in a few seconds..."
'Set the Variables to be used
Dim pt1 As PivotTable
Dim pt2 As PivotTable
Dim pt3 As PivotTable
Dim pt4 As PivotTable
Dim pt5 As PivotTable
Dim pt6 As PivotTable
Dim pt7 As PivotTable
Dim pt8 As PivotTable
Dim pt9 As PivotTable
Dim pt10 As PivotTable
Dim pt11 As PivotTable
Dim pt12 As PivotTable
Dim pt13 As PivotTable
Dim pt14 As PivotTable
Dim pt15 As PivotTable
Dim pt16 As PivotTable
Dim Field1 As PivotField
Dim Field2 As PivotField
Dim Field3 As PivotField
Dim Field4 As PivotField
Dim Field5 As PivotField
Dim Field6 As PivotField
Dim Field7 As PivotField
Dim Field8 As PivotField
Dim Field9 As PivotField
Dim Field10 As PivotField
Dim Field11 As PivotField
Dim Field12 As PivotField
Dim Field13 As PivotField
Dim Field14 As PivotField
Dim Field15 As PivotField
Dim Field16 As PivotField
Dim Field17 As PivotField
Dim Field18 As PivotField
Dim Field19 As PivotField
Dim Field20 As PivotField
Dim Field21 As PivotField
Dim Field22 As PivotField
Dim Field23 As PivotField
Dim Field24 As PivotField
Dim Field25 As PivotField
Dim Field26 As PivotField
Dim Field27 As PivotField
Dim Field28 As PivotField
Dim Field29 As PivotField
Dim Field30 As PivotField
Dim NewCat1 As String
Dim NewCat2 As String
Dim NewCat3 As String
Dim NewCat4 As String
Dim pi As PivotItem
'Here you amend to suit your data
Set pt1 = Worksheets("Analysis Sheet").PivotTables("PivotTable1")
Set pt2 = Worksheets("Analysis Sheet").PivotTables("PivotTable2")
Set pt3 = Worksheets("Analysis Sheet").PivotTables("PivotTable3")
Set pt4 = Worksheets("Analysis Sheet").PivotTables("PivotTable4")
Set pt5 = Worksheets("Analysis Sheet").PivotTables("PivotTable5")
Set pt6 = Worksheets("Analysis Sheet").PivotTables("PivotTable6")
Set pt7 = Worksheets("Analysis Sheet").PivotTables("PivotTable7")
Set pt8 = Worksheets("Analysis Sheet").PivotTables("PivotTable8")
Set pt9 = Worksheets("Analysis Sheet").PivotTables("PivotTable9")
Set pt10 = Worksheets("Analysis Sheet").PivotTables("PivotTable10")
Set Field1 = pt1.PivotFields("Brand")
Set Field2 = pt1.PivotFields("New Product Group")
Set Field3 = pt1.PivotFields("Country")
Set Field4 = pt1.PivotFields("Main Tour")
Set Field5 = pt2.PivotFields("Brand")
Set Field6 = pt2.PivotFields("New Product Group")
Set Field7 = pt2.PivotFields("Country")
Set Field8 = pt2.PivotFields("Main Tour")
Set Field9 = pt3.PivotFields("Brand")
Set Field10 = pt3.PivotFields("New Product Group")
Set Field11 = pt3.PivotFields("Country")
Set Field12 = pt4.PivotFields("Brand")
Set Field13 = pt4.PivotFields("New Product Group")
Set Field14 = pt4.PivotFields("Country")
Set Field15 = pt5.PivotFields("Brand")
Set Field16 = pt5.PivotFields("New Product Group")
Set Field17 = pt5.PivotFields("Country")
Set Field18 = pt6.PivotFields("Brand")
Set Field19 = pt6.PivotFields("New Product Group")
Set Field20 = pt6.PivotFields("Country")
Set Field21 = pt7.PivotFields("Brand")
Set Field22 = pt8.PivotFields("Brand")
Set Field23 = pt8.PivotFields("New Product Group")
Set Field24 = pt9.PivotFields("Brand")
Set Field25 = pt9.PivotFields("New Product Group")
Set Field26 = pt9.PivotFields("Country")
Set Field27 = pt10.PivotFields("Brand")
Set Field28 = pt10.PivotFields("New Product Group")
Set Field29 = pt10.PivotFields("Country")
Set Field30 = pt10.PivotFields("Main Tour")
NewCat1 = Worksheets("Analysis Sheet").Range("C2").Value
NewCat2 = Worksheets("Analysis Sheet").Range("C4").Value
NewCat3 = Worksheets("Analysis Sheet").Range("C6").Value
NewCat4 = Worksheets("Analysis Sheet").Range("C8").Value
With pt1
Field1.ClearAllFilters
Field1.CurrentPage = NewCat1
Field2.ClearAllFilters
Field2.CurrentPage = NewCat2
Field3.ClearAllFilters
Field3.CurrentPage = NewCat3
Field4.ClearAllFilters
Field4.CurrentPage = NewCat4
pt1.RefreshTable
End With
With pt2
Field5.ClearAllFilters
Field5.CurrentPage = NewCat1
Field6.ClearAllFilters
Field6.CurrentPage = NewCat2
Field7.ClearAllFilters
Field7.CurrentPage = NewCat3
Field8.ClearAllFilters
Field8.CurrentPage = NewCat4
pt2.RefreshTable
End With
With pt3
Field9.ClearAllFilters
Field9.CurrentPage = NewCat
Field10.ClearAllFilters
Field10.CurrentPage = NewCat2
Field11.ClearAllFilters
Field11.CurrentPage = NewCat3
pt3.RefreshTable
End With
With pt4
Field12.ClearAllFilters
Field12.CurrentPage = NewCat1
Field13.ClearAllFilters
Field13.CurrentPage = NewCat2
Field14.ClearAllFilters
Field14.CurrentPage = NewCat3
pt4.RefreshTable
End With
With pt5
Field15.ClearAllFilters
Field15.CurrentPage = NewCat1
Field16.ClearAllFilters
Field16.CurrentPage = NewCat2
Field17.ClearAllFilters
Field17.CurrentPage = NewCat3
pt5.RefreshTable
End With
With pt6
Field18.ClearAllFilters
Field18.CurrentPage = NewCat1
Field19.ClearAllFilters
Field19.CurrentPage = NewCat2
Field20.ClearAllFilters
Field20.CurrentPage = NewCat3
pt6.RefreshTable
End With
With pt7
Field21.ClearAllFilters
Field21.CurrentPage = NewCat1
pt7.RefreshTable
End With
With pt8
Field22.ClearAllFilters
Field22.CurrentPage = NewCat1
Field23.ClearAllFilters
Field23.CurrentPage = NewCat2
pt8.RefreshTable
End With
With pt9
Field24.ClearAllFilters
Field24.CurrentPage = NewCat1
Field25.ClearAllFilters
Field25.CurrentPage = NewCat2
Field26.ClearAllFilters
Field26.CurrentPage = NewCat3
pt9.RefreshTable
End With
With pt10
Field27.ClearAllFilters
Field27.CurrentPage = NewCat1
Field28.ClearAllFilters
Field28.CurrentPage = NewCat2
Field29.ClearAllFilters
Field29.CurrentPage = NewCat3
Field30.ClearAllFilters
Field30.CurrentPage = NewCat4
pt10.RefreshTable
End With
Application.Calculation = xlAutomatic
Application.StatusBar = "All done!"
End If
End Sub