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. I am using Excel 2007. I need to use the “This_Sheet_All_Fields” code, but for the Row labels instead of Report Filter Fields. Is that possible? If so, please assist with modifying the code.

  2. Hi,
    I’m trying to synchronize multiply slicers with multiply data sources:
    Sub Sync_MBFL_to_BPL_series()
    Application.ScreenUpdating = False
    Dim SLI As SlicerItem
    Dim SLC_BPL_series As SlicerCache
    Dim SLC_MBFL_series As SlicerCache
    Set SLC_BPL_series = ActiveWorkbook.SlicerCaches(“Slicer_Belt_series”)
    Set SLC_MBFL_series = ActiveWorkbook.SlicerCaches(“Slicer_Belt_series1”)
    SLC_BPL_series.ClearManualFilter
    On Error Resume Next ‘On error set to false
    For Each SLI In SLC_MBFL_series.SlicerItems
    If SLI.Selected = True And SLI.HasData = True Then SLC_BPL_series.VisibleSlicerItems(SLI.Name).Selected _
    = True Else SLC_BPL_series.VisibleSlicerItems(SLI.Name).Selected = False
    Next
    Application.ScreenUpdating = True
    End Sub
    Above script is working, except that sliceritems not present in SLC_MBFL_series will not be turned off in SLC_BPL_series. Any suggestions???
    Thanks in advance
    JHN

  3. jeffreyweir – The situation is that I will always have more sliceritems in SlicerCaches BPL then slicercaches MBFL, meaning that using “For each” function on the SlicerCaches MBFL some sliceritems will never be turned off / checked by the “For each” function.
    Do you know how I could check if sliceritems in SlicerCaches BPL are present in SlicerCaches MBFL? and if not then turn them off? (How would that code look like?)
    Thanks in advance

  4. Also, do you only want the changes to flow one way, i.e. if you alter the visible fields in slicer/pivot A, the slicer/pivot B changes accordingly, but not the other way around.
    Or do you want it to flow both ways? i.e. whichever slicer you use, the changes flow to the other pivot.

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.