Dhanabalan
New Member
- Joined
- Sep 21, 2018
- Messages
- 3
Hi Just copy the code from online for Pivot table filter base on Cell Reference. Its working fine f no ("blank") in filter. Its blanks its shows Error so i add this Code pf.PivotItems("(blank)").Visible = False Now i getting Run time error 13. Its Date filter base on cell Reference, Any one can help to fix this code
Code:
Sub Filter_PivotField()
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Date Audited")
.PivotItems("(blank)").Visible = False
End With
'Description: Filter a pivot table for a specific date or period
Dim sSheetName As String
Dim sPivotName As String
Dim sFieldName As String
Dim sFilterCrit As Double
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
'Set the variables
sSheetName = "Sheet4"
sPivotName = "PivotTable1"
sFieldName = "Date Audited"
'sFilterCrit = "22/08/2017" --most recent date
sFilterCrit = ThisWorkbook.Worksheets("Sheet4").Range("C2").Value
Set pt = ThisWorkbook.Worksheets("Sheet4").PivotTables("PivotTable1")
Set pf = pt.PivotFields("Date Audited")
pf.ClearAllFilters
pf.PivotItems("(blank)").Visible = False
For Each pi In pf.PivotItems
If CDbl(DateValue(pi)) = sFilterCrit Then
pi.Visible = True
Else
pi.Visible = False
End If
Next pi
End Sub