运行宏

时间:2018-03-26 17:11:09

标签: excel vba excel-vba

我正在尝试使用宏来循环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

2 个答案:

答案 0 :(得分:2)

您的代码适合初学者,只需要进行一些调整:

  1. 无需使用Call关键字或激活ws
  2. 您需要将相关工作表作为参数传递给WeekUpdate。请注意,在您放弃Call之后,您不需要括号:
  3. For Each ws In ThisWorkbook.Worksheets
        WeekUpdate ws
        ws.Cells(1, 1) = 1
    Next
    
    1. 更改WeekUpdate子的签名以接受该工作表参数
    2. 将所有ActiveSheet更改为ws
    3. 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'