Hey fair folk,
I found this code which runs through pivot tables and updates fields based on content of other cells. I have found the code all over the interweb, but not any good answers as to how to make the change I'm after. All I want to do is change it from looking at each worksheet and pivot table and instead only update two specific ones.
eg Sheet1.PivotTable1 and Sheet2.PivotTable1
Thanks
I found this code which runs through pivot tables and updates fields based on content of other cells. I have found the code all over the interweb, but not any good answers as to how to make the change I'm after. All I want to do is change it from looking at each worksheet and pivot table and instead only update two specific ones.
eg Sheet1.PivotTable1 and Sheet2.PivotTable1
Thanks
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet
Dim pt As PivotTable
Dim pi As PivotItem
Dim strField As String
On Error Resume Next
Select Case Target.Address
Case Range("M3").Address
strField = "Facility"
Case Range("O3").Address
strField = "Month"
Case Range("O2").Address
strField = "Year"
Case Else
GoTo exitHandler
End Select
Application.EnableEvents = False
Application.ScreenUpdating = False
For Each ws In ThisWorkbook.Worksheets
For Each pt In ws.PivotTables
With pt.PageFields(strField)
For Each pi In .PivotItems
If pi.Value = Target.Value Then
.CurrentPage = Target.Value
Exit For
Else
.CurrentPage = "(All)"
End If
Next pi
End With
Next pt
Next ws
exitHandler:
Application.EnableEvents = True
Application.ScreenUpdating = True
Exit Sub
End Sub