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)

7

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

Starting a Discussion

Tips for getting your Discussion started.

Discover More

Removing a Macro from a Shortcut Key

Associate a macro with a shortcut key, and at some time you may want to break that association. (Perhaps so the shortcut key ...

Discover More

Deleting a Printer

When you get rid of an old printer and install a new one, you'll want to get rid of the old printer definition maintained in ...

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)

Adding a File Path and Filename

If you need to stuff the current workbook's filename and path into a cell or a header or footer, you'll appreciate the ...

Discover More

Specifying a Delimiter when Saving a CSV File in a Macro

You can, within a macro, save a workbook in several different file formats that are understood by Excel. However, you may not ...

Discover More

Getting Rid of "Copy of"

When you save a read-only workbook file under a new name, Excel automatically adds "copy of" to the beginning of that name. ...

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 nine minus 3?

2017-04-20 16:24:50

Conrad

Update - Sorry, answered my own question, For anyone else's benefit,

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

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

Thank you so much for the macro!


2017-04-20 16:00:31

Conrad

Is there a way to modify this to look for other Excel formats? Like .xlsm, or xlsx?


2017-04-10 16:21:16

Bob Westdyk

Is there anyway to only combine sheet one of each workbook. They all have 3 sheets, but the data is only on sheet one.

Second request, is can all that is copied from sheet one of each workbook all be copied to sheet one of the workbook that the macro is in?

Have it copy to its own sheet defeats the purpose of trying to get all the data on one sheet.

thanks


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


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

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