0

如果我要在 Excel 中按下表单控制按钮,是否有一个快速宏可以自动为我在数据透视表中设置过滤器?我对 vba 和宏没有任何经验,我尝试录制一个,但是当其中一个枢轴未设置为与我录制宏时最初设置的过滤器相同时,它会出错。

这是它的代码:

    Sub Filter_Foot()
'
' Filter_Foot Macro
'

Application.ScreenUpdating = False

    ActiveSheet.PivotTables("PivotTable3").PivotFields("MD").CurrentPage = "(All)"
    With ActiveSheet.PivotTables("PivotTable3").PivotFields("MD")
        .PivotItems("Name 1").Visible = False
        .PivotItems("Name 2").Visible = False
        .PivotItems("Name 3").Visible = False
        .PivotItems("Name 4").Visible = False
        .PivotItems("Name 5").Visible = False
        .PivotItems("Name 6").Visible = False
        .PivotItems("Name 7").Visible = False
        .PivotItems("Name 8").Visible = False
        .PivotItems("Name 9").Visible = False
        .PivotItems("Name 10").Visible = False
        .PivotItems("Name 11").Visible = False
        .PivotItems("Name 12").Visible = False
        .PivotItems("Name 13").Visible = False
        .PivotItems("Name 14").Visible = False
        .PivotItems("Name 15").Visible = False
        .PivotItems("Name 16").Visible = False
        .PivotItems("Name 17").Visible = False
        .PivotItems("Name 18").Visible = False
        .PivotItems("Name 19").Visible = False
        .PivotItems("Name 20").Visible = False
        .PivotItems("Name 21").Visible = False
        .PivotItems("(blank)").Visible = False
    End With
    ActiveSheet.PivotTables("PivotTable2").PivotFields("MD").CurrentPage = "(All)"
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("MD")
        .PivotItems("Name 1").Visible = False
        .PivotItems("Name 2").Visible = False
        .PivotItems("Name 3").Visible = False
        .PivotItems("Name 4").Visible = False
        .PivotItems("Name 5").Visible = False
        .PivotItems("Name 6").Visible = False
        .PivotItems("Name 7").Visible = False
        .PivotItems("Name 8").Visible = False
        .PivotItems("Name 9").Visible = False
        .PivotItems("Name 10").Visible = False
        .PivotItems("Name 11").Visible = False
        .PivotItems("Name 12").Visible = False
        .PivotItems("Name 13").Visible = False
        .PivotItems("Name 14").Visible = False
        .PivotItems("Name 15").Visible = False
        .PivotItems("Name 16").Visible = False
        .PivotItems("Name 17").Visible = False
        .PivotItems("Name 18").Visible = False
        .PivotItems("Name 19").Visible = False
        .PivotItems("Name 20").Visible = False
        .PivotItems("Name 21").Visible = False
        .PivotItems("(blank)").Visible = False
    End With
    ActiveSheet.PivotTables("PivotTable4").PivotFields("MD").CurrentPage = "(All)"
    With ActiveSheet.PivotTables("PivotTable4").PivotFields("MD")
        .PivotItems("Name 1").Visible = False
        .PivotItems("Name 2").Visible = False
        .PivotItems("Name 3").Visible = False
        .PivotItems("Name 4").Visible = False
        .PivotItems("Name 5").Visible = False
        .PivotItems("Name 6").Visible = False
        .PivotItems("Name 7").Visible = False
        .PivotItems("Name 8").Visible = False
        .PivotItems("Name 9").Visible = False
        .PivotItems("Name 10").Visible = False
        .PivotItems("Name 11").Visible = False
        .PivotItems("Name 12").Visible = False
        .PivotItems("Name 13").Visible = False
        .PivotItems("Name 14").Visible = False
        .PivotItems("Name 15").Visible = False
        .PivotItems("Name 16").Visible = False
        .PivotItems("Name 17").Visible = False
        .PivotItems("Name 18").Visible = False
        .PivotItems("Name 19").Visible = False
        .PivotItems("Name 20").Visible = False
        .PivotItems("Name 21").Visible = False
        .PivotItems("(blank)").Visible = False
    End With
    ActiveSheet.PivotTables("PivotTable1").PivotFields("MD").CurrentPage = "(All)"
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("MD")
        .PivotItems("Name 1").Visible = False
        .PivotItems("Name 2").Visible = False
        .PivotItems("Name 3").Visible = False
        .PivotItems("Name 4").Visible = False
        .PivotItems("Name 5").Visible = False
        .PivotItems("Name 6").Visible = False
        .PivotItems("Name 7").Visible = False
        .PivotItems("Name 8").Visible = False
        .PivotItems("Name 9").Visible = False
        .PivotItems("Name 10").Visible = False
        .PivotItems("Name 11").Visible = False
        .PivotItems("Name 12").Visible = False
        .PivotItems("Name 13").Visible = False
        .PivotItems("Name 14").Visible = False
        .PivotItems("Name 15").Visible = False
        .PivotItems("Name 16").Visible = False
        .PivotItems("Name 17").Visible = False
        .PivotItems("Name 18").Visible = False
        .PivotItems("Name 19").Visible = False
        .PivotItems("Name 20").Visible = False
        .PivotItems("Name 21").Visible = False
        .PivotItems("(blank)").Visible = False
    End With
    ActiveSheet.PivotTables("PivotTable5").PivotFields("MD").CurrentPage = "(All)"
    With ActiveSheet.PivotTables("PivotTable5").PivotFields("MD")
        .PivotItems("Name 1").Visible = False
        .PivotItems("Name 2").Visible = False
        .PivotItems("Name 3").Visible = False
        .PivotItems("Name 4").Visible = False
        .PivotItems("Name 5").Visible = False
        .PivotItems("Name 6").Visible = False
        .PivotItems("Name 7").Visible = False
        .PivotItems("Name 8").Visible = False
        .PivotItems("Name 9").Visible = False
        .PivotItems("Name 10").Visible = False
        .PivotItems("Name 11").Visible = False
        .PivotItems("Name 12").Visible = False
        .PivotItems("Name 13").Visible = False
        .PivotItems("Name 14").Visible = False
        .PivotItems("Name 15").Visible = False
        .PivotItems("Name 16").Visible = False
        .PivotItems("Name 17").Visible = False
        .PivotItems("Name 18").Visible = False
        .PivotItems("Name 19").Visible = False
        .PivotItems("Name 20").Visible = False
        .PivotItems("Name 21").Visible = False
        .PivotItems("(blank)").Visible = False
    End With
    ActiveSheet.PivotTables("PivotTable6").PivotFields("MD").CurrentPage = "(All)"
    With ActiveSheet.PivotTables("PivotTable6").PivotFields("MD")
        .PivotItems("Name 1").Visible = False
        .PivotItems("Name 2").Visible = False
        .PivotItems("Name 3").Visible = False
        .PivotItems("Name 4").Visible = False
        .PivotItems("Name 5").Visible = False
        .PivotItems("Name 6").Visible = False
        .PivotItems("Name 7").Visible = False
        .PivotItems("Name 8").Visible = False
        .PivotItems("Name 9").Visible = False
        .PivotItems("Name 10").Visible = False
        .PivotItems("Name 11").Visible = False
        .PivotItems("Name 12").Visible = False
        .PivotItems("Name 13").Visible = False
        .PivotItems("Name 14").Visible = False
        .PivotItems("Name 15").Visible = False
        .PivotItems("Name 16").Visible = False
        .PivotItems("Name 17").Visible = False
        .PivotItems("Name 18").Visible = False
        .PivotItems("Name 19").Visible = False
        .PivotItems("Name 20").Visible = False
        .PivotItems("Name 21").Visible = False
        .PivotItems("(blank)").Visible = False
    End With
    ActiveSheet.PivotTables("PivotTable7").PivotFields("MD").CurrentPage = "(All)"
    With ActiveSheet.PivotTables("PivotTable7").PivotFields("MD")
        .PivotItems("Name 1").Visible = False
        .PivotItems("Name 2").Visible = False
        .PivotItems("Name 3").Visible = False
        .PivotItems("Name 4").Visible = False
        .PivotItems("Name 5").Visible = False
        .PivotItems("Name 6").Visible = False
        .PivotItems("Name 7").Visible = False
        .PivotItems("Name 8").Visible = False
        .PivotItems("Name 9").Visible = False
        .PivotItems("Name 10").Visible = False
        .PivotItems("Name 11").Visible = False
        .PivotItems("Name 12").Visible = False
        .PivotItems("Name 13").Visible = False
        .PivotItems("Name 14").Visible = False
        .PivotItems("Name 15").Visible = False
        .PivotItems("Name 16").Visible = False
        .PivotItems("Name 17").Visible = False
        .PivotItems("Name 18").Visible = False
        .PivotItems("Name 19").Visible = False
        .PivotItems("Name 20").Visible = False
        .PivotItems("Name 21").Visible = False
        .PivotItems("(blank)").Visible = False
    End With
    ActiveSheet.PivotTables("PivotTable8").PivotFields("MD").CurrentPage = "(All)"
    With ActiveSheet.PivotTables("PivotTable8").PivotFields("MD")
        .PivotItems("Name 1").Visible = False
        .PivotItems("Name 2").Visible = False
        .PivotItems("Name 3").Visible = False
        .PivotItems("Name 4").Visible = False
        .PivotItems("Name 5").Visible = False
        .PivotItems("Name 6").Visible = False
        .PivotItems("Name 7").Visible = False
        .PivotItems("Name 8").Visible = False
        .PivotItems("Name 9").Visible = False
        .PivotItems("Name 10").Visible = False
        .PivotItems("Name 11").Visible = False
        .PivotItems("Name 12").Visible = False
        .PivotItems("Name 13").Visible = False
        .PivotItems("Name 14").Visible = False
        .PivotItems("Name 15").Visible = False
        .PivotItems("Name 16").Visible = False
        .PivotItems("Name 17").Visible = False
        .PivotItems("Name 18").Visible = False
        .PivotItems("Name 19").Visible = False
        .PivotItems("Name 20").Visible = False
        .PivotItems("Name 21").Visible = False
        .PivotItems("(blank)").Visible = False
    End With
    ActiveSheet.PivotTables("PivotTable9").PivotFields("MD").CurrentPage = "(All)"
    With ActiveSheet.PivotTables("PivotTable9").PivotFields("MD")
        .PivotItems("Name 1").Visible = False
        .PivotItems("Name 2").Visible = False
        .PivotItems("Name 3").Visible = False
        .PivotItems("Name 4").Visible = False
        .PivotItems("Name 5").Visible = False
        .PivotItems("Name 6").Visible = False
        .PivotItems("Name 7").Visible = False
        .PivotItems("Name 8").Visible = False
        .PivotItems("Name 9").Visible = False
        .PivotItems("Name 10").Visible = False
        .PivotItems("Name 11").Visible = False
        .PivotItems("Name 12").Visible = False
        .PivotItems("Name 13").Visible = False
        .PivotItems("Name 14").Visible = False
        .PivotItems("Name 15").Visible = False
        .PivotItems("Name 16").Visible = False
        .PivotItems("Name 17").Visible = False
        .PivotItems("Name 18").Visible = False
        .PivotItems("Name 19").Visible = False
        .PivotItems("Name 20").Visible = False
        .PivotItems("Name 21").Visible = False
        .PivotItems("(blank)").Visible = False
    End With
End Sub
4

1 回答 1

1

这是很多代码。重构可能更容易发现问题:

Sub Filter_Foot()

    Dim pt As PivotTables
    Set pt = ActiveSheet.PivotTables

    Application.ScreenUpdating = False

    SetPF pt("PivotTable3").PivotFields("MD")
    SetPF pt("PivotTable2").PivotFields("MD")
    SetPF pt("PivotTable4").PivotFields("MD")
    SetPF pt("PivotTable1").PivotFields("MD")
    SetPF pt("PivotTable5").PivotFields("MD")
    SetPF pt("PivotTable6").PivotFields("MD")
    SetPF pt("PivotTable7").PivotFields("MD")
    SetPF pt("PivotTable8").PivotFields("MD")
    SetPF pt("PivotTable9").PivotFields("MD")

    Application.ScreenUpdating = True

End Sub

Sub SetPF(pf As PivotField)
    Dim i As Integer
    With pf
        .CurrentPage = "(All)"
        For i = 1 To 21
            .PivotItems("Name " & i).Visible = False
        Next i
        .PivotItems("(blank)").Visible = False
    End With
End Sub
于 2012-08-03T23:10:53.717 回答