Hi,
I am trying to merge multiple sheets into a single worksheet within the same workbook. I can't figure out why my code is only pasting in the first column and not the whole data set in each sheet. (Bonus help would be to copy the first row (field names) from the first sheet only). Below is the code I am currently using:
I am trying to merge multiple sheets into a single worksheet within the same workbook. I can't figure out why my code is only pasting in the first column and not the whole data set in each sheet. (Bonus help would be to copy the first row (field names) from the first sheet only). Below is the code I am currently using:
Code:
Set wrk = ActiveWorkbook 'Working in active workbook
Set sht = wrk.Sheets(2)
'Add new worksheet as the last worksheet
Set trg = wrk.Worksheets.Add(After:=Sheets("Macro"))
'Rename the new worksheet
trg.Name = "Raw Data" & "_" & Format(Date, "mmdd")
'Get column headers from the first worksheet
'Column count first
Set sht = wrk.Sheets(2)
colCount = sht.Cells(1, 255).End(xlToLeft).Column
With trg.Cells(1, 1).Resize(1, colCount)
.Value = sht.Cells(1, 1).Resize(1, colCount).Value
'Set font as bold
.Font.Bold = True
End With
'We can start loop
For Each sht In wrk.Worksheets
If sht.Name <> "Macro" Then
'Data range in worksheet - starts from second row as first rows are the header rows in all worksheets
Set rng = sht.Range(sht.Cells(2, 1), sht.Cells(65536, 1).End(xlUp).Resize(, colCount))
'Put data into the Master worksheet
trg.Cells(65536, 1).End(xlUp).Offset(1).Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
End If
Next sht
'Fit the columns in Master worksheet
trg.Columns.AutoFit