I am getting an object defined error 1004 when i try to run a macro to update the pviot. I want the pivot and chart to update daily so it always shows the last 30 days
'pivot filter refresh Dim DateToday As Date DateToday = Date Dim PvtTbl As PivotTable Set PvtTbl = Worksheets("Sheet1").PivotTables("Pivot1") PvtTbl.PivotFields("DateAdded").PivotFilters. _ Add2 Type:=xlDateBetween, Value1:=DateToday - 30, Value2:=DateToday
I was able to reproduce your problem with one of my own pivot tables. The solution is to
ClearAllFilters from that particular pivot field before attempting to apply your new filter.
One additional check that may be necessary (and not shown here) is to check to make sure the field you’re filtering is actually visible before filtering.
Option Explicit Sub test() Dim dateToday As Date dateToday = Date Dim pvtTbl As PivotTable Set pvtTbl = Staffing.PivotTables(1) Dim ptField As PivotField Set ptField = pvtTbl.PivotFields("FiscalMonth") ptField.ClearAllFilters ptField.PivotFilters.Add2 Type:=xlDateBetween, _ Value1:=dateToday - 30, _ Value2:=dateToday End Sub