Hi, I am using the code below to filter a pivot table based on a cell value that is using a dropdown. It works properly, but I cannot find a solution to use another cell value in another filter. Basically, the process would be: change cell C3, filter first field, change O3, filter second field.
If I am using a single criteria it works, but if I add another criteria it doesn't work. It throws an error on the line marked in yellow. I hope that someone has an idea on how to fix this.
If I am using a single criteria it works, but if I add another criteria it doesn't work. It throws an error on the line marked in yellow. I hope that someone has an idea on how to fix this.
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Intersect(Target, Range("C3:O3")) Is Nothing Then Exit Sub
'Set the Variables to be used
Dim pt As PivotTable
Dim Field1 As PivotField
Dim Field2 As PivotField
Dim NewCat1 As String
Dim NewCat2 As String
'Here you amend to suit your data
Set pt = Worksheets("Obj ").PivotTables("PivotTable3")
Set Field1 = pt.PivotFields("Eq")
Set Field1 = pt.PivotFields("Ach")
NewCat1 = Worksheets("Obj ").Range("C3").Value
NewCat2 = Worksheets("Obj ").Range("O3").Value
'This updates and refreshes the PIVOT table
With pt
Field1.ClearAllFilters
[COLOR=rgb(250, 197, 28)]Field1.CurrentPage = NewCat1[/COLOR]
Field2.ClearAllFilters
Field2.CurrentPage = NewCat2
pt.RefreshTable
End With
End Sub