从工作簿

时间:2018-02-12 02:38:51

标签: excel-vba vba excel

我试图清除工作簿中多个数据透视表上的所有过滤器(在多个工作表上)。我尝试过以下方法,但无法正常工作。

我对VBA FYI来说相对较新。

Sub ClearFiltersAttempt1()
   For Each xWs In Application.ActiveWorkbook.Worksheets
   For Each xTable In xWs.PivotTables
       xPivotField.ClearAllFilters
   End Sub

1 个答案:

答案 0 :(得分:0)

2个For Each语句缺少结束语句Next

Option Explicit

Public Sub ClearFiltersAttempt2()

    Dim ws As Worksheet, pt As PivotTable

    For Each ws In ThisWorkbook.Worksheets
        For Each pt In ws.PivotTables
            pt.ClearAllFilters
        Next
    Next
End Sub

Option Explicit和正确的代码缩进将帮助您确定语法错误和缺少变量声明