VBA to expand & collapse mutiple pivot tables at the same time

jrg729

New Member
Joined
May 22, 2014
Messages
1
Hi, I have some VBA that allows for expanding or collapsing multiple pivot tables of similar structure, controlling the expand/collapse from just one of the pivot tables. The code works great one pivot item at a time, but fails when attempting to Expand Entire Field or Collapse Entire Pivot Field. My novice attempts at adjusting the code to include the Expand/Collapse Entire Field options have failed... hoping someone can help. I've included the code below, and I'm running Excel 2010.

Thanks in advance.
-jrg729

VBA Code:
Private Sub Workbook_SheetPivotTableUpdate(ByVal Sh As Object, ByVal Target As PivotTable)
    Dim ptItem As PivotItem
    Dim ptField As PivotField
    Dim pt As PivotTable
    Dim ws As Worksheet
    Dim strField As String
   
    On Error Resume Next
    Set ptItem = ActiveCell.PivotItem
    On Error GoTo Oops
   
    If Not ptItem Is Nothing Then
        Set ptField = ptItem.Parent
        With ptField
            If .Orientation = xlColumnField Or .Orientation = xlRowField Then
                With Application
                    .ScreenUpdating = False
                    .EnableEvents = False
                End With
                strField = .Name
               On Error Resume Next
                For Each ws In ActiveWorkbook.Worksheets
                    For Each pt In ws.PivotTables
                        If Not pt.TableRange1.Address(external:=True) = _
                                Target.TableRange1.Address(external:=True) Then
                            pt.PivotFields(strField).PivotItems(ptItem.Caption).ShowDetail = ptItem.ShowDetail
                        End If
                    Next pt
                Next ws
                On Error GoTo Oops
            End If
        End With
    End If
   
clean_up:
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
    Exit Sub
   
Oops:
    MsgBox Err.Description
    Resume clean_up

End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Forum statistics

Threads
1,216,027
Messages
6,128,367
Members
449,444
Latest member
abitrandom82

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top