【问题标题】:Pivot Table Manual Update Not Working数据透视表手动更新不起作用
【发布时间】:2015-02-10 01:56:32
【问题描述】:

我有一个数据透视表,我正在尝试根据数组中的值选择某些数据透视项目。我需要这个过程更快,所以我尝试使用 application.calculation = xlcalculationmanual 和 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

【问题讨论】:

    标签: vba excel pivot-table pivotitem


    【解决方案1】:

    pivottable.ManualUpdate [= 设置]
    True 导致 RefreshTable 从数据透视表中清除数据,而不是刷新它
    False 允许 RefreshTable 正常工作。
    默认为 False。
    此属性在调用过程结束后自动重置为 False(重要

    此属性应在您进行更新之前设置为 true(例如,更改枢轴项 Visible 属性)
    下面是一些用 C# 编写的代码作为示例:

        private void FilterByPivotItems(PivotField pf, List<string> pivotItemNames)
        {
            PivotItems pis = pf.ChildItems;
    
            if (pf.Orientation != 0)
            {
                int oldAutoSortOrder = 0;
    
                if (pf.AutoSortOrder != (int)Constants.xlManual)
                {
                    oldAutoSortOrder = pf.AutoSortOrder;
                    pf.AutoSort((int)Constants.xlManual, pf.Name);
                }
    
                int pivotItemsCount = pf.PivotItems().Count;
    
                for (int i = 1; i <= pivotItemsCount; i++)
                {
                    PivotItem pi = pf.PivotItems(i);
    
                    // check if current pivot item needs to be hidden (if it exists in pivotItemNames)
                    var match = pivotItemNames.FirstOrDefault(stringToCheck => stringToCheck.Equals(pi.Value));
    
                    if (match == null)
                    {
                        TryFilterPivotItems(pi, false, true);
                    }
                    else
                    {
                        TryFilterPivotItems(pi, true, true);
                    }
                }
    
                if (oldAutoSortOrder != 0)
                {
                    pf.AutoSort(oldAutoSortOrder, pf.Name);
                }
    
                PivotTable pt = pf.Parent as PivotTable;
                if (pt != null)
                {
                    // changing a pivot item triggers pivot table update
                    // so a refresh should be avoided cause it takes a lot and is unnecessary in this case
                    pt.Update();
                }
            }
        }
    
        private void TryFilterPivotItems(PivotItem currentPI, bool filterValue, bool deferLayoutUpdate = false)
        {
            try
            {
                PivotField pf = currentPI.Parent;
                PivotTable pt = pf.Parent as PivotTable;
    
                if (currentPI.Visible != filterValue)
                {
                    if (deferLayoutUpdate == true && pt != null)
                    {
                        // just keep these three lines stick together, no if, no nothing (otherwise ManualUpdate will reset back to false)
                        pt.ManualUpdate = true;
                        currentPI.Visible = filterValue;
    
                        // this may be redundant since setting Visible property of pivot item, resets ManualUpdate to false
                        pt.ManualUpdate = false;
                    }
                    else
                    {
                        currentPI.Visible = filterValue;
                    }
                }
            }
            catch (Exception ex)
            {
    
            }
        }
    
        private void TryFilterPivotItems(PivotField pf, string itemValue, bool filterValue, bool deferLayoutUpdate = false)
        {
            try
            {
                PivotItem currentPI = pf.PivotItems(itemValue);
                TryFilterPivotItems(currentPI, filterValue, deferLayoutUpdate);
            }
            catch (Exception ex)
            {
    
            }
        }
    

    作为结论,ManualUpdate 属性更改不会持续很长时间(在我的测试中,我可以看到它会尽快重置为 false,因此我建议您在需要时将其设置为 true对枢轴项目进行更改)

    有关 Excel 中的更新意味着什么的更多信息,您可以查看以下内容:
    Pivot Refresh vs. Update – is there a real difference?

    参考:
    标题:使用 VBA 和 .NET 编程 Excel
    作者:杰夫·韦伯、史蒂夫·桑德斯
    打印 ISBN:978-0-596-00766-9 | ISBN 10:0-596-00766-3
    电子书 ISBN:978-0-596-15951-1 | ISBN 10:0-596-15951-X

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多