I have a pivot table which lists a count of how many inventory items were sold between a date range. The to and from dates are stored in cells so the user may modify them.
I've written code that references these cells and attempts to filter the pivot table on the sheet.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = ActiveSheet.Range("E3").Address Then
ActiveSheet.PivotTables("ItemsSold").RefreshTable
ElseIf Target.Address = ActiveSheet.Range("I3").Address Then
ActiveSheet.PivotTables("ItemsSold").RefreshTable
End If
ActiveSheet.PivotTables("ItemsSold").PivotFields("Date Sold ").PivotFilters.Add _
Type:=xlDateBetween, _
Value1:=CLng(Range("E3").value), _
Value2:=CLng(Range("I3").value)
End Sub
I get
"Run Time Error 1004:Application-defined or object-defined error".
Refreshing the table is working properly, but filtering it is not.
An additional complication: will this work if one of the dates (say, Date From:) does not exist on the table? For example, if I want to filter between January 1st and today, but there are no January dates in the data table, will this code still execute properly?