将录制的宏应用于所有Excel表格

时间:2018-03-19 19:19:45

标签: excel vba

我有一张21张的工作簿。其中19张工作表具有带日期过滤器的数据透视表。我录制了一个宏,让我更快地更新过滤器。我想知道是否有一种方法可以让我在Visual Basic中编辑代码,这样我就可以在运行时将宏应用于带有数据透视表的19张表。

Sub WeekUpdate()
'
' WeekUpdate Macro
' Update the week for Wait Vs Price
'
' Keyboard Shortcut: Ctrl+Shift+J
'
    ActiveSheet.PivotTables("PivotTable6").PivotFields( _
        "[Date].[Fiscal Date Hierarchy].[Fiscal Year]").VisibleItemsList = Array("")
    ActiveSheet.PivotTables("PivotTable6").PivotFields( _
        "[Date].[Fiscal Date Hierarchy].[Fiscal Qtr]").VisibleItemsList = Array("")
    ActiveSheet.PivotTables("PivotTable6").PivotFields( _
        "[Date].[Fiscal Date Hierarchy].[Fiscal Period]").VisibleItemsList = Array("")
    ActiveSheet.PivotTables("PivotTable6").PivotFields( _
        "[Date].[Fiscal Date Hierarchy].[Fiscal Week]").VisibleItemsList = Array( _
        "[Date].[Fiscal Date Hierarchy].[Fiscal Week].&[2016012]")
    ActiveSheet.PivotTables("PivotTable6").PivotFields( _
        "[Date].[Fiscal Date Hierarchy].[Date]").VisibleItemsList = Array("")
    ActiveSheet.PivotTables("PivotTable5").PivotFields( _
        "[Date].[Fiscal Date Hierarchy].[Fiscal Year]").VisibleItemsList = Array("")
    ActiveSheet.PivotTables("PivotTable5").PivotFields( _
        "[Date].[Fiscal Date Hierarchy].[Fiscal Qtr]").VisibleItemsList = Array("")
    ActiveSheet.PivotTables("PivotTable5").PivotFields( _
        "[Date].[Fiscal Date Hierarchy].[Fiscal Period]").VisibleItemsList = Array("")
    ActiveSheet.PivotTables("PivotTable5").PivotFields( _
        "[Date].[Fiscal Date Hierarchy].[Fiscal Week]").VisibleItemsList = Array( _
        "[Date].[Fiscal Date Hierarchy].[Fiscal Week].&[2017012]")
    ActiveSheet.PivotTables("PivotTable5").PivotFields( _
        "[Date].[Fiscal Date Hierarchy].[Date]").VisibleItemsList = Array("")
End Sub

提前致谢!

1 个答案:

答案 0 :(得分:1)

使用For Each循环遍历Worksheets中的所有ActiveWorkBook

Sub WeekUpdate()
' WeekUpdate Macro
' Update the week for Wait Vs Price
'
' Keyboard Shortcut: Ctrl+Shift+J
'
Dim ws as Worksheet
For Each ws in ActiveWorkbook

ws.PivotTables("PivotTable6").PivotFields( _
    "[Date].[Fiscal Date Hierarchy].[Fiscal Year]").VisibleItemsList = Array("")
ws.PivotTables("PivotTable6").PivotFields( _
    "[Date].[Fiscal Date Hierarchy].[Fiscal Qtr]").VisibleItemsList = Array("")
ws.PivotTables("PivotTable6").PivotFields( _
    "[Date].[Fiscal Date Hierarchy].[Fiscal Period]").VisibleItemsList = Array("")
ws.PivotTables("PivotTable6").PivotFields( _
    "[Date].[Fiscal Date Hierarchy].[Fiscal Week]").VisibleItemsList = Array( _
    "[Date].[Fiscal Date Hierarchy].[Fiscal Week].&[2016012]")
ws.PivotTables("PivotTable6").PivotFields( _
    "[Date].[Fiscal Date Hierarchy].[Date]").VisibleItemsList = Array("")
ws.PivotTables("PivotTable5").PivotFields( _
    "[Date].[Fiscal Date Hierarchy].[Fiscal Year]").VisibleItemsList = Array("")
ws.PivotTables("PivotTable5").PivotFields( _
    "[Date].[Fiscal Date Hierarchy].[Fiscal Qtr]").VisibleItemsList = Array("")
ws.PivotTables("PivotTable5").PivotFields( _
    "[Date].[Fiscal Date Hierarchy].[Fiscal Period]").VisibleItemsList = Array("")
ws.PivotTables("PivotTable5").PivotFields( _
    "[Date].[Fiscal Date Hierarchy].[Fiscal Week]").VisibleItemsList = Array( _
    "[Date].[Fiscal Date Hierarchy].[Fiscal Week].&[2017012]")
ws.PivotTables("PivotTable5").PivotFields( _
    "[Date].[Fiscal Date Hierarchy].[Date]").VisibleItemsList = Array("")
Next
End Sub