VBA code to exclude items in filter on a pivot table filter

I am relatively new to VBA. I can’t find a code to exclude items in a filter on a pivot table filter. I am currently trying this but I get always get an error with the filter. For this one I get an error 1004 “Unable to get the Pivotfields property of the pivottable class”. Ideally I would also like to add a function that first selects all items in the filter then excludes multiple items.

Sub ExcludeItemsFromFilter()

Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem

'Set the pivot table object
Set pt = ActiveSheet.PivotTables("PivotTable3")

'Set the pivot field object
Set pf = pt.PivotFields("Headquarter")     **ERROR ON THIS LINE**

'Loop through all the pivot items in the pivot field object
For Each pi In pf.PivotItems
    If pi.Name = "Applied Materials, Austin (58460)" Or pi.Name = "Applied Materials, Inc., Dallas (33741)" Then
        pi.Visible = False 'Exclude the item from the filter
    End If
Next pi

End Sub

I have also tried this and I get a error 438 “Object doesn’t support this property or method”.

Sub ExcludeItemsFromBICubeFilter()

Dim objCube As Object
Dim objFilter As Object
Dim strFilterName As String
Dim strItemName As String

'Set the filter name and item names to exclude
strFilterName = "Headquarter"
strItemName = "Applied Materials, Austin (58460)"

'Set the cube object
Set objCube = ActiveSheet.PivotTables("PivotTable3")

'Set the filter object
Set objFilter = objCube.Filters(strFilterName)   **ERROR ON THIS LINE**

'Exclude the specified item from the filter
objFilter.Members(strItemName).Exclude = True

End Sub

I tried the above 2 codes and was trying to create a VBA code to exclude items in filter on a pivot table filter. I got errors

New contributor

Andrew Levin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

I get the same 1004 error when it gets to the pivot field. I did successfully run your sample though. Pivot table screenshot

New contributor

Andrew Levin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Since you are setting pt to ActiveSheet.PivotTables("PivotTable3") make sure you are on the sheet with PivotTable3 when you run your macro, otherwise you will get error 1004.

I created a clean workbook with a very simple data table (below), inserted a Pivot Table (renamed to PivotTable3 to match your code), and ran your first code as-is (except for adding an apostrophe in your line with the error to separate the comment). Everything worked as expected – if the above tip doesn’t work, can you try the same and see if it’s a problem with your code or the workbook?

Data table:

Result:

Code:

Sub ExcludeItemsFromFilter()

Dim ws as Worksheet
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem

'Set worksheet object
Set ws = Sheets("your_sheet_with_pivotttable3") 'replace with your sheet name

'Set the pivot table object
Set pt = ws.PivotTables("PivotTable3")

'Set the pivot field object
Set pf = pt.PivotFields("Headquarter")     ' **ERROR ON THIS LINE**

'Loop through all the pivot items in the pivot field object
For Each pi In pf.PivotItems
    If pi.Name = "Applied Materials, Austin (58460)" Or pi.Name = "Applied Materials, Inc., Dallas (33741)" Then
        pi.Visible = False 'Exclude the item from the filter
    End If
Next pi

End Sub

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật