Copy all sheets into a new one

Yulyo

Board Regular
Joined
Jul 17, 2017
Messages
94
Hello all,

I have a workbook with around 500 sheets. All sheets look the same, same number of columns, etc.
I need a vba to copy the content from all sheets into a new one and paste them one after another.

Here is what i managed to do until now...

Code:
Sub Combine()

Worksheets.Add
Sheets(1).Name = "Combined"



Sheets(1).Range("A1").EntireRow.Value = Sheets(2).Range("A1").EntireRow.Value 'not most effecient, but will do for this



Dim J As Integer
For J = 2 To Sheets.Count ' from sheet 2 to last sheet
    With Sheets(J)
        .Range(.Cells(2, 1), .Cells(.Range("A" & .Rows.Count).End(xlUp).Row, .Cells(2, .Columns.Count).End(xlToLeft).Column)).Copy _
             Sheets(1).Range("A" & Rows.Count).End(xlUp).Offset(2)
    End With
Next


End Sub


The problem that i have right now, is that if the Sheet "Combined" already exists, i recieve runtime error 1004 (the name is already taken).

Regarding the fact that i will execute this code a few times everyday, this is a dealbreaker...

Could you please help me with a solution? How can i delete the sheet "Combined" befor executing the rest of the code?

Thank you in advance.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
How about
Code:
Sub Combinesheets()
   Dim Ws As Worksheet
   Dim ComWs As Worksheet
   
   If Not Evaluate("isref(Combined!A1)") Then Sheets.Add(1).Name = "Combined"
   Set ComWs = Sheets("Combined")
   ComWs.UsedRange.Offset(1).Clear
   For Each Ws In Worksheets
      If Not Ws.Name = "Combined" Then
         Ws.UsedRange.Offset(1).Copy ComWs.Range("A" & Rows.Count).End(xlUp).Offset(1)
      End If
   Next Ws
End Sub
 
Upvote 0
Thank a lot, Fluff

Could you please help me making the vba to copy only the rows that contains data? (right now i have a few hundred rows between sheets...)

Thank you
 
Upvote 0
Do you have formulae in the sheets extending below the data?
 
Upvote 0
Every sheet have some empty rows bellow the data, in case some new data must be entered.
So, I think that the empty rows are imported too, into "combined" sheet...
 
Last edited:
Upvote 0
Is there any data below the "Empty Rows"?
 
Upvote 0
In that case I suspect that the "Empty" rows are not empty. They may have spaces, hidden characters, or Formula that return "".
 
Upvote 0

Forum statistics

Threads
1,216,082
Messages
6,128,700
Members
449,464
Latest member
againofsoul

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top