I'm trying to construct a pivot table and I am filtering the items based on a dynamic list. The list would usually consist of est. 20 items, but the pivot table would have upwards of 5,000 items.
The codes that I have now runs a loop through all 5,000 items and makes those 20 items visible. However, because the dataset is so large (5,000 items), the runtime is extremely long. Is there a better way of running this code to achieve a faster and more efficient result?
I was thinking maybe along the lines of "deselect all" 5,000 items and then find those 20 items and make them visible.
Here is the code that I have now.
Dim PI as PivotItem
lrow = Main.Cells(Rows.Count, "E").End(xlUp).Row
Set Rng = Main.Range("E1:E" & lrow)
With Main.PivotTables("PivotTable2").PivotFields("Details")
.ClearAllFilters
For Each PI In .PivotItems
PI.Visible = WorksheetFunction.CountIf(Rng, PI.Name) > 0
Next PI
End With