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. This code rock thanks so much! And I know you are busy but I have a problem with the code. It works great when I select one item at at time, but if I select multiple items the other pivot tables will not update. I would really appreciate any help on this one, and thanks so much for what yall do!!!! 🙂

  2. Thanks so much for sharing the code! I have 4 pivot tables, each having 6 filters. There are 5 values in Filter #1. When I choose Value #1, #2 and #3, all 4 pivot tables change value. However, when I choose Value #4 or #5, the other pivot tables don’t change value with the first pivot table. The other filters seem to work alright. I have ~200,000 lines in the raw data. What may cause the problem and how to solve it? Thanks!!

    1. When I cut the # of lines down in the raw data, it worked fine for me. I was wondering if Excel or the code has limitation on the amount of raw data to process.

      1. I don’t have slicer. I tried the new code, but I got an error message “Application-Defined or Object-Defined Error.” it happens when i choose value 4 and 5 for one of the filterss. other filters seem to work fine.

      2. Cindi – couple of questions for you:
        1. What version of Excel are you using
        2. Do all the pivot tables share the same data source?
        3. What are the values 4 and 5 for the filter that doesn’t work? Are they text, or numbers?
        4. Are you sure that the values for 4 and 5 actually appear in the other tables?

  3. Hey Jeff,
    Thanks so much for the quick reply!! I tried the updated code and the same thing happened. When multiple items are selected on one pivot table, the others do nothing. But again even if I can’t get this figured out, the code has save me much time!!!

  4. Hi
    I am using this code in two different sheets.( So one table in sheet 1 and 9 other tables in sheet 2. when i filter for any field the Macro works fine and applies the filter to all the pivot tables but its very slow. Its takes around 15 Seconds to update all the pivot. Is this normal or can it be made work faster?

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.