如果我要在 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