I am trying to update a filter in my pivot table (date) from 2 cell input (startdate, enddate), so that I set a startdate (e.g. 01/01/12) and an enddate (28/03/12) the pivot table will automatically apply a filter displaying just the dates within the range. For this purpose, I found some VBA code snippets on the internet:
Option Explicit
Sub FilterPivotDates()
'
Dim dStart As Date
Dim dEnd As Date
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Application.ScreenUpdating = False
On Error Resume Next
dStart = Sheets("Pivot").Range("StartDate").Value
dEnd = Sheets("Pivot").Range("EndDate").Value
Set pt = ActiveSheet.PivotTable2
Set pf = pt.PivotFields("Week")
pt.ManualUpdate = True
pf.EnableMultiplePageItems = True
For Each pi In pf.PivotItems
pi.Visible = True
Next pi
For Each pi In pf.PivotItems
If pi.Value < dStart Or pi.Value > dEnd Then
pi.Visible = False
End If
Next pi
Application.ScreenUpdating = False
pt.ManualUpdate = False
Set pf = Nothing
Set pt = Nothing
End Sub
I have that in the Modules folder of my worksheet and in Sheet "Pivot" I put "Option Explicit". I assigned the range names "StartDate" and "EndDate" to the two cells with date input and assigned a button with the macro. The PivotTable is named "PivotTable" and the respective Field that should be filtered is named "Week". But when I hit the button nothing happens... Does someone see a error in the code or should I implement it differently?
Thanks upfront!