Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("g3:M3")
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
On Error GoTo clean_up
Application.Calculation = xlManual
Application.StatusBar = "Hi " & Application.UserName & "! Your report will be ready in a few seconds..."
Application.EnableEvents = False
'Set the Variables to be used
Dim pt1 As PivotTable
Dim pt2 As PivotTable
Dim pt3 As PivotTable
Dim pt4 As PivotTable
Dim pt7 As PivotTable
Dim Field1 As PivotField
Dim Field2 As PivotField
Dim Field3 As PivotField
Dim Field4 As PivotField
Dim Field5 As PivotField
Dim Field6 As PivotField
Dim Field7 As PivotField
Dim Field8 As PivotField
Dim Field9 As PivotField
Dim Field10 As PivotField
Dim Field21 As PivotField
Dim Field22 As PivotField
Dim Field23 As PivotField
Dim Field24 As PivotField
Dim Field25 As PivotField
Dim NewCat1 As String
Dim NewCat2 As String
Dim NewCat3 As String
Dim NewCat4 As String
Dim NewCat5 As String
Dim NewCat6 As String
Dim pi As PivotItem
Dim Yeartype As String
Yeartype = Range("h3").Value
NewCat1 = Range("e5").Value
NewCat2 = Range("g3").Value
NewCat3 = Range("g3").Value - 1
NewCat4 = "Okay"
NewCat5 = Range("e7").Value
NewCat6 = Range("e9").Value
'-----Pivot1-----
Set pt1 = PivotTables("PivotTable1")
Set Field1 = pt1.PivotFields("Brand")
Set Field2 = pt1.PivotFields("New Product Group")
Set Field3 = pt1.PivotFields("Country")
Set Field4 = pt1.PivotFields("Ignore")
pt1.ManualUpdate = True
With pt1.PivotFields(Yeartype)
.Orientation = xlRowField
.Position = 1
End With
If Yeartype = "Financial Year" Then
pt1.PivotFields("Calendar Year").Orientation = xlHidden
Set Field5 = pt1.PivotFields("Financial Year2")
Else
pt1.PivotFields("Financial Year").Orientation = xlHidden
Set Field5 = pt1.PivotFields("Calendar Year2")
End If
pt1.AllowMultipleFilters = True
pt1.ClearAllFilters
Field1.CurrentPage = NewCat1
Field2.CurrentPage = NewCat5
Field3.CurrentPage = NewCat6
Field4.CurrentPage = NewCat4
For Each pi In Field5.PivotItems
If Not ((pi.Name = NewCat2) Or (pi.Name = NewCat3)) Then
pi.Visible = False
End If
Next
pt1.ManualUpdate = False
pt1.RefreshTable
'-----Pivot2-----
Set pt2 = PivotTables("PivotTable2")
Set Field6 = pt2.PivotFields("Brand")
Set Field7 = pt2.PivotFields("New Product Group")
Set Field8 = pt2.PivotFields("Country")
Set Field9 = pt2.PivotFields("Ignore")
pt2.ManualUpdate = True
With pt2.PivotFields(Yeartype)
.Orientation = xlRowField
.Position = 1
End With
If Yeartype = "Financial Year" Then
pt2.PivotFields("Calendar Year").Orientation = xlHidden
Set Field10 = pt2.PivotFields("Financial Year2")
Else
pt2.PivotFields("Financial Year").Orientation = xlHidden
Set Field10 = pt2.PivotFields("Calendar Year2")
End If
pt2.AllowMultipleFilters = True
pt2.ClearAllFilters
Field6.CurrentPage = NewCat1
Field7.CurrentPage = NewCat5
Field8.CurrentPage = NewCat6
Field9.CurrentPage = NewCat4
For Each pi In Field10.PivotItems
If Not ((pi.Name = NewCat2) Or (pi.Name = NewCat3)) Then
pi.Visible = False
End If
Next
pt2.ManualUpdate = False
pt2.RefreshTable
'-----Pivot7-----
Set pt7 = PivotTables("PivotTable7")
Set Field21 = pt7.PivotFields("Brand")
Set Field22 = pt7.PivotFields("New Product Group")
Set Field23 = pt7.PivotFields("Country")
Set Field24 = pt7.PivotFields("Ignore")
pt7.ManualUpdate = True
If Yeartype = "Financial Year" Then
Set Field25 = pt7.PivotFields("Financial Year2")
Else
Set Field25 = pt7.PivotFields("Calendar Year2")
End If
pt7.AllowMultipleFilters = True
pt7.ClearAllFilters
Field21.CurrentPage = NewCat1
Field22.CurrentPage = NewCat5
Field23.CurrentPage = NewCat6
Field24.CurrentPage = NewCat4
Field25.CurrentPage = NewCat2
pt7.ManualUpdate = False
pt7.RefreshTable
End If
Dim sc1 As SlicerCache, si1 As SlicerItem
Set sc1 = ThisWorkbook.SlicerCaches("Slicer_Brand3")
If Not Intersect(Target, [E5]) Is Nothing Then
If Target.Value = "(All)" Then
sc1.ClearAllFilters
Else
sc1.ClearAllFilters
For Each si1 In sc1.SlicerItems
If Target.Value = si1.Name Then
si1.Selected = True
Else
si1.Selected = False
End If
Next
End If
End If
Dim sc2 As SlicerCache, si2 As SlicerItem
Set sc2 = ThisWorkbook.SlicerCaches("Slicer_New_Product_Group3")
If Not Intersect(Target, [E7]) Is Nothing Then
If Target.Value = "(All)" Then
sc2.ClearAllFilters
Else
sc2.ClearAllFilters
For Each si2 In sc2.SlicerItems
If Target.Value = si2.Name Then
si2.Selected = True
Else
si2.Selected = False
End If
Next
End If
End If
Dim sc3 As SlicerCache, si3 As SlicerItem
Set sc3 = ThisWorkbook.SlicerCaches("Slicer_Country3")
If Not Intersect(Target, [E9]) Is Nothing Then
If Target.Value = "(All)" Then
sc3.ClearAllFilters
Else
sc3.ClearAllFilters
For Each si3 In sc3.SlicerItems
If Target.Value = si3.Name Then
si3.Selected = True
Else
si3.Selected = False
End If
Next
End If
End If
Application.Calculation = xlAutomatic
Application.StatusBar = "All done!"
clean_up:
Application.EnableEvents = True
End Sub