VBA Pivot Table: Problem with pivot item visibility

rotown21

New Member
Joined
May 22, 2013
Messages
3
I am working on a pivot table in VBA. I have a working code for creating the pivot table and filling in the fields. What I would like to do now is to filter the data by dates. In the report filter, I am trying to select specific dates that precede a user inputted date.

Say I have a list of the following dates:
25/4/2013
28/4/2013
2/5/2013
3/5/2013
5/5/2013
The user inputs for example 1/5/2013. The pivot table should then only display the data that corresponds to the dates before this (25/4/2013 and 28/4/2013).

I wrote the code for this as well, but there are two issues I am now facing:

1. When the date inputted by the user precedes the earliest date in filter field, Excel will select the last pivot item, rather than not selecting anything.
2. While the code works on some computers, it doesn't work on others. In this case, the dates selected and not selected are more random.

Below is the code:

Set pf = pt.PivotFields("FIELD2")
With pf
For Each pi In pf.PivotItems
If pi < userdate Then
pi.Visible = True
Else
pi.Visible = False
End If
Next pi
End With

Any help is appreciated!
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,093
Latest member
dbomb1414

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