Selecting Pivot Items with VBA

CPF2011

New Member
Joined
Sep 19, 2011
Messages
8
I have a report that I run daily and then create Pivot Tables off of it. I know how to create the code to completely un-filter the entire pivot table in order to refresh it without errors. However subsequent to that I am trying to then re-filter it - select specific pivot items from that pivot table. The issue I am running into is that at times the specific items that I want to select are not there; though the next day they could be.

In other words I have 10 possible items I want to have show up amongst 50 others. However any one of those 10 might be missing on any given day (since there was nothing associated with that lable that day).

I know how to select items individually however how do I get past the error when the code can't find the item?

This is the code I am using.

Sub AddFilter()

With ActiveSheet.PivotTables(1).PivotFields("Status")
.PivotItems("RECC").Visible = False
.PivotItems("WETC").Visible = False
.PivotItems("(Blank)").Visible = False

End With

End Sub

If, for example, that day "RECC" is not there I get the error

"Unable to get the PivotItems property of the PivotField class"

I tried several ways to "on error go to next" and they all failed. :mad:
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi

You should be able to use On Error Resume Next e.g.

Code:
Sub AddFilter()

    With ActiveSheet.PivotTables(1).PivotFields("Status")
        On Error Resume Next
        .PivotItems("RECC").Visible = False
        .PivotItems("WETC").Visible = False
        .PivotItems("(Blank)").Visible = False
        On Error GoTo 0
    End With

End Sub

HTH
DK
 
Upvote 0
Just a purely academic comment/question from a self-taught coder, but I'm wondering if there's anything better or worse about the solution I would have used: iterate through each item on the PivotField and select case on it's name.

Code:
Sub AddFilter()
Dim pi As PivotItem
    With ActiveSheet.PivotTables(1).PivotFields("Status")
        For Each pi In .PivotItems
            Select Case pi.Name
                Case "RECC", "WETC", "(blank)"
                    pi.Visible = False
            End Select
        Next pi
    End With
End Sub
 
Upvote 0
Just a purely academic comment/question from a self-taught coder, but I'm wondering if there's anything better or worse about the solution I would have used: iterate through each item on the PivotField and select case on it's name.

Code:
Sub AddFilter()
Dim pi As PivotItem
    With ActiveSheet.PivotTables(1).PivotFields("Status")
        For Each pi In .PivotItems
            Select Case pi.Name
                Case "RECC", "WETC", "(blank)"
                    pi.Visible = False
            End Select
        Next pi
    End With
End Sub

There's certainly nothing worse about your code. The only potential issue I can think of is that if there were many pivot items in the pivot field then looping through every item would be slower than using the method I posted - however, the performance difference would be negligible unless we were talking about 1000s of pivot items or the macro was being executed many times. The On Error Resume next method is ok as long as it's used properly - I've seen some examples where someone's stuck it in at the start of a proc but it then hides all errors - even if the error means that the procedure cannot complete successfully. So in that respect your code is probably better :)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,818
Members
449,049
Latest member
cybersurfer5000

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