Preventing a Recursive Call of "SheetPivotTableUpdate"

peglegpete

New Member
Joined
Dec 4, 2005
Messages
13
I have a pivot table hooked to an external data source.

I need to set the pivot's filter settings automatically for this pivot table whenever it is refreshed. See the attached image if you aren't sure what I mean by that.

So I recorded a macro while doing this, and got the .Visible property of the PivotItems object. This, in theory, works but the problem is when I use that code in the SheetPivotTableUpdate command, it becomes recursive (infinite loop) because changing this property fires the exact same event.

Code:
Private Sub Workbook_SheetPivotTableUpdate(ByVal Sh As Object, ByVal Target As PivotTable)
MsgBox ("starting custom pivot table code")
Application.ScreenUpdating = False

With Target.PivotFields("OneOfTheFields")
    .PivotItems("blahblah").Visible = False
    .PivotItems("yadayada").Visible = False
End With

Application.ScreenUpdating = True
End Sub

My event handling knowledge isn't so hot, but I feel like there is a way to prohibit it from firing again.

My bottomline question is does anyone know how to set the filter settings automatically in a pivottable after it has been refreshed?


Thank you for reading. I really appreciate any and all replies.

also posted on http://www.xtremevbtalk.com/showthread.php?p=1323740
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi

You should be able to use the Application.EnableEvents setting to control this:

Code:
Private Sub Workbook_SheetPivotTableUpdate(ByVal Sh As Object, ByVal Target As PivotTable)
MsgBox ("starting custom pivot table code")
Application.ScreenUpdating = False
Application.EnableEvents = False
With Target.PivotFields("OneOfTheFields")
.PivotItems("blahblah").Visible = False
.PivotItems("yadayada").Visible = False
End With
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,203,170
Messages
6,053,866
Members
444,690
Latest member
itgldmrt

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