VBA / Pivot Tables / Looping through PivotItems takes too long

LEXCERM

Active Member
Joined
Jun 26, 2004
Messages
320
Office Version
  1. 365
Platform
  1. Windows
Hi,

So, I have one line of code which quickly hides a value in a filter:-
VBA Code:
ActiveSheet.PivotTables(1).PivotFields("ABC").PivotItems("Test").Visible = False


However, I need to do the reverse and only show value "Test" in the filter. This will work, but takes far too long:-
VBA Code:
Set pt = ActiveSheet.PivotTables(1).PivotFields("ABC")
For Each pi In pt.PivotItems
    If pi.Name = "Test" Then pi.Visible = True Else pi.Visible = False
Next pi

Is there a way to use the same method as in the first example so that it hides all values except "Test"?

Thanks in advance. :)
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try this:

The first line sets the visibility of all items in the pivot field to False, and the second line sets the visibility of the "Test" item to True. This will hide all items in the pivot field except for "Test".

VBA Code:
ActiveSheet.PivotTables(1).PivotFields("ABC").PivotItems.Visible = False
ActiveSheet.PivotTables(1).PivotFields("ABC").PivotItems("Test").Visible = True

Alternatively you can use the following method:

VBA Code:
ActiveSheet.PivotTables(1).PivotFields("ABC").PivotItems. _
    EnableMultiplePageItems = True
ActiveSheet.PivotTables(1).PivotFields("ABC").PivotItems("Test"). _
    Visible = True
 
Upvote 0
Thanks for the reply.

I get this error when executing first lines of each example:-

1673441534993.png
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,443
Members
448,898
Latest member
drewmorgan128

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