• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Exclude Blank Cells from Pivot Table Filter

FarmGuy

New Member
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
 

Attachments

  • Test.xlsm
    34.8 KB · Views: 3
You have (blank) in you PivotItem, which is giving this error as it can't be resolved in DateValue function.

Also, I think you have <= & >= sign reversed? I'm assuming that you want to check Cdate that's between E2 & E3.

Revised code:
Code:
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 pvtI.Name <> "(blank)" Then
        If DateValue(pvtI.Name) >= Range("E2").Value2 And DateValue(pvtI.Name) <= Range("E3").Value2 Then
            pvtI.Visible = True
        Else
            pvtI.Visible = False
        End If
    End If
Next pvtI

End Sub

Edit: Or you can code for error trap.
 
You have (blank) in you PivotItem, which is giving this error as it can't be resolved in DateValue function.

Also, I think you have <= & >= sign reversed? I'm assuming that you want to check Cdate that's between E2 & E3.

Revised code:
Code:
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 pvtI.Name <> "(blank)" Then
        If DateValue(pvtI.Name) >= Range("E2").Value2 And DateValue(pvtI.Name) <= Range("E3").Value2 Then
            pvtI.Visible = True
        Else
            pvtI.Visible = False
        End If
    End If
Next pvtI

End Sub

Edit: Or you can code for error trap.

This still seems to select the "blank" checkbox in the filter. How do I get it to not select the blank in the filter
 
What is the issue?
If "(blank)", Count of Cdate is 0 anyways.

But you can modify the code like below.
Code:
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 pvtI.Name <> "(blank)" Then
        If DateValue(pvtI.Name) >= Range("E2").Value2 And DateValue(pvtI.Name) <= Range("E3").Value2 Then
            pvtI.Visible = True
        Else
            pvtI.Visible = False
        End If
    Else
            pvtI.Visible = False
    End If
Next pvtI

End Sub
 
Back
Top