在Event中调用子例程

我有工作表1和VBA中的透视表我有以下代码基于Update事件在同一工作表中sorting另一个数据透视表。

事件代码

Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable) If Target = "PivotTable1" Then ActiveSheet.PivotTables("75Percentile").PivotFields( _ "[DimCustomer].[Customer Desc].[Customer Desc]").ClearAllFilters ActiveSheet.PivotTables("75Percentile").PivotFields( _ "[DimCustomer].[Customer Desc].[Customer Desc]").PivotFilters.Add2 Type:= _ xlValueIsGreaterThan, DataField:=ActiveSheet.PivotTables("75Percentile"). _ CubeFields("[Measures].[Sales Qty (Van Sales)]"), Value1:=Range("F5").Value Call Module1.SortGold End If End Sub 

在这段代码中,我尝试调用Module1.SortGold

模块代码

 Sub SortGold() ActiveWorkbook.Worksheets("Gold").AutoFilter.Sort.SortFields.Clear ActiveWorkbook.Worksheets("Gold").AutoFilter.Sort.SortFields.Add Key:=Range( _ "E2:E5001"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal With ActiveWorkbook.Worksheets("Gold").AutoFilter.Sort .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With End Sub 

这应该sorting在另一个表(金)的价值。 不幸的是,似乎该模块不会触发。 如果我运行F5的模块表正确sorting,所以问题是启动模块…

有什么想法吗?

尝试这种修改:

 Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable) Application.EnableEvents = False If Target = "PivotTable1" Then ActiveSheet.PivotTables("75Percentile").PivotFields( _ "[DimCustomer].[Customer Desc].[Customer Desc]").ClearAllFilters ActiveSheet.PivotTables("75Percentile").PivotFields( _ "[DimCustomer].[Customer Desc].[Customer Desc]").PivotFilters.Add2 Type:= _ xlValueIsGreaterThan, DataField:=ActiveSheet.PivotTables("75Percentile"). _ CubeFields("[Measures].[Sales Qty (Van Sales)]"), Value1:=Range("F5").Value Call Module1.SortGold End If Application.EnableEvents = True End Sub