Variable Pivot Table Sorts

helpexcel

Well-known Member
Joined
Oct 21, 2009
Messages
656
Hi have code that runs pivot tables for a variable set of data. the problem i'm running into is that some of the fields I want to filter by won't show up until the data is more mature. Is there a way to look at the data field and set the filters based on the data that is present? So below is the code i'm using. At the beginning of the year, only "Active" will shown up and the code will error out because the other values don't exist. However as the year goes on these values will appear. I'm trying to automate the process so i don't have to go in and add the code as they show up. Thanks!

pvt.PivotFields("Status").PivotItems("Active").Visible = True
pvt.PivotFields("Status").PivotItems("Limited Stock").Visible = False
pvt.PivotFields("Status").PivotItems("Stock Coming").Visible = False
pvt.PivotFields("Status").PivotItems("Pending Stock").Visible = False
pvt.PivotFields("Status").PivotItems("Sold Out").Visible = False
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Something like this

Code:
Sub filterpt()
    Dim sh As Worksheet
    Dim pvt As PivotTable, pi As PivotItem
    
    Set sh = ActiveSheet
    Set pvt = sh.PivotTables("PTable1")
    
    With pvt.PivotFields("Status")
        .ClearAllFilters
        For Each pi In .PivotItems
            If pi.Value = "Active" Then
                pi.Visible = True
            Else
                pi.Visible = False
            End If
        Next
    End With
End Sub
 
Upvote 0
Thanks for responding!!

I ended up using this and it works great. I'll try your code too.

With pvt.PivotFields("Status")
On Error Resume Next
.PivotItems("Active").Visible = True
.PivotItems("Limited Stock").Visible = False
.PivotItems("Stock Coming").Visible = False
.PivotItems("Pending Stock").Visible = False
.PivotItems("Sold Out").Visible = False
On Error GoTo 0
End With
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,738
Members
448,988
Latest member
BB_Unlv

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