Run Time Error 1004 'Unable to get the PivotFields property of the PivotTable class'

I am trying to use the code to select all the rows under one of the subheaders in a pivot table. I am getting a run time error 1004:

Unable to get the PivotFields property of the Pivot Table class

From this code:

 Sub ttest() Dim pt As PivotTable Set pt = Sheets("Report").PivotTables("PivotTable1") pt.PivotFields("Row Labels").PivotItems("CL").DataRange.Select End Sub 

Why?

2

2 Answers

I may add an extra answer (as I found nothing on that):

For people working with Data Model Pivot Tables, field names are not obvious ([tablename].[fieldname].[fieldname2]). Hence I suggest using following loop to understand what fields are currently in your pivot table:

Dim pt As PivotTable Dim pf As PivotField Set pt = ActiveSheet.PivotTables(ptName) For Each pf In pt.PivotFields Debug.Print pf.Name Next 

This way you'll have all the names listed in the immediate view.

2

As JosieP said in the comments, the 1004 error means that there is no such object, that is there is no such pivot field that is called "Row Labels".

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJoaHBsYm6EdYKOq6ynZaSeuqZ5xKupqKpdZn1xgIyupZqanJp6tbuMoJytZaSdsm68yK%2BmrZ6Zmrmlv4ypqaiolafBunnOn2StoJVivarCzq2rmpqcmnqkuA%3D%3D