在Event中调用子程序

时间:2015-11-24 11:10:55

标签: excel vba excel-vba

我在工作表1和VBA中有数据透视表我有以下代码,可以根据Update事件在同一工作表中对另一个数据透视表进行排序。

事件代码

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

应该在另一张表(Gold)中对值进行排序。不幸的是,模块似乎不会触发。如果我使用F5运行该模块,则表格正确排序,因此问题是启动模块......

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试此修改:

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