Change All Pivot Tables With One Selection

Change All Pivot Tables With One Selection

There is a new sample file on the Contextures website, with a macro to change all pivot tables with one selection, when you change a report filter in one pivot table.

Change All Pivot Tables

In the sample workbook, if you change the “Item” Report Filter in one pivot table, all the other pivot tables with an “Item” filter will change.

They get the same report filter settings that were in the pivot table that you changed.

Change All Pivot Tables With One Selection

Select Multiple Items

In this version of the sample file, the “Select Multiple Items” setting is also changed, to match the setting that is in the pivot table that you changed.

In the screen shot below, the Item field has the “Select Multiple Items” setting turned off. If any other pivot tables in the workbook have an “Items” filter, the “Select Multiple Items” setting for those fields will also change.

pivotmultichange02

How It Works

The multiple pivot table filtering works with event programming. There is Worksheet_PivotTableUpdate code on each worksheet, and it runs when any pivot table on that worksheet is changed or refreshed.

For each report filter field, the code checks for the Select Multiple Items setting, to change all Pivot Tables with the same report filter field.

The code loops through all the worksheets in the workbook, and loops through each pivot table on each sheet.

Private Sub Worksheet_PivotTableUpdate _
  (ByVal Target As PivotTable)
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
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub

Download the Sample File

To test the Change All Pivot Tables code, you can download the sample file from the Contextures website.

On the Sample Excel Files page, in the Pivot Tables section, look for PT0025 – Change All Page Fields with Multiple Selection Settings.

The file will work in Excel 2007 or later, if you enable macros.

Watch the Video

To see the steps for copying the code into your worksheet, and an explanation of how the code works, watch this short video.

______________________

272 thoughts on “Change All Pivot Tables With One Selection”

  1. Hi.
    This is a great thread full of good posts. My question is:
    I’ve got a pivot derived from an OLAP cube. I would like to create separate tabs based on one of the filters on the main page. Unfortunately, the Show Report Filter Pages in the Options drop menu won’t work with an OLAP cube. Does anyone know of a workaround for this? Also, there are multiple filters on the main page, and I’d like this capability to be associated with just one of the filters, for market.
    Thanks in advance for any help you might be able to provide.
    Chris

  2. Hi Debra, This is exactly what I have been looking for and if I can get it right will save a TON of time. I am trying to modify it so it only updates PT on a single workbook. I am having a hard time getting the code correct based on the comments you made to Becca on April 11, 2012. Here is the code I am using:
    Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
    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 pt In wsMain.PivotTables
    If pt 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
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    End Sub

    1. Hi Debra, I have figure out my issue that I commented above. However, I am not sure if anyone else is having this problem, but when the macro runs the charts I have tied to the pivot tables looses some of its formatting. Specifically the ‘Plot Series On’ formatting. It resets the secondary axis back to the primary axis. Any thoughts?

  3. Hi, I use this with pivot tables from a database connection – I find that if I refresh the data connection, the pivot filters reset. My work around is to comment out the vba but is there any way around this?

  4. Great code and it’s been working perfect…until I had to move my report filter(Department) down to Row Labels
    What can I do to make this work? thank you

  5. Hi, I do an open refresh on all pivot tables and have used the program 4 times which I have to comment out and comment back in again to use the programs. I have various long solution. Do you know a quick way to have the programs running after an open refresh. The program is the master and slave program that does multi sheets pivot tables and multi Items. Amazing program I just can not start it after the refresh automatically !!
    Your expertise would be greatly appreciated.
    Private Sub Worksheet_PivotTableUpdate(ByVal target As PivotTable)
    SyncPivotFields2 target
    End Sub

  6. Sub Test6PTs() ‘ Original Code from the web by Debra Dalgleish
    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 = PivotTable
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Set pfMain = ptMain.PivotFields(“Route”)
    bMI = pfMain.EnableMultiplePageItems
    For Each pt In wsMain.PivotTables
    If pt ptMain Then
    pt.ManualUpdate = True
    Set pf = pt.PivotFields(“Route”)
    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
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.