我正在尝试使用宏来循环Excel文档中的工作表。我之前从未使用过VisualBasic,所以很自然地,我已经碰壁了。
Sub DoSheets()
Dim ws As Worksheet
Dim starting_ws As Worksheet
Set starting_ws = ActiveSheet
For Each ws In ThisWorkbook.Worksheets
ws.Activate
Call WeekUpdate
ws.Cells(1, 1) = 1
Next
starting_ws.Activate
End Sub
这是我打电话的代码。当我每次运行一个工作表时,它可以自行工作,但这需要很长时间。因此,为什么我试图让上面的循环工作。
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].&[2016015]")
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].&[2017015]")
ActiveSheet.PivotTables("PivotTable5").PivotFields( _
"[Date].[Fiscal Date Hierarchy].[Date]").VisibleItemsList = Array("")
End Sub
答案 0 :(得分:2)
您的代码适合初学者,只需要进行一些调整:
Call
关键字或激活ws
。WeekUpdate
。请注意,在您放弃Call
之后,您不需要括号:For Each ws In ThisWorkbook.Worksheets
WeekUpdate ws
ws.Cells(1, 1) = 1
Next
WeekUpdate
子的签名以接受该工作表参数ActiveSheet
更改为ws
:Sub WeekUpdate(ByVal ws As Worksheet)
'
' WeekUpdate Macro
' Update the week for Wait Vs Price
'
' Keyboard Shortcut: Ctrl+Shift+J
'
ws.PivotTables("PivotTable6").PivotFields( _
"[Date].[Fiscal Date Hierarchy].[Fiscal Year]").VisibleItemsList = Array("")
ws.PivotTables("PivotTable6").PivotFields( _
"[Date].[Fiscal Date Hierarchy].[Fiscal Qtr]").VisibleItemsList = Array("")
'... etc
更新
更好。正如评论中所建议的那样,WeekUpdate
sub可以单独使用,例如使用键盘快捷键,使用以下内容:
Sub WeekUpdate(Optional ByVal ws As Worksheet)
'
' WeekUpdate Macro
' Update the week for Wait Vs Price
'
' Keyboard Shortcut: Ctrl+Shift+J
'
If ws Is Nothing Then Set ws = ActiveSheet
ws.PivotTables("PivotTable6").PivotFields( _
"[Date].[Fiscal Date Hierarchy].[Fiscal Year]").VisibleItemsList = Array("")
ws.PivotTables("PivotTable6").PivotFields( _
"[Date].[Fiscal Date Hierarchy].[Fiscal Qtr]").VisibleItemsList = Array("")
'... etc
答案 1 :(得分:0)
尝试将'ThisWorkbook.Worksheets'替换为'ActiveWorkbook.worksheets',并尝试添加'Next WS'而不仅仅是'next'