VBA - Unable to get the PivotItems property of the PivotField Class, but can hide items still?

MagicalHippo

Board Regular
Joined
Oct 13, 2015
Messages
122
Really confused here. I have a Pivot table and using VBA to hide specific fields. It works, however, when it gets to the commented code it stops and throws the "unable to get pivot field property class". I can comment out this segement of code, it will run through the 100 other Pivot Fields inside of "Cost Centre" but then freeze and throw another area for another .PivotItem field within "Cost Centre". I'm confused as to what is going on....First I thought it was due to having no fields selected, as you cannot have everything set to "False", So I set my fields to true at the beginning, but that didn't resolve anything

Code:
ws3.PivotTables("PivotTable2").PivotFields("Cost Centre #"). _
        CurrentPage = "(All)"
    With ws3.PivotTables("PivotTable2").PivotFields("Cost Centre #")
        .PivotItems("430813").Visible = True
        .PivotItems("430829").Visible = True
        .PivotItems("0").Visible = False
'        .PivotItems("182249").Visible = False
        .PivotItems("182833").Visible = False
        .PivotItems("182841").Visible = False
        .PivotItems("182895").Visible = False
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
I had a similar problem. I was hiding PivotItems value 0.00. My code worked fine for years, then suddenly gave me an Unable to get the PivotItems property of the PivotField Class error:
With ActiveSheet.PivotTables("PTAssignment").PivotFields("MB5S")
.ClearAllFilters
.EnableMultiplePageItems = True
.PivotItems("0.00").Visible = False
End With

I replaced the code with:
Dim ptItem As PivotItem
With ActiveSheet.PivotTables("PTAssignment").PivotFields("MB5S")
For Each ptItem In .PivotItems
If ptItem = "0.00" Then ptItem.Visible = False
Next
End With
and this worked.

Intrigued, I added
Dim ptItem As PivotItem
Dim cnt As Long
With ActiveSheet.PivotTables("PTAssignment").PivotFields("MB5S")
For Each ptItem In .PivotItems
If ptItem = "0.00" Then
ptItem.Visible = False
cnt = cnt + 1
End If
Next
End With
Debug.Print cnt & " items hidden"
and I got:
2 items hidden
 
Upvote 0

Forum statistics

Threads
1,215,152
Messages
6,123,323
Members
449,094
Latest member
Chestertim

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