VBA Pivot table selection

kizmet311

New Member
Joined
Sep 7, 2018
Messages
8
I am working on a VBA for a pivot table. I have several items that I wan to deselect, which I have figure out how to do. I will be running this multiple times a month the data will be changing each time. I have a set list of items that I want deselected, i am getting an error
"Run-time error '1004': Unable to get the PivotItems property of the PivotField class"
when deselected item is not in the data set. How can i resolve this. Below is sample code, the last line is currently erroring.

With ActiveSheet.PivotTables("PivotTable2").PivotFields("Object")
.PivotItems("0113").Visible = False
.PivotItems("0120").Visible = False
.PivotItems("0131").Visible = False
.PivotItems("0140").Visible = False
.PivotItems("0150").Visible = False
.PivotItems("0291").Visible = False
 

Some videos you may like

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

kizmet311

New Member
Joined
Sep 7, 2018
Messages
8
I was able to figure out my issues. see working code.

Dim objarray() As Variant
objarray = Array("0113", "0120", "0131", "0140", "0150", "0291", "0291A", "0299")
For I = LBound(objarray) To UBound(objarray)
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Object")
On Error Resume Next
.PivotItems(objarray(I)).Visible = False
On Error GoTo 0
End With
Next I
 

Watch MrExcel Video

Forum statistics

Threads
1,122,193
Messages
5,594,767
Members
413,933
Latest member
Msar5586

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
Top