Written by Allen Wyatt (last updated July 20, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365
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 forming 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 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:=ThisWorkbook.Sheets _ (ThisWorkbook.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.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12652) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Merging Many Workbooks.
Best-Selling VBA Tutorial for Beginners Take your Excel knowledge to the next level. With a little background in VBA programming, you can go well beyond basic spreadsheets and functions. Use macros to reduce errors, save time, and integrate with other Microsoft applications. Fully updated for the latest version of Office 365. Check out Microsoft 365 Excel VBA Programming For Dummies today!
When you open a workbook, Excel displays the worksheet that was visible when the workbook was last saved. You may want, ...
Discover MoreIf you are using Excel as a repository for data used in your business, you may want to figure out a way to make that ...
Discover MoreWhen you save a workbook, you have the opportunity to save a thumbnail image that can be displayed within Windows. Here's ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2024-07-21 14:59:42
J. Woolley
The Tip's macro references ThisWorkbook, which is the location of the macro; that will be a problem if the macro is relocated to Personal.xlsb or an add-in. Here is a recommended replacement for the macro's While loop:
Dim AWB As Workbook
Set AWB = ActiveWorkbook
For x = LBound(FilesToOpen) To UBound(FilesToOpen)
Workbooks.Open(FilesToOpen(x)).Sheets.Move _
After:=AWB.Sheets(AWB.Sheets.Count)
Next x
Notice the macro opens workbooks but doesn't close them. Apparently they close automatically after all their sheets are moved, but they retain their sheets as if the sheets were only copied. Here is another version that is more specific:
Dim AWB As Workbook
Set AWB = ActiveWorkbook
For x = LBound(FilesToOpen) To UBound(FilesToOpen)
With Workbooks.Open(FilesToOpen(x))
.Sheets.Copy After:=AWB.Sheets(AWB.Sheets.Count)
.Close SaveChanges:=False
End With
Next x
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments