Thursday, July 17, 2014

How to merge worksheets into one worksheet in excel?

Following VBA code can help you to get data from all worksheets of active workbook together
into a new single worksheet. At the same time, all of the worksheets must have the same field
structure, same column headings and same column order

-Hold down ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window
-Click Insert -> Module, and paste the following code in the Module Window

Sub Combine()
Dim J As Integer
On Error Resume Next
Sheets(1).Select
Worksheets.Add
Sheets(1).Name = "Combined"
Sheets(2).Activate
Range("A1").EntireRow.Select
Selection.Copy Destination:=Sheets(1).Range("A1")
For J = 2 To Sheets.Count
Sheets(J).Activate
Range("A1").Select
Selection.CurrentRegion.Select
Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
Selection.Copy Destination:=Sheets(1).Range("A65536").End(xlUp)(2)
Next
End Sub

-Then press F5 to run the code, and all the data in the workbook has been merged into
a new worksheet named Combined.

Note:
-Your data must start from A1
-Your data must have the same structure

No comments:

Post a Comment