Peter.Stevens2
Board Regular
- Joined
- Sep 16, 2008
- Messages
- 56
Hi Folks,
I'm having a nightmare trying to speed up some code I've written to select multiple page fields in Excel 2007 Pivot tables.
I have a workbook that has several sheets with 20+ pivottables on each that make up a dashboard of sorts, there is a user form that users can use to select filters on these pivot tables and the selected filters are used to apply to all pivot tables on the active worksheet. this saves them having to make the selections on each pivot table in turn.
I'd set this up so users could select 'all' pivot items in a partcular field or just select one, for a specific team etc. This works fine and refreshes the filter on all pivot tables in a couple of seconds. However I've had a request to make some of the pivot item selections 'multiselect'. I've done this by changing the Combobox on the userform into a multiselect Listbox which populates an array when the user apply's their selections. The Array is then looped through to make the pivot items visible. the problem is that the Pivot fields in question can have up to 300ish indivudual items and it takes aaaaaages for the loop to go through each of these pivot items and make it Visible/Hidden as applicable. I've tried using pt.ManualUpdate = True to no avail.
The source data for the pivots are in the same workbook and all pivot tables run off 1 or 2 pivot caches.
The code I'm having trouble with is below, any help to fix would be greatly appreciated! Thanks All
I'm having a nightmare trying to speed up some code I've written to select multiple page fields in Excel 2007 Pivot tables.
I have a workbook that has several sheets with 20+ pivottables on each that make up a dashboard of sorts, there is a user form that users can use to select filters on these pivot tables and the selected filters are used to apply to all pivot tables on the active worksheet. this saves them having to make the selections on each pivot table in turn.
I'd set this up so users could select 'all' pivot items in a partcular field or just select one, for a specific team etc. This works fine and refreshes the filter on all pivot tables in a couple of seconds. However I've had a request to make some of the pivot item selections 'multiselect'. I've done this by changing the Combobox on the userform into a multiselect Listbox which populates an array when the user apply's their selections. The Array is then looped through to make the pivot items visible. the problem is that the Pivot fields in question can have up to 300ish indivudual items and it takes aaaaaages for the loop to go through each of these pivot items and make it Visible/Hidden as applicable. I've tried using pt.ManualUpdate = True to no avail.
The source data for the pivots are in the same workbook and all pivot tables run off 1 or 2 pivot caches.
The code I'm having trouble with is below, any help to fix would be greatly appreciated! Thanks All
Code:
'loop through each pivottable and make correct page selections
For Each pt In ThisWorkbook.ActiveSheet.PivotTables
pt.ManualUpdate = True
Application.StatusBar = "Calculating - " & Round(Counter / ptCounter, 2) * 100 & "%"
'if there are selected accounts then apply the filter
If FilterAccount = True Then
pt.PivotFields("Account").EnableMultiplePageItems = True
For Each pi In pt.PivotFields("Account").PivotItems
If pi <> "(blank)" Then pi.Visible = False
Next pi
For i = 1 To UBound(AccountArray, 1)
pt.PivotFields("Account").PivotItems(AccountArray(i)).Visible = True
Next i
pt.PivotFields("Position").EnableMultiplePageItems = True
'else select all accounts
Else
pt.PivotFields("Account").ClearAllFilters
pt.PivotFields("Account").EnableMultiplePageItems = False
pt.PivotFields("Account").CurrentPage = "(All)"
End If