Hi all,
These forums have helped me quite a bit over the past few months as I've gotten to grips with a new job that required a lot more Excel use than before, and today I'm hoping you can help with a problem I'm having. The below code is used to change the date filter on all the pivot tables in a worksheet when I change the pivot on this tab:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
On Error Resume Next
Dim wsMain As Worksheet
Dim ws As Worksheet
Dim ptMain As PivotTable
Dim pt As PivotTable
Dim pfMain As PivotField
Dim pf As PivotField
Dim pi As PivotItem
Dim bMI As Boolean
On Error Resume Next
Set wsMain = ActiveSheet
Set ptMain = Target
Application.EnableEvents = False
Application.ScreenUpdating = False
For Each pfMain In ptMain.PageFields
bMI = pfMain.EnableMultiplePageItems
For Each ws In ThisWorkbook.Worksheets
For Each pt In ws.PivotTables
If ws.Name & "_" & pt <> wsMain.Name & "_" & ptMain Then
pt.ManualUpdate = True
Set pf = pt.PivotFields(pfMain.Name)
bMI = pfMain.EnableMultiplePageItems
With pf
.ClearAllFilters
Select Case bMI
Case False
.CurrentPage = pfMain.CurrentPage.Value
Case True
.CurrentPage = "(All)"
For Each pi In pfMain.PivotItems
.PivotItems(pi.Name).Visible = pi.Visible
Next pi
.EnableMultiplePageItems = bMI
End Select
End With
bMI = False
Set pf = Nothing
pt.ManualUpdate = False
End If
Next pt
Next ws
Next pfMain
wsMain.Select
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
This worked just fine in a worksheet I built before, but another worksheet won't accept it. Changing the pivot on this tab now resets all the other pivots to have no date filter, rather than the selected one. Strangely, the (blank) filter is the only one left unselected (which exists because I've built the pivot tables using columns rather than a range).
Example: My data has 01/09/2012 and 01/10/2012 as possible filters. Changing Pivot 1 to show 01/10/2012 should automatically change Pivot 2 to the same date. But it doesn't, rather Pivot 2 gets it's filter reset to show both dates, but not (blank).
This leads me to believe that the problem doesn't occur until:
Select Case bMI
Case False
.CurrentPage = pfMain.CurrentPage.Value
Case True
.CurrentPage = "(All)"
For Each pi In pfMain.PivotItems
.PivotItems(pi.Name).Visible = pi.Visible
Next pi
.EnableMultiplePageItems = bMI
End Select
But I can't figure out why... Can anyone shed some light on this, please?
Bookmarks