Combining All Open Workbooks into Single Worksheet

SGMacro

New Member
Joined
Jul 11, 2015
Messages
16
I have been looking around websites and forums for a solution to combining all open workbooks into single worksheets and really struggling here as I'm completely new to VB.

I found the following code that combines all open workbooks sheet1 into one workbook as separate tabs which is great but does not achieve what I need it to do. I have tried to modify and failed.

Can anyone help with this as I'm really struggling.


Code:
Sub CombinedWorksheetsFromOpenWorkbook()
    Dim Wkb As Workbook
    Dim sWksName As String


    sWksName = "Sheet1"
    For Each Wkb In Workbooks
        If Wkb.Name <> ThisWorkbook.Name Then
            Wkb.Worksheets(sWksName).Copy _
              Before:=ThisWorkbook.Sheets(1)
        End If
    Next
    Set Wkb = Nothing
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I have been looking around websites and forums for a solution to combining all open workbooks into single worksheets and really struggling here as I'm completely new to VB.

I found the following code that combines all open workbooks sheet1 into one workbook as separate tabs which is great but does not achieve what I need it to do. I have tried to modify and failed.

Can anyone help with this as I'm really struggling.


Code:
Sub CombinedWorksheetsFromOpenWorkbook()
    Dim Wkb As Workbook
    Dim sWksName As String


    sWksName = "Sheet1"
    For Each Wkb In Workbooks
        If Wkb.Name <> ThisWorkbook.Name Then
            Wkb.Worksheets(sWksName).Copy _
              Before:=ThisWorkbook.Sheets(1)
        End If
    Next
    Set Wkb = Nothing
End Sub



Sub CopyFromWorksheets()
Dim wrk As Workbook 'Workbook object - Always good to work with object variables
Dim sht As Worksheet 'Object for handling worksheets in loop
Dim trg As Worksheet 'Master Worksheet
Dim rng As Range 'Range object
Dim colCount As Integer 'Column count in tables in the worksheets

Set wrk = ActiveWorkbook 'Working in active workbook

For Each sht In wrk.Worksheets
If sht.Name = "Master" Then
MsgBox "There is a worksheet called as 'Master'." & vbCrLf & _
"Please remove or rename this worksheet since 'Master' would be" & _
"the name of the result worksheet of this process.", vbOKOnly + vbExclamation, "Error"
Exit Sub
End If
Next sht

'We don't want screen updating
Application.ScreenUpdating = False

'Add new worksheet as the last worksheet
Set trg = wrk.Worksheets.Add(After:=wrk.Worksheets(wrk.Worksheets.Count))
'Rename the new worksheet
trg.Name = "Master"
'Get column headers from the first worksheet
'Column count first
Set sht = wrk.Worksheets(1)
colCount = sht.Cells(1, 255).End(xlToLeft).Column
'Now retrieve headers, no copy&paste needed
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 worksheet in loop is the last one, stop execution (it is Master worksheet)
If sht.Index = wrk.Worksheets.Count Then
Exit For
End If
'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
Next sht
'Fit the columns in Master worksheet
trg.Columns.AutoFit

'Screen updating should be activated
Application.ScreenUpdating = True
End Sub






Does this work for you?
 
Upvote 0
Hi Jamie, Thanks for looking at this.

I've tried your code but this copies the text from the active sheet containing the macro and pastes it into a separate master sheet.

I am trying to copy text from "B2:Q10000" in workbooks external to the workbook containing the macro.
Then paste into the active sheet below existing text without overwriting it.
 
Upvote 0
I've done some more digging and come up with the following 2 step process.

1 - Combine worksheets from any open workbooks macro
2 - Merge worksheets within workbook into 'combined' sheet

Its a bit clunky and would be great if it could be rolled up into 1 process.

Code:
Sub CombinedWorksheetsFromOpenWorkbook()
    Dim Wkb As Workbook
    Dim sWksName As String




    sWksName = "Sheet1"
    For Each Wkb In Workbooks
        If Wkb.Name <> ThisWorkbook.Name Then
            Wkb.Worksheets(sWksName).Copy _
              Before:=ThisWorkbook.Sheets(1)
        End If
    Next
    Set Wkb = Nothing
End Sub

Code:
Sub MergeWorksheetsIntoOneWorksheet()
Dim J As Integer
On Error Resume Next
Sheets(1).Select
Worksheets.Add
Sheets(1).Name = "Combined"
Sheets(2).Activate
Range("A1").EntireRow.Select
Selection.Copy Destination:=Sheets(1).Range("A1")
For J = 2 To Sheets.Count
Sheets(J).Activate
Range("A1").Select
Selection.CurrentRegion.Select
Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
Selection.Copy Destination:=Sheets(1).Range("A65536").End(xlUp)(2)
Next
End Sub
 
Upvote 0
I've done some more digging and come up with the following 2 step process.

1 - Combine worksheets from any open workbooks macro
2 - Merge worksheets within workbook into 'combined' sheet

Its a bit clunky and would be great if it could be rolled up into 1 process.

Code:
Sub CombinedWorksheetsFromOpenWorkbook()
    Dim Wkb As Workbook
    Dim sWksName As String




    sWksName = "Sheet1"
    For Each Wkb In Workbooks
        If Wkb.Name <> ThisWorkbook.Name Then
            Wkb.Worksheets(sWksName).Copy _
              Before:=ThisWorkbook.Sheets(1)
        End If
    Next
    Set Wkb = Nothing
End Sub

Code:
Sub MergeWorksheetsIntoOneWorksheet()
Dim J As Integer
On Error Resume Next
Sheets(1).Select
Worksheets.Add
Sheets(1).Name = "Combined"
Sheets(2).Activate
Range("A1").EntireRow.Select
Selection.Copy Destination:=Sheets(1).Range("A1")
For J = 2 To Sheets.Count
Sheets(J).Activate
Range("A1").Select
Selection.CurrentRegion.Select
Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
Selection.Copy Destination:=Sheets(1).Range("A65536").End(xlUp)(2)
Next
End Sub

Put this at the end of the first one:

Call MergeWorksheetsIntoOneWorksheet
 
Upvote 0
Put this at the end of the first one:

Call MergeWorksheetsIntoOneWorksheet

I'm having trouble getting this to work, results in following error:

Compile error:
Only comments may appear after End Sub, End Function, or End Property



Code:
Sub CombinedWorksheetsFromOpenWorkbook() 'Worksheets only import when named Sheet1
    Dim Wkb As Workbook
    Dim sWksName As String


'Application.ScreenUpdating = False Stops Screen Flicker When Running VBA Code
Application.ScreenUpdating = False


    sWksName = "Sheet1"
    For Each Wkb In Workbooks
        If Wkb.Name <> ThisWorkbook.Name Then
            Wkb.Worksheets(sWksName).Copy _
              Before:=ThisWorkbook.Sheets(1)
        End If
    Next
    Set Wkb = Nothing
End Sub


Call MergeWorksheetsIntoOneWorksheet


Sub MergeWorksheetsIntoOneWorksheet()
Dim J As Integer


'Application.ScreenUpdating = False Stops Screen Flicker When Running VBA Code
Application.ScreenUpdating = False


On Error Resume Next
Sheets(1).Select
Worksheets.Add
Sheets(1).Name = "Summary"
Sheets(2).Activate
Range("A1").EntireRow.Select
Selection.Copy Destination:=Sheets(1).Range("A1")
For J = 2 To Sheets.Count
Sheets(J).Activate
Range("A1").Select
Selection.CurrentRegion.Select
Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
Selection.Copy Destination:=Sheets(1).Range("A65536").End(xlUp)(2)
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top