我有一张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
提前致谢!
答案 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