Refresh PivotTables in ActiveSheet by Selection

dellzy

Board Regular
Joined
Apr 24, 2013
Messages
146
Hi Excel Experts,

Can anyone help me to edit this so that it will only refresh on ActiveSheet instead of entire workbook, please?

Code:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)


Dim wsMain As Worksheet
Dim ws As Worksheet
Dim ptMain As PivotTable
Dim pt As PivotTable
Dim pfMain As PivotField
Dim pf As PivotField
Dim pi As PivotItem
Dim bMI As Boolean


On Error Resume Next
Set wsMain = ActiveSheet
Set ptMain = Target


Application.EnableEvents = False
Application.ScreenUpdating = False


For Each pfMain In ptMain.PageFields
    bMI = pfMain.EnableMultiplePageItems
    For Each ws In ThisWorkbook.Worksheets
        For Each pt In ws.PivotTables
            If ws.Name & "_" & pt <> wsMain.Name & "_" & ptMain Then
                pt.ManualUpdate = True
                Set pf = pt.PivotFields(pfMain.Name)
                        bMI = pfMain.EnableMultiplePageItems
                        With pf
                            .ClearAllFilters
                            Select Case bMI
                                Case False
                                    .CurrentPage = pfMain.CurrentPage.Value
                                Case True
                                    .CurrentPage = "(All)"
                                    For Each pi In pfMain.PivotItems
                                        .PivotItems(pi.Name).Visible = pi.Visible
                                    Next pi
                                    .EnableMultiplePageItems = bMI
                            End Select
                        End With
                        bMI = False


                Set pf = Nothing
                pt.ManualUpdate = False
            End If
        Next pt
    Next ws
Next pfMain


Application.EnableEvents = True
Application.ScreenUpdating = True


End Sub

Thank you in advance.
DZ
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I haven't tested it, but try...

Code:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)

Dim ptMain As PivotTable
Dim pt As PivotTable
Dim pfMain As PivotField
Dim pf As PivotField
Dim pi As PivotItem
Dim bMI As Boolean

On Error Resume Next
Set ptMain = Target

Application.EnableEvents = False
Application.ScreenUpdating = False

For Each pfMain In ptMain.PageFields
    bMI = pfMain.EnableMultiplePageItems
    For Each pt In Me.PivotTables
        If pt.Name <> ptMain.Name Then
            pt.ManualUpdate = True
            Set pf = pt.PivotFields(pfMain.Name)
            bMI = pfMain.EnableMultiplePageItems
            With pf
                .ClearAllFilters
                Select Case bMI
                    Case False
                        .CurrentPage = pfMain.CurrentPage.Value
                    Case True
                        .CurrentPage = "(All)"
                        For Each pi In pfMain.PivotItems
                            .PivotItems(pi.Name).Visible = pi.Visible
                        Next pi
                        .EnableMultiplePageItems = bMI
                End Select
            End With
            bMI = False
            Set pf = Nothing
            pt.ManualUpdate = False
        End If
    Next pt
Next pfMain

Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub

Hope this helps!
 
Upvote 0
I haven't tested it, but try...

Code:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)

Dim ptMain As PivotTable
Dim pt As PivotTable
Dim pfMain As PivotField
Dim pf As PivotField
Dim pi As PivotItem
Dim bMI As Boolean

On Error Resume Next
Set ptMain = Target

Application.EnableEvents = False
Application.ScreenUpdating = False

For Each pfMain In ptMain.PageFields
    bMI = pfMain.EnableMultiplePageItems
    For Each pt In Me.PivotTables
        If pt.Name <> ptMain.Name Then
            pt.ManualUpdate = True
            Set pf = pt.PivotFields(pfMain.Name)
            bMI = pfMain.EnableMultiplePageItems
            With pf
                .ClearAllFilters
                Select Case bMI
                    Case False
                        .CurrentPage = pfMain.CurrentPage.Value
                    Case True
                        .CurrentPage = "(All)"
                        For Each pi In pfMain.PivotItems
                            .PivotItems(pi.Name).Visible = pi.Visible
                        Next pi
                        .EnableMultiplePageItems = bMI
                End Select
            End With
            bMI = False
            Set pf = Nothing
            pt.ManualUpdate = False
        End If
    Next pt
Next pfMain

Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub

Hope this helps!

Hi Domenic, Awesome! Your my saviour! It works perfectly! Thank you a zillion! :)
 
Upvote 0
You're very welcome!

And thanks for the feedback!

Cheers!
 
Upvote 0
Hi Excel Experts!

Recently Domenic has helped me the solution for any selection on any filter options. Now I need help as I have a requirement to allow refresh all pivots on same worksheet based on changes on specific filter only. eg. Month

Only when user change the month that all pivots will follow, otherwise other changes on other filters should not impact other pivots.

Here I paste back the code edited by Domenic earlier :-

<style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Courier; -webkit-text-stroke: #000000}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Courier; -webkit-text-stroke: #000000; min-height: 14.0px}span.s1 {font-kerning: none}</style>
Code:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)


Dim ptMain As PivotTable
Dim pt As PivotTable
Dim pfMain As PivotField
Dim pf As PivotField
Dim pi As PivotItem
Dim bMI As Boolean


On Error Resume Next
Set ptMain = Target


Application.EnableEvents = False
Application.ScreenUpdating = False


For Each pfMain In ptMain.PageFields
    bMI = pfMain.EnableMultiplePageItems
    For Each pt In Me.PivotTables
        If pt.Name <> ptMain.Name Then
            pt.ManualUpdate = True
            Set pf = pt.PivotFields(pfMain.Name)
            bMI = pfMain.EnableMultiplePageItems
            With pf
                .ClearAllFilters
                Select Case bMI
                    Case False
                        .CurrentPage = pfMain.CurrentPage.Value
                    Case True
                        .CurrentPage = "(All)"
                        For Each pi In pfMain.PivotItems
                            .PivotItems(pi.Name).Visible = pi.Visible
                        Next pi
                        .EnableMultiplePageItems = bMI
                End Select
            End With
            bMI = False
            Set pf = Nothing
            pt.ManualUpdate = False
        End If
    Next pt
Next pfMain


Application.EnableEvents = True
Application.ScreenUpdating = True


End Sub

Thanks in advance.
DZ
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,638
Latest member
Oluwabukunmi

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