Hi, I am following this code and works perfect, however I need to update 3 pivot tables on 3 different tabs.
http://dedicatedexcel.com/how-to-control-excel-pivot-tables-from-a-cell-value-with-vba/
I just repeated each line 3x, assigned new variables and it worked, but there should be a better way?
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, Range("F12:F13")) 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("Monthly").PivotTables("PivotTable2")
Set pt2 = Worksheets("Weekly").PivotTables("PivotTable4")
Set pt3 = Worksheets("Daily").PivotTables("PivotTable1")
Set Field1 = pt1.PivotFields("Core")
Set Field2 = pt2.PivotFields("Core")
Set Field3 = pt3.PivotFields("Core")
NewCat1 = Worksheets("Dashboard").Range("F12").Value
NewCat2 = Worksheets("Dashboard").Range("F12").Value
NewCat3 = Worksheets("Dashboard").Range("F12").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
http://dedicatedexcel.com/how-to-control-excel-pivot-tables-from-a-cell-value-with-vba/
I just repeated each line 3x, assigned new variables and it worked, but there should be a better way?
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, Range("F12:F13")) 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("Monthly").PivotTables("PivotTable2")
Set pt2 = Worksheets("Weekly").PivotTables("PivotTable4")
Set pt3 = Worksheets("Daily").PivotTables("PivotTable1")
Set Field1 = pt1.PivotFields("Core")
Set Field2 = pt2.PivotFields("Core")
Set Field3 = pt3.PivotFields("Core")
NewCat1 = Worksheets("Dashboard").Range("F12").Value
NewCat2 = Worksheets("Dashboard").Range("F12").Value
NewCat3 = Worksheets("Dashboard").Range("F12").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