I Have multiple worksheets (like 24 in number!). I would like to merge it into single sheet. All the worksheets have similar structure with header.
Glitch: At the end of every worksheet there is one or two rows with data summary
I would like to omit those line and have continues data of all worksheets.
Here is a piece of code which I used to merge it. But it made multiple sheets in single excel file. Is it possible to add some code within this piece of code.
Thanks in advance!
Sub GetSheets()
Path = "C:\path"
Filename = Dir(Path & "*.XLSX")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet
Workbooks(Filename).Close
Filename = Dir()
Loop
End Sub
What does following code do:
- Code will copy data from all the sheets of all .xlsx
files in the specified folder assuming all files have same structure
- Data is copied to sheet name Output
of active file
- Last row of each sheet is not copied assuming it contains data summary
- Header will be copied from the first copied sheet
- Code will not add sheets to current file
Sub GetSheets()
Dim path As String, fileName As String
Dim lastRow As Long, rowCntr As Long, lastColumn As Long
Dim outputWS As Worksheet
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'this is the sheet where all the data will be displyed
Set outputWS = ThisWorkbook.Sheets("Output")
rowCntr = 1
path = "C:\path" & "\"
fileName = Dir(path & "*.XLSX")
Do While fileName <> ""
Workbooks.Open fileName:=path & fileName, ReadOnly:=True
For Each ws In ActiveWorkbook.Sheets
If rowCntr = 1 Then
'get column count
lastColumn = ws.Cells(1, Columns.Count).End(xlToLeft).Column
'copy header
Range(outputWS.Cells(1, 1), outputWS.Cells(1, lastColumn)).Value = Range(ws.Cells(1, 1), ws.Cells(1, lastColumn)).Value
rowCntr = rowCntr + 1
End If
'get last row with data of each sheet
lastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row
'copy data from each sheet to Output sheet
Range(outputWS.Cells(rowCntr, 1), outputWS.Cells(rowCntr + lastRow - 3, lastColumn)).Value = Range(ws.Cells(2, 1), ws.Cells(lastRow - 1, lastColumn)).Value
rowCntr = rowCntr + lastRow - 2
Next ws
Workbooks(fileName).Close
fileName = Dir()
Loop
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub