PDA

View Full Version : [SOLVED:] Add Header When Merging Data from Multiple Workbooks/Worksheets



BenChod
06-02-2017, 07:26 AM
Hello All -

I have multiple worksheets from multiple workbooks where I want to append data into a master worksheet. Each worksheet will have the same headers and when appending, I want the header on the top row and the data from the worksheets to append without their headers. The code will copy from the second row until the end (sans the header). The code is copied below. I added a line code at the end to insert a row and then copy the header and it's not working. I am hoping someone can take a quick look and tell me what I am doing wrong. Thanks for your help.


Sub Basic_Example_2()
Dim MyPath As String
Dim SourceRcount As Long, Fnum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long
Dim SaveDriveDir As String
Dim FName As Variant
Dim FirstCell As String




'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With


SaveDriveDir = CurDir
ChDirNet "C:\Data\Test"


FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xl*), *.xl*", _
MultiSelect:=True)
If IsArray(FName) Then


'Add a new workbook with one sheet
Set BaseWks = ThisWorkbook.Worksheets("QC")
rnum = 1




'Loop through all files in the array(myFiles)
For Fnum = LBound(FName) To UBound(FName)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(FName(Fnum))
On Error GoTo 0


If Not mybook Is Nothing Then


On Error Resume Next
With mybook.Worksheets(1)
FirstCell = "A2"
Set sourceRange = .Range(FirstCell & ":" & RDB_Last(3, .Cells))
'Test if the row of the last cell >= then the row of the FirstCell
If RDB_Last(1, .Cells) < .Range(FirstCell).Row Then
Set sourceRange = Nothing
End If
End With




If Err.Number > 0 Then
Err.Clear
Set sourceRange = Nothing
Else
'if SourceRange use all columns then skip this file
If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0


If Not sourceRange Is Nothing Then


SourceRcount = sourceRange.Rows.Count


If rnum + SourceRcount >= BaseWks.Rows.Count Then
MsgBox "Sorry there are not enough rows in the sheet"
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
GoTo ExitTheSub
Else


'Copy the file name in column A
With sourceRange
BaseWks.Cells(rnum, "A"). _
Resize(.Rows.Count).Value = FName(Fnum)
End With


'Set the destrange
Set destrange = BaseWks.Range("B" & rnum)


'we copy the values from the sourceRange to the destrange
With sourceRange
Set destrange = destrange. _
Resize(.Rows.Count, .Columns.Count)
End With
destrange.Value = sourceRange.Value


rnum = rnum + SourceRcount
End If
End If
mybook.Close savechanges:=False
End If


Next Fnum
BaseWks.Columns.AutoFit
End If






ExitTheSub:
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
ChDirNet SaveDriveDir

Range("A1").EntireRow.Insert
'Copy header row, change the range if you use more columns
If WorksheetFunction.CountA(BaseWks.UsedRange) = 0 Then
sourceRange.Range("A1:Z1").Copy BaseWks.Range("A1")


End If
End Sub

p45cal
06-02-2017, 11:08 AM
I would move the line:
FirstCell = "A2"
from where it is to directly after this line:
rnum = rnum + SourceRcount
then I would add the line:
FirstCell = "A1"
directly before thiis line:
For Fnum = LBound(FName) To UBound(FName)

That way (if I've got it right) the first copy will include the header row, but after the first successful copy, only the data rows will be copied.

BenChod
06-02-2017, 01:18 PM
I would move the line:
FirstCell = "A2"
from where it is to directly after this line:
rnum = rnum + SourceRcount
then I would add the line:
FirstCell = "A1"
directly before thiis line:
For Fnum = LBound(FName) To UBound(FName)

That way (if I've got it right) the first copy will include the header row, but after the first successful copy, only the data rows will be copied.

BenChod
06-02-2017, 01:19 PM
Thank you so much. Worked perfectly.

p45cal
06-03-2017, 03:14 AM
I've come across a cross post here: https://www.mrexcel.com/forum/excel-questions/1008130-add-header-when-merging-data-multiple-workbooks-worksheets.html

BenChod,
Netiquette.
Please have a read of http://www.excelguru.ca/content.php?184
While you're at it, if you have solved your problem, it's only fair to update all threads where you've cross-posted to that it has been solved, and perhaps even how it was solved.
I for one, will be reluctant to help you again unless…

BenChod
06-03-2017, 06:46 AM
My bad and apologies. So far my experience and the help I have received from this forum has been phenomenal. I will make sure I follow the proper netiquette going forward. Thanks again to those who have assisted.