Change for each ws to specific ones

simico

New Member
Joined
May 27, 2011
Messages
30
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
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
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
You simply take out the loops and use some direct references:
Code:
...
Application.EnableEvents = False
Application.ScreenUpdating = False
    
    Set pt = Sheet1.PivotTables("{insert correct pivot table name here}")
    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
    
    Set pt = Sheet2.PivotTables("{insert correct pivot table name here}")
    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

exitHandler:
...
 
Upvote 0

Forum statistics

Threads
1,224,537
Messages
6,179,408
Members
452,912
Latest member
alicemil

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top