Add multiple filter items to a Powerpivot pivot table from a named range using VBA

megan947us

New Member
Joined
Dec 7, 2018
Messages
2
I have some code that works perfectly with a normal pivot table, but does not work if I apply it to a pivot table in a PowerPivot workbook. After doing some research, it seems that VBA doesn't exactly work as usual with PowerPivot.

I have a named range of values and I'm trying to filter the pivot table with those values. When I run the code, it seems like it's not recognizing the PivotFields "AgreementNumber" which absolutely exists.

Error message: "Unable to get the PivotFields property of the PivotTable class"

Hoping someone can give some insight on how to tweak this to make it functional within a PowerPivot workbook.

Code:
Sub Button1_Click()


Dim rng As Range
Set rng = Range("FilterCriteria")
Dim ptb As PivotTable
Set ptb = Sheets("Agreements_Materials").PivotTables("PivotTable4")
Dim fld As PivotField
Set fld = ptb.PivotFields("AgreementNumber")
           
    With fld


    Dim Item As PivotItem


    For Each Item In .PivotItems
     Item.Visible = True
    Next Item
     
    For Each Item In .PivotItems
        Item.Visible = False
        Dim cell As Range
            For Each cell In rng
                If Item.Caption = cell.Text Then
                    Item.Visible = True
                    Exit For
                End If
            Next cell
    Next Item


    End With
    
End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Forum statistics

Threads
1,213,489
Messages
6,113,952
Members
448,535
Latest member
alrossman

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