在vba中更改数据透视表而不触发事件处理程序

我想有两个数据透视表,其中我应用于其中一个页面字段中的filter也适用于另一个。 我正在用VBA做这个。

我将两个数据透视表的事件处理程序设置为

Private Sub Worksheet_PivotTableUpdate(ByVal target As PivotTable) Dim Other As PivotTable If target.Name = "Component Table" Then Set Other = ThisWorkbook.Worksheets("Categories in Comparison"). _ PivotTables("Operation Table") Else Set Other = ThisWorkbook.Worksheets("Categories in Comparison"). _ PivotTables("Component Table") End If Modul2.copyFilters target, Other End Sub 

其中调用sub copyFilters的定义如下:

 Sub copyFilters(source As PivotTable, destination As PivotTable) Dim field As PivotField For Each field In source.PageFields Dim item As PivotItem For Each item In field.PivotItems If item.Name <> "(blank)" Then If (destination.PageFields("Contract Type").PivotItems(item.Name). _ Visible <> item.Visible) Then destination.PageFields("Contract Type").PivotItems(item.Name). _ Visible = item.Visible End If End If Next item Next field End Sub 

当我现在手动改变多个项目的可见性时,事件处理程序只被调用一次,当我select要筛选的项目后按OK。

但是,当上面的子运行时,每当一个项目的可见性被改变,它将触发另一个数据透视表的事件处理程序。 这是不受欢迎的,因为它会改变我刚刚手动完成的一些更改。

我想到的一个解决scheme是,在我操作它的时候,将copyFilters中的事件处理程序从另一个数据透视表中分离出来,然后重新附加。 但是我怎么能这样做呢? 还是有其他更好的解决scheme来解决我的问题?