跳过特定名称 sheet

Skip a specific name sheet

我的代码通过从工作簿上可用的多个 sheet 复制特定范围的数据来运行。但我想跳过一个名为“数据回顾”的 sheet,以便代码仅针对其他 sheet 运行

我应该在我的代码中添加什么?

Sub Copy_Data()
    Dim ws As Worksheet, MasterSheet As Worksheet
    Dim originalDestinationCell As Range, nextDestCell As Range
    Dim firstGreyCell As Range, c As Range, e As Range, s As Range
    Dim lastRow As Long, firstRow As Long, colToCheckLast As Long, i As Long
    Dim isMain As Boolean
    
    Set MasterSheet = Sheets("Form Rekap")            'where you want to put the copied data
    Set originalDestinationCell = MasterSheet.Range("C6") 'the first cell the data will be copied to
    Set nextDestCell = originalDestinationCell.Offset(-1, 0)
    
    firstRow = 6
    colToCheckLast = 7
    
    For Each ws In ThisWorkbook.Worksheets
        If Not ws.Name = MasterSheet.Name Then
            Set firstGreyCell = ws.Range("C" & firstRow) 'Set first starting loop cell
            lastRow = ws.Cells(ws.Rows.Count, colToCheckLast).End(xlUp).Row
            isMain = True
            For i = firstRow To lastRow
                Set c = ws.Range("C" & i)
                Set e = ws.Range("E" & i)
                Set s = Nothing
                If isMain Then
                    If c.Interior.Color = firstGreyCell.Interior.Color Then
                        If Not IsEmpty(c) Then
                            Set s = c
                        Else
                            isMain = False
                        End If
                    End If
                Else
                    If c.Interior.Color = firstGreyCell.Interior.Color Then
                        If Not IsEmpty(c) Then
                            Set s = c
                        End If
                        isMain = True
                    Else
                        If Not IsEmpty(e) Then
                            Set s = e
                        End If
                    End If
                End If
                
                If Not s Is Nothing Then
                    Set nextDestCell = MasterSheet.Cells(nextDestCell.Row + 1, originalDestinationCell.Column)
                    nextDestCell.Interior.Color = s.Interior.Color
                    nextDestCell.Value = s.Value
                End If
            Next
        End If
    Next ws
End Sub

做你想做的几种方法:

Sub SkipSpecificWorksheet()
    Dim ws As Worksheet
    
    'Your version
    For Each ws In ThisWorkbook.Worksheets
        If Not ws.Name = MasterSheet.Name And Not ws.Name = "Data Recap" Then 'Add another condition
            'Do stuffs to the worksheet
        End If
    Next ws
    
    'Alternative
    'Same logic as above, just different syntax
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> MasterSheet.Name And ws.Name <> "Data Recap" Then
            'Do stuffs to the worksheet
        End If
    Next ws
    
    'Another alternative using Select Statement
    For Each ws In ThisWorkbook.Worksheets
        Select Case ws.Name
            Case MasterSheet.Name, "Data Recap" 'List of worksheet to skip
                
            Case Else
                'Do stuffs to the worksheet
        End Select
    Next ws
End Sub

处理有例外的工作表

Option Explicit

Sub ProcessWorksheets()

    Const ExceptionsList As String = "Form Recap,Data Recap"
    
    Dim Exceptions() As String: Exceptions = Split(ExceptionsList, ",")

    Dim ws As Worksheet
    
    For Each ws In ThisWorkbook.Worksheets
        If IsError(Application.Match(ws.Name, Exceptions, 0)) Then
            ' e.g.:
            Debug.Print ws.Name
        
        'Else ' is in the list; do nothing
        End If
    Next ws
    
End Sub