Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Merging Many Workbooks.

Merging Many Workbooks

by Allen Wyatt
(last updated January 2, 2017)

4

Joy ran into a problem merging quite a few workbooks together. The majority of the workbooks—about 200 of them, all in a single folder—each contain a single worksheet, but some contain more. The worksheets form each of these workbooks needs to be added to a single workbook.

The easiest way to do merges of this magnitude—particularly if you have to do it often—is with a macro. The following macro displays a dialog box asking you to select the files to merge. (You can select multiple workbooks by holding down the Ctrl key as you click each one.) It loops thru the list you select, opening each one and moving all its worksheets to the end of the workbook with the code.

Sub CombineWorkbooks()
    Dim FilesToOpen
    Dim x As Integer
    Dim Outwbk As Workbook

    Set Outwbk = ActiveWorkbook

    On Error GoTo ErrHandler
    Application.ScreenUpdating = False

    FilesToOpen = Application.GetOpenFilename _
      (FileFilter:="Microsoft Excel Files (*.xls), *.xls", _
      MultiSelect:=True, Title:="Files to Merge")

    If TypeName(FilesToOpen) = "Boolean" Then
        MsgBox "No Files were selected"
        GoTo ExitHandler
    End If

    x = 1
    While x <= UBound(FilesToOpen)
        Workbooks.Open Filename:=FilesToOpen(x)
        Sheets().Move After:=Outwbk.Sheets(Outwbk.Sheets.Count)
        x = x + 1
    Wend

ExitHandler:
    Application.ScreenUpdating = True
    Exit Sub

ErrHandler:
    MsgBox Err.Description
    Resume ExitHandler
End Sub

In the process of adding the worksheets to the end of the workbook, Excel will automatically append a (2), (3), etc. when duplicates worksheet names are detected. Any formulas in the book referring to other sheets will also be updated to reflect the new names.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2409) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Merging Many Workbooks.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Opening a Text File and Template from the Command Line

Word includes a command-line syntax that you can use to open files and do other operations. If you want to load a text file ...

Discover More

Replacing Random Text with Your Own Text

Word includes a little-known function that allows you to put "filler text" into your document. If you want this function to ...

Discover More

Extracting First and Last Words

When working with text phrases stored in cells, it might be helpful to be able to extract words from the phrase. In this tip ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

MORE EXCELTIPS (MENU)

CSV File Opens with Data in a Single Column

When you import a CSV file into an Excel worksheet, you may be surprised at how the program allocates the information among ...

Discover More

Closing Multiple Files

When working with multiple workbooks open, you may want a way to close them all with a single command. Here's the secret.

Discover More

Use Filenames That Sort Properly

When storing your Excel workbook, you need to specify a file name to be used for the workbook. Take a moment to consider how ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments for this tip:

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 - 1?

2015-02-06 13:22:33

Kiara

can any one of you please provide me code for merging sheet 1 of all excel sheets, sheet 2 of all excel sheets and so on in 1 workbook.


2014-12-05 03:56:37

unokashi

Can you please help me with my question.
Scenario:
1. There is a common template which has some tasks listed in columns against which the team members will update the time taken (effort) for each task in the entire month.
2. There are different levels of team members (Entry, Intermediate, Specialist, etc.).
3. The template has multiple sheets, but I need to pick the data from only one specific sheet of the workbook. So in the solution I am looking for, it would be good to have an option to select the required sheet in that workbook.
4. The template has the common tasks listed in the Columns (Say A25 to A156) and on the rows there are these levels (Entry - Cell B24, Inter - Cell C24, Spe - Cell D24, etc) mentioned under which depending on his/ her level, the team member enters the time (effort) for each task [so if he/ she is and Entry, they will enter values (Effort) in B25 to B150 cells]. There are chances that the cells can be empty for few of the tasks also.

Tasks SPE INT ENT COR
Task 1 2
Task 2 4
Task 3 5
Task 4
Task 5 12
Task 6 22
Task 7 1
Task 8
Task 9 4
Task 10 5
Task 11 1
Task 12 12
Task 13
Task 14 1
Task 15 12
Task 16
Task 17 1

5. Each of the team member submits one excel work book with the entries for the entire month based on his level, so I will have multiple such work books in the name of each team member.

The need:
I have a master file which has the same tasks listed in columns and same levels mentioned in the rows. I need to consolidate by adding the effort taken for each task against each level.

A macro with the following would help:
1. An option to specify the path of the folder in which all the excel files contain.
2. A button to 'Update' in the master sheet.


2013-03-17 23:19:55

sundar

Great, but with this code the opened files were not merged!!!!!!


2013-03-17 02:31:13

CdeBruin

Hi Allen,

Confirm that when the While...Wend loop ends, Joy's 200 spread sheets are still open.

This could :
1) Use unnecessary resources
2) Require all 200 to be closed manually

so would I be correct to suggest that after the x = x + 1, one could add….(assuming that the source is the “Active” workbook)

ActiveWorkbook.Close SaveChanges:=False

If I am correct, then each workbook would be closed within the loop.

Many thanks for all the great tips:-)
Regards Corrie


Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.

Links and Sharing
Share