Filter Pivot Table based on date in cell

Aubrey Reel

New Member
Joined
Apr 4, 2023
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Good day. I am trying to filter a Pivot Table based on a date in a cell, but am having trouble getting VBA code to work. Below is the code I am using. It is not working, but also when I hit run it pulls up the Macros screen. Not sure why. I want to use the cell reference because there are hundreds of dates, so the dropdown is not efficient.

Reference cell = B31
Sheet name = Hourly Rates
Pivot Table name = PivotTable2
Pivot Table field to filter = date_week_ended

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Update by Extendoffice 20180702
Dim xPTable As PivotTable
Dim xPFile As PivotField
Dim xStr As String
On Error Resume Next
If Intersect(Target, Range("B31:B32")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable = Worksheets("Hourly Rates").PivotTables("PivotTable2")
Set xPFile = xPTable.PivotFields("date_week_ended")
xStr = Target.Text
xPFile.ClearAllFilters
xPFile.CurrentPage = xStr
Application.ScreenUpdating = True
End Sub


1680791155077.png



Additional info:
The dates from the pivot table data are in regular date format, but the pivot table filter drop down shows differently.

1680791468425.png
1680791494836.png
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Forum statistics

Threads
1,214,918
Messages
6,122,252
Members
449,075
Latest member
staticfluids

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