Is the filter in the Filter section of the table or the row section ?
If its in the Filter section try this:
VBA Code:
pvFld.CurrentPage = CDate(dt)
If its in the row section try this:
VBA Code:
pvFld.PivotFilters.Add2 Type:=xlSpecificDate, Value1:=dt
Note: in the 2nd one if Add2 doesn't work try Add, I think its excel version dependant with Add2 working from 2013 on.
Thanks, Its in the filters and tried the code and didn't work. I was searching around and came across one of my older posts, I am now using this code, which seems to work fine with multiple pivot tables, but how would I condense this for just one pivot table? Its failing at
Field2.CurrentPage = NewCat2
Basically I'm trying to change 3 filters in the pivot table filters so users can get the data based on a dashboard selection. I tried using pt1 across the board, that didn't work. I don't think I need to clear the filters each time, or that just removes what I just filtered. It does filter the Date field, which is the first Field, but fails on the second.
-------
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'This line stops the worksheet updating on every change, it only updates when cell
'H6 or H7 is touched
If Intersect(Target, Worksheets("Dashboard").Range("M6")) Is Nothing Then Exit Sub
'Set the Variables to be used
Dim pt1 As PivotTable
Dim pt2 As PivotTable
Dim pt3 As PivotTable
Dim Field1 As PivotField
Dim Field2 As PivotField
Dim Field3 As PivotField
Dim NewCat1 As String
Dim NewCat2 As String
Dim NewCat3 As String
'Here you amend to suit your data
Set pt1 = Worksheets("Results").PivotTables("ResultsPivotTbl")
Set pt2 = Worksheets("Results").PivotTables("ResultsPivotTbl")
Set pt3 = Worksheets("Results").PivotTables("ResultsPivotTbl")
Set Field1 = pt1.PivotFields("cc-StartDate")
Set Field2 = pt2.PivotFields("Direct")
Set Field3 = pt3.PivotFields("Code")
NewCat1 = Worksheets("Dashboard").Range("M6").Value
NewCat2 = Worksheets("Dashboard").Range("L6").Value
NewCat3 = Worksheets("Dashboard").Range("O6").Value
'This updates and refreshes the PIVOT table
With pt1
Field1.ClearAllFilters
Field1.CurrentPage = NewCat1
pt1.RefreshTable
End With
With pt2
Field2.ClearAllFilters
Field2.CurrentPage = NewCat2
pt2.RefreshTable
End With
With pt2
Field3.ClearAllFilters
Field3.CurrentPage = NewCat3
pt3.RefreshTable
ActiveWorkbook.RefreshAll
End With
End Sub