Hi,
Can you help me with my code please?
I want to able to type a year into cell G3, and that will then update my pivot tables with filtering Field "FY2" or "Cal Year" with the year in cell G3 and the year before that.
eg: if I type "2018" into G3, it will filter 2018 and 2017 in my pivot tables.
Below is my code.
Apologies if it's a bit crude, I'm very new to this stuff.
Your help would be greatly appreciated.
Kind Regards
Christof
Can you help me with my code please?
I want to able to type a year into cell G3, and that will then update my pivot tables with filtering Field "FY2" or "Cal Year" with the year in cell G3 and the year before that.
eg: if I type "2018" into G3, it will filter 2018 and 2017 in my pivot tables.
Below is my code.
Apologies if it's a bit crude, I'm very new to this stuff.
Your help would be greatly appreciated.
Kind Regards
Christof
Code:
Sub test()
Application.Calculation = xlManual
'Set the Variables to be used
Dim pt, pt2, pt3, pt4 As PivotTable
Dim Field, Field2, Field3, Field4, Field5, Field6, Field7, Field8, Field9, Field10, Field11, Field12 As PivotField
Dim NewCat, NewCat2, NewCat3, NewCat4 As String
Dim pi As PivotItem
'Here you amend to suit your data
Set pt = Worksheets("Product Summary").PivotTables("PivotTable1")
Set pt2 = Worksheets("Product Summary").PivotTables("PivotTable2")
Set pt3 = Worksheets("Product Summary").PivotTables("PivotTable3")
Set pt4 = Worksheets("Product Summary").PivotTables("PivotTable4")
Set Field = pt.PivotFields("Brand")
Set Field2 = pt.PivotFields("New Product Group")
Set Field3 = pt.PivotFields("Cal Year")
Set Field4 = pt2.PivotFields("Brand")
Set Field5 = pt2.PivotFields("New Product Group")
Set Field6 = pt2.PivotFields("FY2")
Set Field7 = pt3.PivotFields("Brand")
Set Field8 = pt3.PivotFields("New Product Group")
Set Field9 = pt3.PivotFields("Cal Year")
Set Field10 = pt4.PivotFields("Brand")
Set Field11 = pt4.PivotFields("New Product Group")
Set Field12 = pt4.PivotFields("FY2")
pt.AllowMultipleFilters = True
pt2.AllowMultipleFilters = True
pt3.AllowMultipleFilters = True
pt4.AllowMultipleFilters = True
NewCat = Worksheets("Product Summary").Range("e5").Value
NewCat2 = Worksheets("Product Summary").Range("e7").Value
NewCat3 = Worksheets("Product Summary").Range("g3").Value
NewCat4 = Worksheets("Product Summary").Range("g3").Value - 1
With pt
Field.ClearAllFilters
Field.CurrentPage = NewCat
Field2.ClearAllFilters
Field2.CurrentPage = NewCat2
Field3.ClearAllFilters
Field3.CurrentPage = NewCat3 And NewCat4
pt.RefreshTable
End With
With pt2
Field4.ClearAllFilters
Field4.CurrentPage = NewCat
Field5.ClearAllFilters
Field5.CurrentPage = NewCat2
Field6.ClearAllFilters
Field6.CurrentPage = NewCat3 And NewCat4
pt2.RefreshTable
End With
With pt3
Field7.ClearAllFilters
Field7.CurrentPage = NewCat
Field8.ClearAllFilters
Field8.CurrentPage = NewCat2
Field9.ClearAllFilters
Field9.CurrentPage = NewCat3 And NewCat4
pt3.RefreshTable
End With
With pt4
Field10.ClearAllFilters
Field10.CurrentPage = NewCat
Field11.ClearAllFilters
Field11.CurrentPage = NewCat2
Field12.ClearAllFilters
Field12.CurrentPage = NewCat3 And NewCat4
pt4.RefreshTable
End With
Calculate
End Sub