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
Bookmarks