Hi All,
One of the Excel ninjas here a VBA script that allows me to change a filter on several pivot tables at once by inputting a value on a cell. Problem is, I don't know how to optimise it as every time I ask it to update a pivot table, it clears the values on the filter, changes it to the value I want and refreshes everything. Multiply this by even 10 pivot tables and you have yourself a script that takes up a lot of memory and time.
Would love your thoughts on how to optimise this so that it runs faster. Here's the code below:
One of the Excel ninjas here a VBA script that allows me to change a filter on several pivot tables at once by inputting a value on a cell. Problem is, I don't know how to optimise it as every time I ask it to update a pivot table, it clears the values on the filter, changes it to the value I want and refreshes everything. Multiply this by even 10 pivot tables and you have yourself a script that takes up a lot of memory and time.
Would love your thoughts on how to optimise this so that it runs faster. Here's the code below:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("C9:C10")) Is Nothing Then Exit Sub
Dim pt As PivotTable
Dim Field As PivotField
Dim NewCat As String
Set pt = Worksheets("Pivots").PivotTables("PivotTable2")
Set Field = pt.PivotFields("Debtor Name")
NewCat = Worksheets("Benchmark Tables").Range("C9").Value
With pt
Field.ClearAllFilters
Field.CurrentPage = NewCat
pt.RefreshTable
End With
Set pt = Worksheets("Pivots").PivotTables("PivotTable5")
Set Field = pt.PivotFields("Debtor Name")
NewCat = Worksheets("Benchmark Tables").Range("C9").Value
' This updates and refreshes the PIVOT table
With pt
Field.ClearAllFilters
Field.CurrentPage = NewCat
pt.RefreshTable
End With
Set pt = Worksheets("Pivots").PivotTables("PivotTable8")
Set Field = pt.PivotFields("Debtor Name")
NewCat = Worksheets("Benchmark Tables").Range("C9").Value
' This updates and refreshes the PIVOT table
With pt
Field.ClearAllFilters
Field.CurrentPage = NewCat
pt.RefreshTable
End With
End Sub