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!
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!