Excel 2013. I am using 3 combo boxes to change filters on the pivot table. My first combo box has "Project1", "Project2" & All. My second combo box has "Customer1", "Customer2" & All. My third combo box has "Country1", "Country2" & All.
I am using 9 pivot tables, all of them have filters as [Project], [Customer], [Country].
My intention is to change first combo box to Project1 & all the pivot tables filter should change as Project1.I am successfully able to do that.
However when I select the first combo box as "All". First Combo box cell link to Y1. I get VBA Run time error 1004: Application-defined or object-defined error.
Sub ProjectName()
ActiveSheet.PivotTables("PVT1").PivotFields("Project Name").ClearAllFilters
ActiveSheet.PivotTables("PVT2").PivotFields("Project Name").ClearAllFilters
ActiveSheet.PivotTables("PVT3").PivotFields("Project Name").ClearAllFilters
ActiveSheet.PivotTables("PVT1").PivotFields("Project Name").CurrentPage = Range("Y1").Text
ActiveSheet.PivotTables("PVT2").PivotFields("Project Name").CurrentPage = Range("Y1").Text
ActiveSheet.PivotTables("PVT3").PivotFields("Project Name").CurrentPage = Range("Y1").Text
Since the first three lines of code go without issue, I will assume that the Pivot Table
PVT1and the FieldProject Nameall exist. This places the error somewhere after that.For the call to
.CurrentPageyou will get a 1004 error for the following reasons:Report Filter. You cannot use theCurrentPageto filter any rows or columnsOn the second point, this is where the call to
Rangemight be relevant..Textwhich will use the display value of the cell and not its underlying.ValueTo resolve these issues, there are a couple of options:
PivotItemsand setVisible = True/FalseCurrentPage, you can iterate thePivotItemsfor thatPivotFieldand check that one matches. The code for that is very similar to theFor Eachloop with the check on value, just don't setVisible.Code for setting a filter on a row or column
Picture of ranges