Aubrey Reel
New Member
- Joined
- Apr 4, 2023
- Messages
- 7
- Office Version
- 365
- Platform
- 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
Additional info:
The dates from the pivot table data are in regular date format, but the pivot table filter drop down shows differently.
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
Additional info:
The dates from the pivot table data are in regular date format, but the pivot table filter drop down shows differently.