Forum Discussion
Append data from same-named worksheets in multiple excel workbooks to new excel workbook
Hello - I have multiple worksheets of the same name in multiple workbooks. For example, each of Workbooks #1, #2, and #3, I have Worksheets named 'Sheet1', 'Sheet2', 'Sheet3'. Each Worksheet contains the exact same columns across the 3 original Workbooks.
I would like to run a script that cycles through each of the 3 Workbooks and takes 'Sheet1' and appends all rows into a Worksheet called 'Sheet1' in a new Workbook (called 'Workbook 4'). Then, do the same process for 'Sheet2' (into 'Sheet2' of 'Workbook 4'), and the same again for 'Sheet3' (into 'Sheet3' of 'Workbook 4').
A couple of points:
1) The 1st row of all Worksheets is always a Header Row, so it only needs to be brought into each Sheet in 'Workbook 4' once.
2) The original Workbooks (#1, #2, & #3) and the new Workbook #4 will be in the same folder.
Is there some VBA code I can use to accomplish this?
Thank you!
Aargh - you were right all the time. My code contains a stupid typo - my sincere apologies!
The line
r = wsT.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row = 1
should be
r = wsT.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row - 1
with Row - 1 instead of Row = 1.
11 Replies
Try this; please test carefully:
Sub MergeWorkbooks() Dim sFolder As String Dim sFile As String Dim wbT As Workbook Dim wsT As Worksheet Dim wbS As Workbook Dim wsS As Worksheet Dim r As Long With Application.FileDialog(4) ' msoFileDialogFolderPicker If .Show Then sFolder = .SelectedItems(1) Else Beep Exit Sub End If End With On Error GoTo ErrHandler Application.ScreenUpdating = False Application.DisplayAlerts = False If Right(sFolder, 1) <> "\" Then sFolder = sFolder & "\" End If sFile = Dir(sFolder & "*.xls*") Set wbT = Workbooks.Open(sFolder & sFile) sFile = Dir Do While sFile <> "" Set wbS = Workbooks.Open(sFolder & sFile) For Each wsS In wbS.Worksheets Set wsT = wbT.Worksheets(wsS.Name) r = wsT.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row = 1 wsS.UsedRange.Offset(1).Copy Destination:=wsT.Range("A" & r) Application.CutCopyMode = False Next wsS wbS.Close SaveChanges:=False sFile = Dir Loop wbT.SaveAs Filename:=sFolder & "New Workbook.xlsx", FileFormat:=xlOpenXMLWorkbook ' Optional: close the new workbook wbT.Close SaveChanges:=False ExitHandler: Application.DisplayAlerts = True Application.ScreenUpdating = True Exit Sub ErrHandler: MsgBox Err.Description, vbExclamation Resume ExitHandler End Sub
- josevelez5Copper ContributorHi Hans, thank you for your valuable contribution, it saved my day!
In my case I had to make a slight modification by adding 2 to variable "r", as the macro for some reason was "eating" two rows during the first add. - PatDoolsBrass Contributor
Hi Hans - Thank you so much for the code above. I'm getting the following error: Method 'Range' of object '_Worksheet' failed
I've tried launching the code from both a new Workbook in the same directory (and even tried naming all the Worksheets the same as they are named in the existing Workbooks), as well as launching from an existing Workbook, and I get the error either way. I do see that it opens the 1st and 2nd Workbooks in the folder (there are 4 total), once I pick the folder from the Dialogue Box that your code launches, but then the above error appears. I'm a bit of a rookie with VBA, and I'm not sure how to troubleshoot this error - although I do see that both the 'sFile' and 'sFolder' variables that I put a Watch on are empty at the time the error message is launched. Please let me know what might be happening with this error message. Thanks again!
Do all workbooks in the folder that you select have exactly the same number worksheets, with the same names?