Forum Discussion
Append data from same-named worksheets in multiple excel workbooks to new excel workbook
- Feb 21, 2023
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.
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
- josevelez5Aug 22, 2024Copper 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. - PatDoolsFeb 21, 2023Brass 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!
- HansVogelaarFeb 21, 2023MVP
Do all workbooks in the folder that you select have exactly the same number worksheets, with the same names?
- PatDoolsFeb 21, 2023Brass Contributor
Hi Hans - yes, all workbooks in the directory have exactly the same number worksheets, with the same names and there are no hidden worksheets. For starters, should I be running this code from a separate Workbook, or should I be running the code from one of the 'source' Workbooks in the directory? Also, the code opens the first 2 of the 4 Workbooks in the directory before it errors out. I just recreated the 3rd and 4th Workbooks, just to see if there was any corruption, but this is not the case - still seeing the error. I'm including a screenshot of the Worksheet names so you can see they are the same. What else might be happening here? Thank you!