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

Excel 在vba中更改透视表而不触发事件处理程序,excel,vba,Excel,Vba,我希望有两个透视表,其中我应用于其中一个的页面字段中的过滤器也应用于另一个。我正在用VBA做这件事 我将两个透视表的事件处理程序设置为 Private Sub Worksheet_PivotTableUpdate(ByVal target As PivotTable) Dim Other As PivotTable If target.Name = "Component Table" Then Set Other = ThisWorkbook.Worksheets

我希望有两个透视表,其中我应用于其中一个的页面字段中的过滤器也应用于另一个。我正在用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
调用子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
子副本筛选器(源作为数据透视表,目标作为数据透视表)
作为数据透视字段的Dim字段
对于source.PageFields中的每个字段
将项目设置为数据透视项
对于字段.PivotItems中的每个项目
如果item.Name“(空白)”则
If(destination.PageFields(“合同类型”).PivotItems(item.Name)_
可见项。可见)然后
destination.PageFields(“合同类型”).PivotItems(item.Name)_
可见=项目。可见
如果结束
如果结束
下一项
下一场
端接头
当我现在手动改变多个项目的可见性时,当我在选择要筛选的项目后按OK时,事件处理程序只被调用一次

但是,当运行上述子项时,每次更改一个项目的可见性时,它都会触发另一个透视表的事件处理程序。这是不需要的,因为它将更改回我刚才手动进行的一些更改


我想到的一个解决方案是,在对另一个透视表进行操作时,将copyFilters中的事件处理程序从另一个透视表中分离出来,然后再次附加它。但是我怎么能做到呢?还是有其他/更好的解决方案来解决我的问题?

在事件处理程序
工作表\u PivotTableUpdate
中添加
Application.EnableEvents=False
,最后添加
Application.EnableEvents=True