Use VBA to change Pivot Table Filter

Healey33

New Member
Joined
May 28, 2012
Messages
19
Hey, I have a Pivot table called "PivotTable1" in Excel and would like to change the filter on it using VBA. The field I would like to change is a date field called "LEASE SIGNED BY LANDLORD". I would like to select all of the dates and leave out the blanks (doing this manually requires checking the "All" checkbox then unchecking the "Blanks" so I am assuming the code would have to be in that order as well?) The code I have so far is :
Code:
With Sheets("Sales Dashboard input").PivotTables("PivotTable1").PivotFields("LEASE SIGNED BY LANDLORD")

.PivotItems("All").Visible = True
.PivotItems("Blank").Visible = False

End With

I receive the error "Run-time error '1004': Unable to get the PivotItems property of the PivotFields class" on the .PivotItems("All").Visible = True line. I have triple checked that the sheet name, pivot table name and field name are correct so that is not the issue.

This is slightly urgent (I need it working for tomorrow night) so any help is greatly appreciated!

Thanks,
Healey33
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hey, I have a Pivot table called "PivotTable1" in Excel and would like to change the filter on it using VBA. The field I would like to change is a date field called "LEASE SIGNED BY LANDLORD". I would like to select all of the dates and leave out the blanks (doing this manually requires checking the "All" checkbox then unchecking the "Blanks" so I am assuming the code would have to be in that order as well?) The code I have so far is :
Code:
With Sheets("Sales Dashboard input").PivotTables("PivotTable1").PivotFields("LEASE SIGNED BY LANDLORD")

.PivotItems("All").Visible = True
.PivotItems("Blank").Visible = False

End With

I receive the error "Run-time error '1004': Unable to get the PivotItems property of the PivotFields class" on the .PivotItems("All").Visible = True line. I have triple checked that the sheet name, pivot table name and field name are correct so that is not the issue.

This is slightly urgent (I need it working for tomorrow night) so any help is greatly appreciated!

Thanks,
Healey33

Try Adding This instead of the current items:
.PivotItems("All").Visible = True
.PivotItems("Blank").Visible = False
Code:
.PivotFields("LEASE SIGNED BY LANDLORD").PivotItems("(blank)").Visible = False
 
Upvote 0
Try Adding This instead of the current items:
.PivotItems("All").Visible = True
.PivotItems("Blank").Visible = False
Code:
.PivotFields("LEASE SIGNED BY LANDLORD").PivotItems("(blank)").Visible = False

Ok so, I tried that and it DOES uncheck "(blank)" (This is what I wanted) but when I add the "(All)" line, that is where I receive the error. I don't understand why you wouldn't be allowed to check the "All" checkbox using code. Any ideas?

I have the code for the "All" line the same as the blank one but I will post it anyways.
Code:
With Sheets("Sales Dashboard input").PivotTables("PivotTable1")
.PivotFields("LEASE SIGNED BY LANDLORD").PivotItems("(All)").Visible = True
.PivotFields("LEASE SIGNED BY LANDLORD").PivotItems("(blank)").Visible = False
End With
 
Upvote 0
Well All should be selected by default right? unless upon the time of macro run, the filter is in place and all needs to be checked.
 
Upvote 0
Well All should be selected by default right? unless upon the time of macro run, the filter is in place and all needs to be checked.

Silly me. Yes, I've now checked "all" and the code just unchecks blank. Thank you, this works exactly as desired.
 
Upvote 0
I am still having trouble with enabling a filter (setting a pivot items property to true). I have another pivot table that shows just the past week, filtering on a date field and i have it looping through, checking only the records form the past week. Here is the code:

Code:
Dim pt As PivotTable
Dim x As Integer
Dim day As Date
Dim day1 As Date
day = Now()
x = 0
    Worksheets("Sales Dashboard input").Select
    For Each pt In ActiveSheet.PivotTables
        pt.RefreshTable
    Next pt
    
    Worksheets("Eng Items").Select
    For Each pt In ActiveSheet.PivotTables
        pt.RefreshTable
    Next pt
    Worksheets("Sales Dashboard input").Select
With Sheets("Sales Dashboard input")
.PivotTables("PivotTable22").ManualUpdate = True
Application.EnableEvents = False
Application.ScreenUpdating = False
.PivotTables("PivotTable1").PivotFields("LEASE SIGNED BY LANDLORD").PivotItems("(blank)").Visible = False
Do While x < 7
day1 = day - x
.PivotTables("PivotTable22").PivotFields("LEASE SIGNED BY LANDLORD").PivotItems(day1).Visible = True
Loop


End With

I receive the same error, "Run-time error '1004': Unable to get the PivotItems property of the PivotFields class" on the ".PivotTables("PivotTable22").PivotFields("LEASE SIGNED BY LANDLORD").PivotItems(day1).Visible = True" line

Is there something I need to do differently in order to set the pivotitems property to True?
 
Upvote 0

Forum statistics

Threads
1,215,233
Messages
6,123,771
Members
449,122
Latest member
sampak88

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