选择所有pivotitems并取消选择(空白)

时间:2016-02-29 21:03:00

标签: excel vba pivotitem

我有一个包含多个字段的数据透视表。其中一个领域是评论。刷新表时,注释字段仅设置为(空白)。我搜索了一种方法,使pivottable默认不选择空白而不是运气。

我编写了下面的代码来取消选中(空白)并选择所有其他的pivotitems。代码可以运行,但运行时间超过5分钟。

任何有关如何加速代码的建议或更好的取消选择(空白)和全部选择的方法都将非常感激。

`Dim ary As Variant, RVary As Variant
Dim StartTime As Double
Dim SecondsElapsed As Double
StartTime = Timer
ary = Array("CM PSL", "NM PSL", "CQ PSL", "NQ PSL", "CM Country", "NM Country", "CQ Country", "NQ Country", "QTR Summary", "TY PSL", "TY Country")
RVary = Array("CMRVCS", "NMRVCS", "CQRVCS", "NQRVCS")
Application.ScreenUpdating = False
On Error Resume Next
For Each i In ary
Sheets(i).Activate
    For Each j In RVary
        With ActiveSheet.PivotTables("RVCS")
            With .PivotFields(j)
                For k = 2 To .PivotItems.Count
                    .PivotItems(k).Visible = True
                Next k
                .PivotItems(1).Visible = False
            End With
        End With
    Next j
Next i

Erase array1
Erase RVCS

Application.ScreenUpdating = True
Sheets("CM PSL").Select
SecondsElapsed = Round(Timer - StartTime, 2)
MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation`

1 个答案:

答案 0 :(得分:0)

好的我有一个解决方案可以将运行时间减少到30秒。这是新的循环。

    For Each i In ary
    Sheets(i).Activate
        For Each j In RVary
            ActiveSheet.PivotTables("RVCS").PivotFields(j).ClearAllFilters
            With ActiveSheet.PivotTables("RVCS").PivotFields(j)
                .PivotItems("(blank)").Visible = False
            End With
        Next j
Next i