+ Reply to Thread
Results 1 to 8 of 8

Looping through worksheets to copy/paste to new sheet - last sheet never copies

Hybrid View

  1. #1
    Registered User
    Join Date
    12-10-2011
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2007
    Posts
    10

    Looping through worksheets to copy/paste to new sheet - last sheet never copies

    Hello all - I have a simple sub that creates a new sheet, then loops through all the remaining sheets to copy its data and paste it to the new sheet. For some reason, however, it never copies the last sheet. My code:

    Sub CopyPasta()
    
    Application.ScreenUpdating = False
    Worksheets.Add(Before:=Worksheets(1)).Name = "Data Sheet"
    
    Dim ws As Worksheet
             
       For Each ws In Worksheets
       
          If ws.Name <> "Data Sheet" Then
              Range("A2").Select
              Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Copy Destination:=Worksheets("Data Sheet").Range("A1048576").End(xlUp).Offset(1, 0)
              ws.Activate
          End If
       Next
       
    Worksheets("Data Sheet").Activate
    Rows("1").EntireRow.Delete
    Range("A1").Select
    Application.ScreenUpdating = True
                     
    End Sub
    I have tried using a variable counter, i.e. - For i = 1 to ActiveWorkbooks.Worksheets.Count - and I get the same results, it copies/pastes all but the last sheet.

    This happens no matter how many sheets I try to copy. What's more, if I add one to the counter, i.e. - For i = 1 to ActiveWorkbooks.Worksheets.Count + 1 - it throws a "subscript out of range" error, yet it manages to copy all the sheets as needed. I can add an error handler to ignore the error, but I'd rather not leave it that way.

    I know the loop is reaching all the sheets, but I cannot for the life of me figure out why it will not copy/paste that last sheet. What am I missing? Any ideas?

    Thanks - jfp
    Last edited by lifeboat; 12-11-2011 at 10:35 AM.

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Looping through worksheets to copy/paste to new sheet - last sheet never copies

    maybe so
    Sub CopyPasta()
    Application.ScreenUpdating = False
    Worksheets.Add(Before:=Worksheets(1)).Name = "Data Sheet"
    Dim ws As Worksheet
    With Worksheets("Data Sheet")
        For Each ws In Worksheets
            If ws.Name <> "Data Sheet" Then
                ws.Range(ws.[a2], ws.[a2].SpecialCells(xlLastCell)).Copy .Range("A1048576").End(xlUp).Offset(1, 0)
            End If
        Next
        .Rows("1").Delete
        .Activate
        .Range("A1").Select
    End With
    Application.ScreenUpdating = True
    End Sub

  3. #3
    Registered User
    Join Date
    12-10-2011
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Looping through worksheets to copy/paste to new sheet - last sheet never copies

    nilem - thank you very much. That works perfectly.

    Out of curiosity, do you (or anyone else reading) know why the original code skipped the copy/paste option for the last sheet?

  4. #4
    Forum Expert
    Join Date
    09-27-2011
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    1,312

    Re: Looping through worksheets to copy/paste to new sheet - last sheet never copies

    hi look at this code I think it is what you need

    Sub CopyPasta()
    
    Application.ScreenUpdating = False
    Worksheets.Add(Before:=Worksheets(1)).Name = "Data Sheet"
    
    Dim ws As Worksheet
             
       For Each ws In Worksheets
       
          If ws.Name <> "Data Sheet" Then
              ws.Range("A2:" & ws.Cells.SpecialCells(xlLastCell).Address).Copy Destination:=Worksheets("Data Sheet").Range("A1048576").End(xlUp).Offset(1, 0)
             
          End If
       Next
       
    Worksheets("Data Sheet").Rows("1").EntireRow.Delete
    Range("A1").Select
    Application.ScreenUpdating = True
                     
    End Sub
    Regards

    tom1977

    If You are satisfied with my solution click the small star icon on the left to say thanks.

  5. #5
    Forum Expert
    Join Date
    09-27-2011
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    1,312

    Re: Looping through worksheets to copy/paste to new sheet - last sheet never copies

    try to run your code in steps (pressing F8 and everything will be clear)

  6. #6
    Registered User
    Join Date
    12-10-2011
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Looping through worksheets to copy/paste to new sheet - last sheet never copies

    tom1977 - thank you as well; your code also worked as needed. Will look at it in steps as you suggest to see what I'm missing.

    Thanks again.

  7. #7
    Forum Expert
    Join Date
    09-27-2011
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    1,312

    Re: Looping through worksheets to copy/paste to new sheet - last sheet never copies

    look also at this. This is Your code without any changes (I changed only places of two rows)
    Sub CopyPasta()
    
    Application.ScreenUpdating = False
    Worksheets.Add(Before:=Worksheets(1)).Name = "Data Sheet"
    
    Dim ws As Worksheet
             
       For Each ws In Worksheets
       
          If ws.Name <> "Data Sheet" Then
             
              ws.Activate
               Range("A2").Select
              Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Copy Destination:=Worksheets("Data Sheet").Range("A1048576").End(xlUp).Offset(1, 0)
              
          End If
       Next
       
    Worksheets("Data Sheet").Activate
    Rows("1").EntireRow.Delete
    Range("A1").Select
    Application.ScreenUpdating = True
                     
    End Sub
    and remember that you do not have to select or activate any objects to do something with them
    Last edited by tom1977; 12-10-2011 at 01:33 PM.

  8. #8
    Registered User
    Join Date
    12-10-2011
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Looping through worksheets to copy/paste to new sheet - last sheet never copies

    Thanks again, Tom - I see that now with not needing to activate or select objects; I'm relatively new to this.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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