Forum Discussion
Combination of multiple excel workbooks with multiple sheets (not in a single sheet)
HansVogelaar
Hello! 🙂
All sheets do have the same names and I do not want to skip any sheets.
Thanks in advantage!
Try this.
The target workbook should already have the 5 correctly named sheets.
It should be the active workbook when you run the macro.
You'll be prompted for each workbook to process.
Click Cancel when you want to stop.
Sub CombineWorkbooks()
Dim wbkS As Workbook
Dim wbkT As Workbook
Dim wshS As Worksheet
Dim wshT As Worksheet
Dim strFile As String
Dim lngS As Long
Dim lngT As Long
Application.ScreenUpdating = False
Set wbkT = ActiveWorkbook
Do
strFile = Application.GetOpenFilename( _
FileFilter:="Excel Workbooks (*.xls*),*.xls*", _
Title:="Select a workbook to process. Click Cancel to stop")
If strFile = "False" Then Exit Do
Set wbkS = Workbooks.Open(Filename:=strFile)
For Each wshS In wbkS.Worksheets
Set wshT = wbkT.Worksheets(wshS.Name)
lngS = 0
lngT = 0
On Error Resume Next
lngS = wshS.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
lngT = wshT.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
On Error GoTo 0
If lngS > 1 Then
If lngT = 0 Then
wshS.Range("A1:A" & lngS).EntireRow.Copy Destination:=wshT.Range("A1")
Else
wshS.Range("A2:A" & lngS).EntireRow.Copy Destination:=wshT.Range("A" & lngT + 1)
End If
End If
Next wshS
wbkS.Close SaveChanges:=False
Loop
Application.ScreenUpdating = True
End Sub- LaSta95Apr 07, 2021Copper Contributor
Thank you Hans, it works! 🙂
I do have a follow-up question: Do you know if it would be possible to split the newly created master based on a column? so each sheet has a specific column (lets say BZ) and there is either an A or an B as a cell value included. I now want to have my five sheets in the wb seperated into to masters A and B, where I have the same sheet structure.Thanks a lot in advantage!
- HansVogelaarApr 07, 2021MVP
Am I correct in understanding that you want to end up with 10 sheets?
- LaSta95Apr 07, 2021Copper Contributor
HansVogelaar
Hello Hans,I want to have two (master) workbooks, each has the original 5 sheets, but it is splitted based on the column
so Master 1 has 5 sheets but in column BZ is only A, in each sheet
and Master 2 has 5 sheets buit in column BZ is only B, in each sheet