excel - 默认情况下选择刷新时添加的 VBA PivotItems

标签 excel pivotitem vba

当我刷新数据,然后使用以下代码刷新数据透视表时,默认情况下会选择新添加的项目。有什么方法可以防止这种情况发生,因为这意味着我必须再次进入并取消选择?

    Dim pt As PivotTable
    Dim pf As PivotField
    For Each pt In Worksheets("Summary").PivotTables

    pt.PivotCache.MissingItemsLimit = xlMissingItemsNone
    pt.PivotCache.Refresh

    For Each pf In pt.PivotFields
       pf.AutoSort xlAscending, pf.Name
    Next pf
    Next pt

    Set pf = Nothing
    Set pt = Nothing

最佳答案

根据我上面的评论,这样的事情应该有效。如果您有很多需要检查的数据透视字段,您可能可以创建一些函数来使其更容易或更高效。

Sub pt()


Dim pt As PivotTable, pf As PivotField, pi As PivotItem
Dim dict As Dictionary 'requires reference to Microsoft Scripting Runtime

For Each pt In Worksheets("Summary").PivotTables

    pt.PivotCache.MissingItemsLimit = xlMissingItemsNone

    Set dict = New Dictionary

    ' you will need to loop through each Fields PivotItem Collection separately 
    For Each pi In pt.PivotFields("myPivotField").PivotItems
        dict.Add pi.Name, 1
    Next

    pt.PivotCache.Refresh

   'again, you will need to loop through each one separately to check
    For Each pi In pt.PivotFields("myPivotField").PivotItems
        If dict.Exists(pi.Name) Then Else: pi.Visible = False
    Next

    For Each pf In pt.PivotFields
       pf.AutoSort xlAscending, pf.Name
    Next pf

Next pt

Set pf = Nothing
Set pt = Nothing

End Sub

关于excel - 默认情况下选择刷新时添加的 VBA PivotItems,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11473117/

相关文章:

arrays - 具有三个条件的索引/匹配

windows-phone-8 - window 电话 8 : remove pivot header

windows-phone-7 - 具有超过 100 个 pivotItems 的 pivot

c# - 如何隐藏单个 PivotItem 的可见性

VBA构造函数不起作用

vba - 拆分( "TEXT", "+"或 "-",2)

vba - 使用 VBA 更改 Excel 图表的方向(纵向或横向)

excel - 如何在不打开 Visual Basic 编辑器的情况下执行 VBA 代码

excel - 比较不同/单独表列中的值时出现 'Type mismatch' 运行时错误

excel - VBA 引用计数 - 对象销毁