+ Reply to Thread
Results 1 to 5 of 5

Skipping over certain sheets in a For Each loop

Hybrid View

  1. #1
    Registered User
    Join Date
    06-01-2013
    Location
    United States
    MS-Off Ver
    Excel 2011
    Posts
    64

    Skipping over certain sheets in a For Each loop

    Previously I had been shown a trick to delete all unprotected cells in my workbook.
    The problem is, I want to to skip over a certain sheet, can I skip over a certain sheet with For Each loops?



    
    Sub emptyUnlocked()
        
         Dim ws As Worksheet, bVBProtected As Boolean
         
         For Each ws In Worksheets
         
            If Not ws.ProtectContents Then
                ws.Protect
                bVBProtected = True
            End If
            
            'mikerickson's trick
            On Error Resume Next
            ws.UsedRange = ""
            On Error GoTo 0
            
            If bVBProtected Then
                ws.Unprotect
                bVBProtected = False
            End If
            
        Next ws
        
    End Sub

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Skipping over certain sheets in a For Each loop

    Maybe:

    Sub Nicholast()
        
         Dim ws As Worksheet, bVBProtected As Boolean
         
         For Each ws In Worksheets
            If ws.Name <> "THE NAME OF THE SHEET YOU WANT TO SKIP" Then
         
            If Not ws.ProtectContents Then
                ws.Protect
                bVBProtected = True
            End If
            
            'mikerickson's trick
            On Error Resume Next
            ws.UsedRange = ""
            On Error GoTo 0
            
            If bVBProtected Then
                ws.Unprotect
                bVBProtected = False
            End If
            End If
        Next ws
        
    End Sub

  3. #3
    Registered User
    Join Date
    06-01-2013
    Location
    United States
    MS-Off Ver
    Excel 2011
    Posts
    64

    Re: Skipping over certain sheets in a For Each loop

    Oh, good call!
    Thanks, worked great!

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Skipping over certain sheets in a For Each loop

    Hello NicholasL,

    Sub emptyUnlocked()
        
         Dim ws As Worksheet, bVBProtected As Boolean
         
         For Each ws In Worksheets
         
            Select Case ws
              ' Names of worksheets to skip
                Case Is = "Sheet1", "Sheet4"
                    ' Do nothing.
                Case Else
                    If Not ws.ProtectContents Then
                        ws.Protect
                        bVBProtected = True
                    End If
            
                    'mikerickson's trick
                    On Error Resume Next
                        ws.UsedRange = ""
                    On Error GoTo 0
            
                    If bVBProtected Then
                        ws.Unprotect
                        bVBProtected = False
                    End If
            End Select
            
        Next ws
        
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Skipping over certain sheets in a For Each loop

    Hi,

    Yes, just add an additional IF test

    Sub emptyUnlocked()
        
         Dim ws As Worksheet, bVBProtected As Boolean
         
         For Each ws In Worksheets
            If ws.CodeName <> "code_name_of_sheet_to_skip"     
                If Not ws.ProtectContents Then
    
                'rest of code
                End If            
            End If
    
    
        Next ws
        
    End Sub
    Note. Use the VBA sheet code name rather than the sheet tab name in case someone should change the tab name.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1