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.
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