I need some help with some poetry. I want a macro that will filter a pivot table based on items listed in a range in another worksheet. Here is what I have so far:
Sub Filter_PT_By_List
'Filter a pivot table to exclude all items in the "DEPT" field except for those listed in "MyRange"
For Each PivotItem In Worksheets("Sheet1").PivotTables("PivotTable1").PivotFields("DEPT").PivotItems
'
If WorksheetFunction.IsNA(WorksheetFunction.VLookup(PivotItem.Name, Worksheets("Sheet2").Range("MyRange"), 1, False)) Then
PivotItem.Visible = False
Else
PivotItem.Visible = True
End If
Next PivotItem
End Sub
The line that begins with "If WorksheetFunction..." seems awkward. That line is supposed to emulate a VLookup function which, if it evaluates to "#N/A", causes the PivotItem to be hidden. I either need to make this line work or find a completely different approach.
Thanks
Moderator Edit:
Welcome to the forum, MikeWard100.
Please notice that code tags have been added to your post. The forum rules require them so please keep that in mind and add them yourself whenever showing code in any of your future posts. To see instructions for applying them, click on the Forum Rules button at the top of the page and read Rule #3.
Thanks.
Bookmarks