+ Reply to Thread
Results 1 to 12 of 12

Looping Worksheet Macro - Skip a Sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    02-02-2015
    Location
    Brisbane
    MS-Off Ver
    2013
    Posts
    5

    Looping Worksheet Macro - Skip a Sheet

    Hi,
    I have this macro to cycle through all worksheets, retrieve data and paste into the "Test Lookup" sheet, but I want to skip one worksheet (called "Specs").

    Any thoughts on how? Thanks.
    Sub Macro3()
    '
    ' Macro3 Macro
    '
    
    '
     Dim i As Long
     For i = 1 To Worksheets.Count
    
        Sheets(i).Select
        Range("G9:H9").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        Sheets("Test Lookup").Select
        Range("A65535").Select
        Selection.End(xlUp).Select
        Selection.Offset(1, 0).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Sheets(i).Select
        ActiveWindow.SmallScroll Down:=-9
        Range("P9").Select
        Range(Selection, Selection.End(xlDown)).Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Test Lookup").Select
        Range("c65535").Select
        Selection.End(xlUp).Select
        Selection.Offset(1, 0).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Next i
        
        
    End Sub
    Last edited by Leith Ross; 02-02-2015 at 03:04 AM. Reason: Added Code Tags

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,953

    Re: Looping Worksheet Macro - Skip a Sheet

    Sub x()
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Sheets
    If ws.Name <> "Test Lookup" And ws.Name <> "Specs" Then
    'do stuff
    Next ws
    
    End Sub
    Ben Van Johnson

  3. #3
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Looping Worksheet Macro - Skip a Sheet

    Try this...

    Sub Macro3()
    Dim Ws As Worksheet
    
    For Each Ws In ThisWorkbook.Worksheets
        With Ws
            If UCase(Ws.Name) <> UCase("Test Lookup") And UCase(Ws.Name) <> UCase("Specs") Then
                Ws.Select
                Range("G9:H9", Range("G9:H9").End(xlDown)).Copy
                Sheets("Test Lookup").Range("A65535").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
                
                ActiveWindow.SmallScroll Down:=-9
                Application.CutCopyMode = False
                
                Range("P9", Range("P9").End(xlDown)).Copy
                Sheets("Test Lookup").Range("c65535").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
                Application.CutCopyMode = False
            End If
        End With
    Next i
    
    End Sub


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  4. #4
    Registered User
    Join Date
    02-02-2015
    Location
    Brisbane
    MS-Off Ver
    2013
    Posts
    5

    Re: Looping Worksheet Macro - Skip a Sheet

    Quote Originally Posted by :) Sixthsense :) View Post
    Try this...

    Sub Macro3()
    Dim Ws As Worksheet
    
    For Each Ws In ThisWorkbook.Worksheets
        With Ws
            If UCase(Ws.Name) <> UCase("Test Lookup") And UCase(Ws.Name) <> UCase("Specs") Then
                Ws.Select
                Range("G9:H9", Range("G9:H9").End(xlDown)).Copy
                Sheets("Test Lookup").Range("A65535").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
                
                ActiveWindow.SmallScroll Down:=-9
                Application.CutCopyMode = False
                
                Range("P9", Range("P9").End(xlDown)).Copy
                Sheets("Test Lookup").Range("c65535").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
                Application.CutCopyMode = False
            End If
        End With
    Next i
    
    End Sub
    This worked (with "Next I" changed to "Next ws") - thanks to both!

  5. #5
    Registered User
    Join Date
    02-02-2015
    Location
    Brisbane
    MS-Off Ver
    2013
    Posts
    5

    Re: Looping Worksheet Macro - Skip a Sheet

    I just realised it's still going back to the "Spec" sheet at the end and copying and trying to paste data (but erroring as there's no data in those cells).

    Any ideas? Thanks.
    Last edited by tudley; 02-02-2015 at 09:15 PM.

  6. #6
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Looping Worksheet Macro - Skip a Sheet

    Hi tudley,

    Here's how you could do it with your existing code:

    Sub Macro3()
        
        Dim i As Long
        
        For i = 1 To Worksheets.Count
    
            If Sheets(i).Name <> "Specs" Then
        
                Sheets(i).Select
                Range("G9:H9").Select
                Range(Selection, Selection.End(xlDown)).Select
                Selection.Copy
                Sheets("Test Lookup").Select
                Range("A65535").Select
                Selection.End(xlUp).Select
                Selection.Offset(1, 0).Select
                Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Sheets(i).Select
                ActiveWindow.SmallScroll Down:=-9
                Range("P9").Select
                Range(Selection, Selection.End(xlDown)).Select
                Application.CutCopyMode = False
                Selection.Copy
                Sheets("Test Lookup").Select
                Range("c65535").Select
                Selection.End(xlUp).Select
                Selection.Offset(1, 0).Select
                Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        
            End If
        
        Next i
        
    End Sub
    HTH

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  7. #7
    Registered User
    Join Date
    02-02-2015
    Location
    Brisbane
    MS-Off Ver
    2013
    Posts
    5

    Re: Looping Worksheet Macro - Skip a Sheet

    Quote Originally Posted by Trebor76 View Post
    Hi tudley,

    Here's how you could do it with your existing code:

    Sub Macro3()
        
        Dim i As Long
        
        For i = 1 To Worksheets.Count
    
            If Sheets(i).Name <> "Specs" Then
        
                Sheets(i).Select
                Range("G9:H9").Select
                Range(Selection, Selection.End(xlDown)).Select
                Selection.Copy
                Sheets("Test Lookup").Select
                Range("A65535").Select
                Selection.End(xlUp).Select
                Selection.Offset(1, 0).Select
                Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Sheets(i).Select
                ActiveWindow.SmallScroll Down:=-9
                Range("P9").Select
                Range(Selection, Selection.End(xlDown)).Select
                Application.CutCopyMode = False
                Selection.Copy
                Sheets("Test Lookup").Select
                Range("c65535").Select
                Selection.End(xlUp).Select
                Selection.Offset(1, 0).Select
                Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        
            End If
        
        Next i
        
    End Sub
    HTH

    Robert
    Thanks, I still get a runtime error as it's copying all the blank data in the "Specs" worksheet.

    Quote Originally Posted by :) Sixthsense :) View Post
    I given code based on your above statements.

    If your sheet name is spec not specS then correct it in the provided code.
    Sorry, my typo, the worksheet name is "Specs".

    Also realised it was looking up data in blank sheets at the end. Deleting them and all good, except it runtime errors at "ws.select" when it gets to the end...
    Last edited by tudley; 02-03-2015 at 12:02 AM.

  8. #8
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Looping Worksheet Macro - Skip a Sheet

    Quote Originally Posted by tudley View Post
    except it runtime errors at "ws.select" when it gets to the end...
    I believe there is some availability of hidden worksheet(s)

    Try this revised code

    Sub Macro3()
    Dim Ws As Worksheet
    
    For Each Ws In ThisWorkbook.Worksheets
        With Ws
            If Ws.Visible = xlSheetVisible Then
                If UCase(Ws.Name) <> UCase("Test Lookup") And UCase(Ws.Name) <> UCase("Specs") Then
                
                    Ws.Select
                    Range("G9:H9", Range("G9:H9").End(xlDown)).Copy
                    Sheets("Test Lookup").Range("A65535").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
                    
                    ActiveWindow.SmallScroll Down:=-9
                    Application.CutCopyMode = False
                    
                    Range("P9", Range("P9").End(xlDown)).Copy
                    Sheets("Test Lookup").Range("c65535").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
                    Application.CutCopyMode = False
                End If
            End If
        End With
    Next i
    
    End Sub

  9. #9
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Looping Worksheet Macro - Skip a Sheet

    Quote Originally Posted by tudley View Post
    I want to skip one worksheet (called "Specs")
            If UCase(Ws.Name) <> UCase("Test Lookup") And UCase(Ws.Name) <> UCase("Specs") Then
    Quote Originally Posted by tudley View Post
    I just realised it's still going back to the "Spec" sheet at the end and copying
    I given code based on your above statements.

    If your sheet name is spec not specS then correct it in the provided code.

  10. #10
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Looping Worksheet Macro - Skip a Sheet

    Thanks, I still get a runtime error as it's copying all the blank data in the "Specs" worksheet.
    I don't know what you mean as it should do nothing with that tab - it didn't for me when I created a workbook and called one of the tabs "Specs"

    Are you sure there's no trailing spaces in the tab name?

  11. #11
    Registered User
    Join Date
    02-02-2015
    Location
    Brisbane
    MS-Off Ver
    2013
    Posts
    5

    Re: Looping Worksheet Macro - Skip a Sheet

    @ Sixthsense - that worked - thankyou!

    @ Trebor76 - I thought that was strange too - just renamed the sheet again and it now skips it - working as well, so I have two options, thanks!

    Thanks all for you help.

  12. #12
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Looping Worksheet Macro - Skip a Sheet

    @ Trebor76 - I thought that was strange too - just renamed the sheet again and it now skips it - working as well, so I have two options, thanks!
    There must have a space (or spaces) either at the start and/or end of the tab name.

    If you could mark the thread as solved it would be appreciated.

    Regards,

    Robert

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] can't get macro to skip worksheet if row 2 is blank...
    By fabrecass in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-28-2012, 05:10 AM
  2. [SOLVED] Multiiple Worksheet Loop Macro only Looping on 1 sheet
    By lscarstens in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-20-2012, 02:44 PM
  3. [SOLVED] Event macro to skip worksheet
    By nickmax1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-31-2012, 11:42 AM
  4. Replies: 1
    Last Post: 08-18-2012, 06:59 PM
  5. macro to skip delete sheet question
    By excellentexcel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-13-2009, 08:40 AM

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