加快透视表过滤VBA代码 [英] Speed up pivot table filtering VBA code

查看:128
本文介绍了加快透视表过滤VBA代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一个枢轴字段的数据透视表,包含很多项目。
我有VBA代码逻辑来决定是不是可见的。
问题是excel重新计算显示或隐藏的每个字段的数据透视表,这使得它非常慢。
在所有的值设置完成之后,我想要重新计算一次的东西。
我尝试使用Application.Calculation = xlCalculationManual但没有帮助。



我正在使用的vba代码是这样的

 对于i = 1 To oPivotField.PivotItems.Count 
If(oPivotField.PivotItems(i).Name =TestCondition)Then
oPivotField.PivotItems(i).Visible = True'重新计算枢轴表
Else
oPivotField.PivotItems(i).Visible = False'重新计算枢轴表
结束如果
下一个

我要手动执行此操作,取消选中显示所有框,然后重新检查我想要的字段可见。这会导致Excel重新计算一次,并仅显示我想要查看的枢轴项。
我想通过VBA代码做同样的事情。



我甚至尝试使用

  Application.ScreenUpdating = False 
Application.DisplayAlerts = False

但没有工作。

解决方案

哦!我刚刚解决了这个问题:



在代码开头,关闭自动更新,如下所示:

  PivotTable.ManualUpdate = True 

然后在最后的代码,重新开始:

  ActiveSheet.PivotTables(PivotTable1)。PivotCache.Refresh 

我发现这个线程搜索帮助编写代码,以确定数据透视表值是否应该可见。你的oPivotField背后是什么?这是我失踪的部分!


I have a pivot table with a pivot field and contain many items. I've VBA code logic to decide if the pivot value should be visible or not. The problem is excel recalculates pivot table for each field shown or hidden which makes it very slow. I would like something where it recalculates only once, after all the values are set. I tried using Application.Calculation = xlCalculationManual but it didnt help.

vba code that I am using is something like this

For i = 1 To oPivotField.PivotItems.Count
    If (oPivotField.PivotItems(i).Name = "TestCondition") Then
        oPivotField.PivotItems(i).Visible = True   'Recalulates pivot table
    Else
        oPivotField.PivotItems(i).Visible = False 'Recalulates pivot table
    End If
Next

I am to do this manually by uncheck the "show all" box and re-check for the fields I want visible. This cause Excel to recalculate once and show only the pivot items I want visible. I would like to do same thing via VBA code.

I even tried using

Application.ScreenUpdating = False
Application.DisplayAlerts = False

but didn't work.

解决方案

Oh! I just solved that one:

At the beginning of your code, turn off the auto-update like this:

PivotTable.ManualUpdate = True

And then at the end of the code, turn it back on:

ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh

I found this thread searching for help writing code that determines if a PivotTable value should be visible. What is behind your oPivotField? That's the part I'm missing!

这篇关于加快透视表过滤VBA代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆