macronovice14
New Member
- Joined
- Sep 22, 2015
- Messages
- 1
Hi,
I am trying to run a macro to expand/collapse pivot fields whenever the pivot table is updated. Pivot table is filtered using a slicer that is connected to one of the pivot table row fields (not a pagefield). The first time I make a selection in the slicer and pivot table is filtered, required field is being expanded. However, when the I clear the selection from the slicer so that pivotfield will not have any filter, the code is going into some kind of loop. And when I make a slicer selection again, it is throwing a run time error [Run-time error '1004': Unable to get the PivotFields property of the PivotTable class]. I am using Excel 2013.
I searched quite a bit but couldnt find any solution. Any help is much appreciated
Thanks!!
Here is the code I'm using
I am trying to run a macro to expand/collapse pivot fields whenever the pivot table is updated. Pivot table is filtered using a slicer that is connected to one of the pivot table row fields (not a pagefield). The first time I make a selection in the slicer and pivot table is filtered, required field is being expanded. However, when the I clear the selection from the slicer so that pivotfield will not have any filter, the code is going into some kind of loop. And when I make a slicer selection again, it is throwing a run time error [Run-time error '1004': Unable to get the PivotFields property of the PivotTable class]. I am using Excel 2013.
I searched quite a bit but couldnt find any solution. Any help is much appreciated

Thanks!!
Here is the code I'm using
Code:
Private Sub Workbook_SheetPivotTableUpdate(ByVal Sh As Object, ByVal Pvt As PivotTable)
Application.ScreenUpdating = False
If Sh.Name = "pivots" Then
If Pvt.Name = "cost_element" Then
Debug.Print Pvt.PivotFields("Cost Element").Name
If Pvt.PivotFields("Cost Element").AllItemsVisible = True Then
Pvt.PivotFields("Cost Element").ShowDetail = False
Else
Pvt.PivotFields("Cost Element").ShowDetail = True
End If
'update_chart Sh.Name, Pvt.Name
End If
ElseIf Sh.Name = "Cost Review" Then
Exit Sub
End If
Application.ScreenUpdating = True
End Sub