combining multiple workbooks

Lino

Active Member
Joined
Feb 27, 2002
Messages
429
Is there a way to concatenate workbooks.

Suppose you have 30 workbooks in a directory and you know all of the information is in sheet one for each of the 30 workbooks and you want to create a 31st workbook with all of the data in the 30 workbooks.

How can this be done programmatically?

Lino
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hi Lino,

You forgot to mention whether you want one workbook with 30 sheets, or one worksheet with the rows of all 30 worksheets appended to the first worksheet.

Damon
 
Upvote 0
Try this:

Code:
Sub Test()
'   Change Folder name to suit
    Const Folder As String = "P:\TEMP\MrExcel\MergeBooks"
    Dim i As Integer
    Application.ScreenUpdating = False
    With Application.FileSearch
        .NewSearch
        .LookIn = Folder
        .SearchSubFolders = False
        .Filename = "*.xls"
        If .Execute() > 0 Then
            For i = 1 To .FoundFiles.Count
                Workbooks.Open .FoundFiles(i)
                If i = 1 Then
                    ActiveWorkbook.Worksheets("Sheet1").UsedRange.Copy ThisWorkbook.Worksheets("Sheet1").Range("A1")
                Else
                    ActiveWorkbook.Worksheets("Sheet1").UsedRange.Copy ThisWorkbook.Worksheets("Sheet1").Range("A65536").End(xlUp).Offset(1, 0)
                End If
                ActiveWorkbook.Close False
            Next i
        Else
            MsgBox "There were no files found."
        End If
    End With
    Application.ScreenUpdating = True
End Sub

You will need to change the Folder to suit. Note that the code assumes that the target workbook is NOT in the same folder as the workbooks you want to merge.
 
Upvote 0
Hi,
Thanks for this code from yesteryear. I have modified it slightly to consolidate multiple values in fields from multiple workbooks into one summary worksheet.
I've changed the "used range" to individual cells containing the information I want to collate.
However, I'm finding that when one of the source cells contains a null value, it is not copied, and the value from the next worksheet is put in its place.
Is there a way to modify the copy method so that it copies null values also?

Any help would be gratefully appreciated.

Thanks,
Ben
 
Upvote 0

Forum statistics

Threads
1,215,482
Messages
6,125,061
Members
449,206
Latest member
Healthydogs

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