Filter specific date in a Pivot table

Pranesh

Board Regular
Joined
Jun 29, 2014
Messages
219
Hello All,

Im trying to filter a specific date in a pivot table but im receiving "Application-deifned or object-defined error".

I tried to record macro and got the below output, but while i try to run the same recorded output again im getting the above mentioned error.

VBA Code:
ActiveSheet.PivotTables("PivotTable4").PivotFields("Date"). _
        ClearAllFilters
    ActiveSheet.PivotTables("PivotTable4").PivotFields("Date"). _
        CurrentPage = "9/18/2020"

In sheet1 range("A5") i have a date and that should be applied in the pivot table. Can someone help me..
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hi @Pranesh try this:

VBA Code:
Sub change_filter()
Dim curdate As String

curdate = ActiveSheet.Range("A5").Value

Sheets("Sheet1").PivotTables("PivotTable1").PivotFields("Date").CurrentPage = curdate

End Sub

Note that ".CurrentPage" is used to refer filter section of Pivot table.
 
Upvote 0
Sub change_filter() Dim curdate As String curdate = ActiveSheet.Range("A5").Value Sheets("Sheet1").PivotTables("PivotTable1").PivotFields("Date").CurrentPage = curdate End Sub
Hi Pankaj - Thanks for responding. I tried your code and now im getting "Subscript out of range error" in the below code

VBA Code:
Sheets("Sheet1").PivotTables("PivotTable1").PivotFields("Date").CurrentPage = curdate
 
Upvote 0
@Pranesh you need to make sure few things.
1. If your Sheet name, Pivot table name, and filter name are different from what is there in code, you need to make those changes on your own.
2. Your date format in cell A5 and in pivot filter should be same. Example, I was using 31-Mar-20.

If it still doesn't solve the problem, you can share your data and that way, I can help you better.
 
Upvote 0
@Pranesh you need to make sure few things.
1. If your Sheet name, Pivot table name, and filter name are different from what is there in code, you need to make those changes on your own.
2. Your date format in cell A5 and in pivot filter should be same. Example, I was using 31-Mar-20.

If it still doesn't solve the problem, you can share your data and that way, I can help you better.
Yes Pankaj.. I have changed sheet name, pivot table name & pivot field name in my file and then only i tried this code.
The date format in cell A5 & Pivot are mm/dd/yyyy.
 
Upvote 0
Hi @Pranesh , here's the data I used to test the code. Once I change the date in cell A5 and run the code (by pressing Alt+F8 and selecting change_filter), it's working.
Capture.PNG
 
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,174
Members
448,870
Latest member
max_pedreira

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