遍历所有工作表

时间:2016-01-08 18:08:57

标签: excel vba excel-vba

下面的代码,而不是循环遍历所有工作表(除例外)在执行时激活的工作表上反复执行。

我做错了什么?

Dim wsSheet As Worksheet

  For Each wsSheet In ThisWorkbook.Worksheets
    Select Case wsSheet.Name
        Case "Affiliates", "New Report", "Pasted Report", "New Month Or Client", "Set Up Data"
            'Do nothing.

        Case Else
            With wsSheet
  Range("B72").Select
  Range("B72:L86").Sort Key1:=Range("I72"), Order1:=xlDescending, Header:= _
  xlGuess, OrderCustom:=1, MatchCase:=False, _
  Orientation:=xlTopToBottom, _
  DataOption1:=xlSortNormal

  Range("B72:L72,B74:L74,B76:L76,B78:L78,B80:L80,B82:L82,B84:L84,B86:L86").Select
  Range("B86").Activate
  ExecuteExcel4Macro "PATTERNS(,0,1,TRUE,2,4,0,0)"
  Range("B73:L73,B75:L75,B77:L77,B79:L79,B81:L81,B83:L83,B85:L85").Select
  Range("B85").Activate
  ExecuteExcel4Macro "PATTERNS(,0,10,TRUE,2,4,0,0.799981688894314)"
  Range("C93").Select

            End With
    End Select

  Next wsSheet

End Sub

1 个答案:

答案 0 :(得分:1)

更正后,您的代码应如下所示:

Dim wsSheet As Worksheet

For Each wsSheet In ThisWorkbook.Worksheets
    Select Case wsSheet.Name
        Case "Affiliates", "New Report", "Pasted Report", "New Month Or Client", "Set Up Data"
            'Do nothing.
        Case Else
            wsSheet.Select
            With ActiveWorksheet
                .Range("B72").Select
                .Range("B72:L86").Sort Key1:=Range("I72"), Order1:=xlDescending, Header:= _
                    xlGuess, OrderCustom:=1, MatchCase:=False, _
                    Orientation:=xlTopToBottom, _
                    DataOption1:=xlSortNormal

                .Range("B72:L72,B74:L74,B76:L76,B78:L78,B80:L80,B82:L82,B84:L84,B86:L86").Select
                .Range("B86").Activate
                ExecuteExcel4Macro "PATTERNS(,0,1,TRUE,2,4,0,0)"
                .Range("B73:L73,B75:L75,B77:L77,B79:L79,B81:L81,B83:L83,B85:L85").Select
                .Range("B85").Activate
                ExecuteExcel4Macro "PATTERNS(,0,10,TRUE,2,4,0,0.799981688894314)"
                .Range("C93").Select
            End With
    End Select
Next wsSheet