PivotFields Property Run time error

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

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
 

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