Displaying only specific pivot items based on a list in a named range

hnpkeeper13

New Member
Joined
Jun 4, 2014
Messages
2
My question is how to display only specific entries in a pivot table which is connected to a SQL query.

For example, I have 20 fields. One of those fields is "name". There are 50 entries in that name field. I only want to show 3 specific names. In Excel, I simply use the filtering dropdown and deselect what I don't want. I have too many tables to perform this manually.

I have been able to do this with a pivot table which is getting data from a simple defined table. I used a named range to store the entries I wish to keep. I used "ActiveSheet.PivotTables(1).PivotFields("name").PivotItems(i).Visible = True" in a loop to iterate through all items and set them all first to display, then iterated through each item again, comparing against the desired names and setting Visible to False for those which did not match the entries in the named range.

The same code is not working with the SQL connected pivot table. This is due to changes in how it references the field, adding [Query] to the beginning of the field references.

If I record a macro for clues how to do this, it tries to create a VisibleItemsList from an array and use this to define the visible items using this command: ActiveSheet.PivotTables(1).PivotFields("[Query].[name].[name]").VisibleItemsList = Array("[Query].[name].&[ACTUAL TEXT OF ITEM]", "[Query].[name].&[ITEM2]", "[Query].[name].&[ITEM3]")

Since I have a large number of items and would have to produce such list on the fly, it seems impossible to use that method.

If I try to just do one (shown below), it fails with "unable to get pivotitems property of the pivotfields class"

ActiveSheet.PivotTables(1).PivotFields("[Query].[name].[name]").PivotItems("name of what I want").Visible = False

Is there a way to either access the pivotitems property, or a better way to do what I am doing?
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Possible to just avoid VBA and do something more... traditional? Say a "ShouldDisplay" field that you can filter on?
 
Upvote 0

Forum statistics

Threads
1,223,997
Messages
6,175,874
Members
452,679
Latest member
darryl47nopra

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