I am trying to select a date range in a pivot table whose ultimate function in the attached sample is to count the number of times that cell isn't blank. When using the code below, I get a type mismatch error. Suggestions on how to fix this code so that it excludes the cells that don't have a date from the filter?
Sub PageItemFilter()
Dim pvtF As PivotField
Dim pvtI As PivotItem
Set pvtF = Worksheets("PivotTables").PivotTables("PivotTable5").PivotFields("CDate")
For Each pvtI In pvtF.PivotItems
If DateValue(pvtI.Name) <= Range("E2").Value2 And DateValue(pvtI.Name) >= Range("E3").Value2 Then
pvtI.Visible = True
Else
pvtI.Visible = False
End If
Next pvtI
End Sub
Sub PageItemFilter()
Dim pvtF As PivotField
Dim pvtI As PivotItem
Set pvtF = Worksheets("PivotTables").PivotTables("PivotTable5").PivotFields("CDate")
For Each pvtI In pvtF.PivotItems
If DateValue(pvtI.Name) <= Range("E2").Value2 And DateValue(pvtI.Name) >= Range("E3").Value2 Then
pvtI.Visible = True
Else
pvtI.Visible = False
End If
Next pvtI
End Sub