1

我有一个数据透视表,我正在尝试根据数组中的值选择某些数据透视项。我需要这个过程更快,所以我尝试使用Application.Calculation = xlCalculationManualand PivotTables.ManualUpdate = True,但似乎都没有工作;每次更改数据透视表时,数据透视表仍会重新计算。

有什么我可以做的不同的事情来防止 Excel 每次都重新计算吗?或者有没有办法一次(而不是单独)取消选择所有项目以使过程更快?

这是我的代码:

Application.Calculation = xlCalculationManual

'code to fill array with list of companies goes here    

Dim PT As Excel.PivotTable
Set PT = Sheets("LE Pivot Table").PivotTables("PivotTable1")

Sheets("LE Pivot Table").PivotTables("PivotTable1").ManualUpdate = True
Dim pivItem As PivotItem

'compare pivot items to array.  
'If pivot item matches an element of the array, make it visible=true, 
'otherwise, make it visible=false
For Each pivItem In PT.PivotFields("company").PivotItems
    pivItem.Visible = False 'initially make item unchecked
    For Each company In ArrayOfCompanies()
        If pivItem.Value = company Then
            pivItem.Visible = True
        End If
    Next company
Next pivItem
4

2 回答 2

0
于 2015-05-13T07:07:39.300 回答
0

您似乎真的想尝试一些不同的东西来显着减少在数据透视表中选择所需项目所需的时间。我建议使用“MirrorField”,即“公司”的副本,用于在数据透视表的源数据中设置您需要隐藏\显示的项目。

首先,您需要手动(或以编程方式)添加“MirrorField”并命名为与源字段相同,开头带有特殊字符,例如“!Company”,该项目必须是源数据的一部分,并且可以放置在任何列中它(因为这将是一个“程序员”字段,所以我会将它放在最后一列中,并且可能隐藏起来,以免给用户造成任何问题)

请在下面找到更新数据透视表数据源和刷新数据透视表的代码

我还要求更新 PivotField 使其灵活,因为它可以用于任何字段(前提是“FieldMirror”已经创建) 最后:如果您在可透视工作表中运行任何事件,它们应该是禁用和启用仅与最后一个数据透视表更新一起运行

希望这是您正在寻找的。

Sub Ptb_ShowPivotItems_MirrorField(vPtbFld As Variant, aPtbItmSelection As Variant)
Dim oPtb As PivotTable
Dim rPtbSrc As Range
Dim iCol(2) As Integer
Dim sRC(2) As String
Dim sFmlR1C1 As String
Dim sPtbSrcDta As String

    Rem Set PivotTable & SourceData
    Set oPtb = ActiveSheet.PivotTables(1)
    sPtbSrcDta = Chr(34) & oPtb.SourceData & Chr(34)
    Set rPtbSrc = Evaluate("=INDIRECT(" & sPtbSrcDta & ",0)")

    Rem Get FieldMirrow Position in Pivottable SourceData (FieldMirrow Already present SourceData)
    With rPtbSrc
        iCol(1) = -1 + .Column + Application.Match(vPtbFld, .Rows(1), 0)
        iCol(2) = Application.Match("!" & vPtbFld, .Rows(1), 0)
    End With

    Rem Set FieldMirror Items PivotTable SourceData as per User Selection
    sRC(1) = """|""&RC" & iCol(1) & "&""|"""
    sRC(2) = """|" & Join(aPtbItmSelection, "|") & "|"""
    sFmlR1C1 = "=IF(ISERROR(SEARCH(" & sRC(1) & "," & sRC(2) & ")),""N/A"",""Show"")"
    With rPtbSrc.Offset(1).Resize(-1 + rPtbSrc.Rows.Count).Columns(iCol(2))
        .Value = "N/A"
        .FormulaR1C1 = sFmlR1C1
        .Value = .Value2
    End With

    Rem Refresh PivotTable & Select FieldMirror Items
    With oPtb

        Rem Optional: Disable Events - In case you are running any events in the pivottable worksheet
        Application.EnableEvents = False

        .ClearAllFilters
        .PivotCache.Refresh
        With .PivotFields("!" & vPtbFld)
            .Orientation = xlPageField
            .EnableMultiplePageItems = False

            Rem Optional: Enable Events - To triggrer the pivottable worksheet events only with last update
            Application.EnableEvents = True
            .CurrentPage = "Show"

    End With: End With

End Sub
于 2015-05-13T17:01:27.353 回答