using cell data to filter pivot table (date field)

riverstar

New Member
Joined
May 9, 2012
Messages
2
I am using a macro to update the a filter on a pivot table. The below script works great when the field is text however I can not get it to work with a date value.

I have tried several differnt changes but cannot get it to work. It will clear the filter but does not appear to recognise the date value from the cell as a valid selection. I have even tried to converting the date to a text value first with no luck.

My Pivot table (Campaigns) has two filters (Group and Date) which both need to be updated from different cells (A1 and B1). Below is the script that works and updates the Group filter.

Any help would be much appreciated. I am new to playing with VBA and seem to be completely lost with this one.


Sub Apply_Reporting_Group_Filter()
Dim pvtTable As PivotTable
Dim pvtField As PivotField
Dim pvtItem As PivotItem
Dim filterReportingGroup As String

Set pvtTable = Worksheets("Sheet1").PivotTables("Campaigns")
Set pvtField = pvtTable.PivotFields("Reporting Group")

filterReportingGroup = Worksheets("Data Control").Range("A1")

For Each pvtItem In pvtField.PivotItems
If pvtItem.Value = filterReportingGroup Then
pvtField.CurrentPage = filterReportingGroup
Exit For
End If
Next pvtItem
End Sub
 
Last edited:
@AlphaFrog Many thanks for your reply.

I've been trying to get my head round how to adapt it to filter by a range of dates. For example if I had in Cell T3 01 Feb 2020 and in T4 30 Apr 2021.

I saw here XlPivotFilterType enumeration (Excel) that instead of xlSpecificDate, I could use xlBeforeOrEqualTo and xlAfterOrEqualTo, so tried to adapt your code like this:
VBA Code:
Sub Filter_PT_Date_Range()
    Dim pt As PivotTable
    For Each pt In Sheet2.PivotTables
        With pt.PivotFields("Event Date")
            .ClearAllFilters
            .PivotFilters.Add Type:=xlAfterOrEqualTo, Value1:=Format(Range("T3").Value, "dd mmm yyyy")
            .PivotFilters.Add Type:=xlBeforeOrEqualTo, Value1:=Format(Range("T4").Value, "dd mmm yyyy")
        End With
    Next pt
End Sub

But when I run ran the code, nothing happened (none of the charts updated). I though perhaps the field name "Event Date" needed to be referenced differently, so I recorded a macro as below, to see how these fields would be referenced and then try to adapt it to the code. The recording gave me:
VBA Code:
Sub Filter_PT_Date_Range()
' Macro1
    ActiveWorkbook.ShowPivotTableFieldList = True
    With ActiveSheet.PivotTables("PivotTable3").CubeFields( _
        "[q Events].[Event Date]")
        .Orientation = xlPageField
        .Position = 1
    End With
End Sub
...so I tried "[q Events].[Event Date]" instead of "Event Date" , but that didn't work, so I tried this:
VBA Code:
Sub Filter_PT_Date_Range()
    Dim pt As PivotTable
    For Each pt In Sheet2.PivotTables
        With pt.CubeFields("[q Events].[Event Date]")
            .ClearAllFilters
            .PivotFilters.Add Type:=xlAfterOrEqualTo, Value1:=Format(Range("T3").Value, "dd mmm yyyy")
            .PivotFilters.Add Type:=xlBeforeOrEqualTo, Value1:=Format(Range("T4").Value, "dd mmm yyyy")
        End With
    Next pt
End Sub
... but that gives me a compile error at .ClearAllFilters.


1620988310657.png



Where am I going wrong?
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,224,828
Messages
6,181,214
Members
453,024
Latest member
Wingit77

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top