VBA清除所有切片器并刷新数据透视表Excel 2013

时间:2016-03-10 22:22:52

标签: excel vba excel-vba

我正在寻找VBA清除所有切片器并刷新Pivot表Excel 2013,但是,它打破了“ClearAllFilters”。我研究的所有东西都建议使用它。是否有更好的方法来刷新切片机并显示所有结果。此外,还希望包括数据模型和数据透视表的刷新。

Public Sub RefreshSlicersOnWorksheet(ws As Worksheet)

Dim sc As SlicerCache
Dim scs As SlicerCaches
Dim slice As Slicer

Set scs = ws.Parent.SlicerCaches

If Not scs Is Nothing Then
    For Each sc In scs
        For Each slice In sc.slicers
            If slice.Shape.Parent Is ws Then
                slice.ClearAllFilters
                Exit For 'unnecessary to check the other slicers of the slicer cache
            End If
        Next slice
    Next sc
End If

End Sub

我尝试过你的建议,但仍然遇到ClearAllFilters问题。

    Sub Reset()
Dim pt As PivotTable

 Application.ScreenUpdating = False
 ActiveWorkbook.Model.Refresh
    RefreshSlicersOnWorksheet ActiveSheet
    For Each pt In ActiveSheet.PivotTables
        pt.RefreshTable
    Next pt
Application.ScreenUpdating = True
End Sub
Public Sub RefreshSlicersOnWorksheet(ws As Worksheet)
Dim pt As PivotTable
Dim slice As Slicer

For Each pt In ws.PivotTables
    For Each slice In pt.Slicers
        slice.SlicerCache.ClearAllFilters
    Next slice
    pt.PivotCache.Refresh
Next pt
End Sub

1 个答案:

答案 0 :(得分:0)

ClearAllFilters方法适用于SlicerCache对象,而不适用于Slicer对象

变化:

slice.ClearAllFilters

为:

sc.ClearAllFilters

编辑:或者您可以从工作表中的数据透视表集合开始,然后从那里访问切片器。然后,您可以刷新数据透视表,如下所示:

Dim pt As PivotTable
Dim slice As Slicer

For Each pt In ws.PivotTables
    For Each slice In pt.Slicers
        slice.SlicerCache.ClearAllFilters
    Next slice
    pt.PivotCache.Refresh
Next pt