VBA to set a page field filter to yes in a pivot table

Johnny C

Well-known Member
Joined
Nov 7, 2006
Messages
1,069
Office Version
  1. 365
Platform
  1. Windows
I've got a workbook with a ton of pivot tables, most of which have a page filter based on Month. I want to be able to enter a number in a cell, then run a macro to go through all pivots and 'tick' the value in the list of month values so that the month is included.

This is the code I have. The 3 lines with Visible = True are me trying to get it done diffrent ways. They all give the same error "Unable to set the visible item of the pivotItem class".
What am I doing wrong? 6 pages of Googles say this is what you do.

VBA Code:
    Dim ws As Worksheet
    Dim pt As PivotTable
    Dim pi As PivotItem
    Dim pf As PivotField
    Dim IntLatestMonth%, strLatestMonth$
    
    On Error Resume Next
    IntLatestMonth = wksData.Range("LatestMonth").Value
    If Err.Number <> 0 Then IntLatestMonth = 0
    On Error GoTo 0
    strLatestMonth$ = CStr(IntLatestMonth)

For Each ws In ThisWorkbook.Worksheets
        For Each pt In ws.PivotTables
            Application.StatusBar = "Refreshing tab [" & ws.Name & "] Pivot table " & pt.Name
            On Error Resume Next
            pt.RefreshTable
            On Error GoTo 0
            For Each pf In pt.PageFields
                If pf.Name = "Month" Then
                     pf.PivotItems(strLatestMonth).Visible = True
                    For Each pi In pf.PivotItems
                           If pi.Name = strLatestMonth Then pi.Visible = True
                           If pi.Name = strLatestMonth Then pf.PivotItems(strLatestMonth).Visible = True
                    Next pi
                End If
            Next pf
         
            On Error GoTo 0
        Next
    Next

I even tried hard coding it and that didn't work
VBA Code:
Sheets("Sales Summary (1)").PivotTables("PivotTable1").PivotFields("Month").PivotItems("4").Visible = True

End Sub

Thanks
 
Last edited:

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Are you trying to add that item to existing selections, or set the filter to that item? You'd use Currentpage for the latter.
 
Upvote 0
I'm trying to add to existing selections. It's a Year To Date so each month we need a month adding to the existing (multiple) selection. There are other filters where the whole year is populated and we only want up to the current month.
 
Upvote 0
First obvious question: what code do you get recording a macro doing it manually?
 
Upvote 0
This is what I got:-
VBA Code:
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Month")
        .PivotItems("4").Visible = True
    End With

I saw a post on StackOverflow that said basically you can't just add to a prexisting multiple selection - what you need to do is clear the existing filters and add a new one with allow multiple filters set and rebuild them. I'm trying that at the moment
 
Upvote 0
That's not generally true. It is usually quite possible to select another additional item using code such as the code you posted. Does the code you recorded work when you run it?
 
Upvote 0
No,
if I try
VBA Code:
If pf.PivotItems("12").Visible = False Then
it says unable to get the PivotItems property of the PivotsField class

When I try and set the visible property it says "Unable to set the visible item of the pivotItem class".
 
Upvote 0
So the code from post 5 fails?
 
Upvote 0
Even that does, yes. I even tried setting
ActiveSheet.PivotTables("PivotTable1").PivotFields("Month").CurrentPage = "(All)" to see if I could deselect what I didn't want but it wouldn't do that.
 
Upvote 0
What about ActiveSheet.PivotTables("PivotTable1").PivotFields("Month").clearallfilters?
 
Upvote 0

Forum statistics

Threads
1,215,455
Messages
6,124,938
Members
449,197
Latest member
k_bs

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