Merge Multiple Excel Files
Merge Multiple Excel Files
Home » Macros & Excel VBA » How to Merge Multiple Excel Files into One Sheet by VBA (3 Criteria)
Implementing VBA macro is the most effective, quickest and safest method to run any
operation in Excel. In this article, we will show you how to merge multiple Excel files into
one sheet or one workbook if needed in Excel with the VBA macro.
Table of Contents [
hide
]
3 Criteria to Merge Multiple Excel Files into One Sheet Using VBA
Conclusion
https://www.exceldemy.com/merge-multiple-excel-files-into-one-sheet-vba/ 1/22
8/2/22, 10:47 AM How to Merge Multiple Excel Files into One Sheet by VBA (3 Criteria)
https://www.exceldemy.com/merge-multiple-excel-files-into-one-sheet-vba/ 2/22
8/2/22, 10:47 AM How to Merge Multiple Excel Files into One Sheet by VBA (3 Criteria)
https://www.exceldemy.com/merge-multiple-excel-files-into-one-sheet-vba/ 3/22
8/2/22, 10:47 AM How to Merge Multiple Excel Files into One Sheet by VBA (3 Criteria)
https://www.exceldemy.com/merge-multiple-excel-files-into-one-sheet-vba/ 4/22
8/2/22, 10:47 AM How to Merge Multiple Excel Files into One Sheet by VBA (3 Criteria)
Remember: The files you need to copy the information form (e.g. File1, File2, File3) need
to be open while running the macro. Because Excel will loop through the open files and
copy the information into a new sheet or workbook.
https://www.exceldemy.com/merge-multiple-excel-files-into-one-sheet-vba/ 5/22
8/2/22, 10:47 AM How to Merge Multiple Excel Files into One Sheet by VBA (3 Criteria)
This criterion will let you know how to merge File1, File2, and File3 into one sheet in a new
workbook in Excel.
Steps:
Press Alt + F11 on your keyboard or go to the tab Developer -> Visual Basic to open
Visual Basic Editor.
In the pop-up code window, from the menu bar, click Insert -> Module.
https://www.exceldemy.com/merge-multiple-excel-files-into-one-sheet-vba/ 6/22
8/2/22, 10:47 AM How to Merge Multiple Excel Files into One Sheet by VBA (3 Criteria)
In the code window, copy the following code and paste it.
Sub MergeMultipleSheetsToNew()
On Error GoTo eh
Dim wb As Workbook
Dim sh As Worksheet
Application.ScreenUpdating = False
'get the name of the new workbook so you exclude it from the loop below
strDestName = wbDestination.Name
'now loop through each of the workbooks open to get the data
Set wbSource = wb
sh.Activate
ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Activate
iRws = ActiveCell.Row
iCols = ActiveCell.Column
https://www.exceldemy.com/merge-multiple-excel-files-into-one-sheet-vba/ 7/22
8/2/22, 10:47 AM How to Merge Multiple Excel Files into One Sheet by VBA (3 Criteria)
wbDestination.Activate
wsDestination.Cells.SpecialCells(xlCellTypeLastCell).Select
totRws = ActiveCell.Row
End If
End If
Next wb
'now close all the open files except the one you want
wb.Close False
End If
Next wb
Set wb = Nothing
Application.ScreenUpdating = False
Exit Sub
eh:
MsgBox Err.Description
End Sub
https://www.exceldemy.com/merge-multiple-excel-files-into-one-sheet-vba/ 8/22
8/2/22, 10:47 AM How to Merge Multiple Excel Files into One Sheet by VBA (3 Criteria)
Press F5 on your keyboard or from the menu bar select Run -> Run Sub/UserForm. You
can also just click on the small Play icon in the sub-menu bar to run the macro.
You will get all the data from the multiple opened Excel files in one sheet of a new Excel
workbook (see the picture below to understand more).
https://www.exceldemy.com/merge-multiple-excel-files-into-one-sheet-vba/ 9/22
8/2/22, 10:47 AM How to Merge Multiple Excel Files into One Sheet by VBA (3 Criteria)
https://www.exceldemy.com/merge-multiple-excel-files-into-one-sheet-vba/ 10/22
8/2/22, 10:47 AM How to Merge Multiple Excel Files into One Sheet by VBA (3 Criteria)
This criterion will let you know how to combine File1, File2, and File3 into one sheet in an
active workbook in Excel.
Steps:
Same way as before, open Visual Basic Editor from the Developer tab and Insert a
Module in the code window.
In the code window, copy the following code and paste it.
Sub MergeMultipleSheetsToActive()
On Error GoTo eh
Dim wb As Workbook
Dim sh As Worksheet
Application.ScreenUpdating = False
Application.DisplayAlerts = False
https://www.exceldemy.com/merge-multiple-excel-files-into-one-sheet-vba/ 11/22
8/2/22, 10:47 AM How to Merge Multiple Excel Files into One Sheet by VBA (3 Criteria)
ActiveWorkbook.Sheets("Consolidation").Delete
On Error GoTo eh
Application.DisplayAlerts = True
With ActiveWorkbook
wsDestination.Name = "Consolidation"
End With
'now loop through each of the workbooks open to get the data
Set wbSource = wb
sh.Activate
ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Activa
iRws = ActiveCell.Row
iCols = ActiveCell.Column
wbDestination.Activate
wsDestination.Cells.SpecialCells(xlCellTypeLastCell).Sele
totRws = ActiveCell.Row
End If
'add a row to paste on the next row down if you are not in row 1
End If
Next wb
'now close all the open files except the one you want
https://www.exceldemy.com/merge-multiple-excel-files-into-one-sheet-vba/ 12/22
8/2/22, 10:47 AM How to Merge Multiple Excel Files into One Sheet by VBA (3 Criteria)
wb.Close False
End If
Next wb
Set wb = Nothing
Application.ScreenUpdating = False
Exit Sub
eh:
MsgBox Err.Description
End Sub
https://www.exceldemy.com/merge-multiple-excel-files-into-one-sheet-vba/ 13/22
8/2/22, 10:47 AM How to Merge Multiple Excel Files into One Sheet by VBA (3 Criteria)
https://www.exceldemy.com/merge-multiple-excel-files-into-one-sheet-vba/ 14/22
8/2/22, 10:47 AM How to Merge Multiple Excel Files into One Sheet by VBA (3 Criteria)
Run the code and you will get all the data from the multiple opened Excel files in one sheet
in an active Excel workbook (see the picture above to understand more).
This criterion will let you know how to combine File1, File2, and File3 into a new workbook
as individual sheets in Excel.
Steps:
Same way as before, open Visual Basic Editor from the Developer tab and Insert a
Module in the code window.
In the code window, copy the following code and paste it.
Sub MergeMultipleFiles()
On Error GoTo eh
Dim wb As Workbook
Dim sh As Worksheet
https://www.exceldemy.com/merge-multiple-excel-files-into-one-sheet-vba/ 15/22
8/2/22, 10:47 AM How to Merge Multiple Excel Files into One Sheet by VBA (3 Criteria)
Application.ScreenUpdating = False
'get the name of the new workbook so you exclude it from the loop below
strDestName = wbDestination.Name
'now loop through each of the workbooks open to get the data but exclude
For Each wb In Application.Workbooks
Set wbSource = wb
sh.Copy After:=Workbooks(strDestName).Sheets(1)
Next sh
End If
Next wb
'now close all the open files except the new file and the Personal macro
For Each wb In Application.Workbooks
wb.Close False
End If
Next wb
Application.DisplayAlerts = False
Sheets("Sheet1").Delete
Application.DisplayAlerts = True
Set wb = Nothing
Application.ScreenUpdating = False
Exit Sub
eh:
MsgBox Err.Description
End Sub
https://www.exceldemy.com/merge-multiple-excel-files-into-one-sheet-vba/ 16/22
8/2/22, 10:47 AM How to Merge Multiple Excel Files into One Sheet by VBA (3 Criteria)
Run the code and you will get all the opened Excel files in a new Excel workbook as
individual sheets (see the picture above to understand more).
https://www.exceldemy.com/merge-multiple-excel-files-into-one-sheet-vba/ 17/22
8/2/22, 10:47 AM How to Merge Multiple Excel Files into One Sheet by VBA (3 Criteria)
Conclusion
This article showed you how to merge multiple Excel files into one sheet and one workbook
in Excel by implementing the VBA macro. I hope this article has been very beneficial to
you. Feel free to ask any questions regarding the topic.
0
Save
Sanjida Ahmed
Hello World! This is Sanjida, an Engineer who is passionate about researching real-world
problems and inventing solutions that haven’t been discovered yet. Here, I try to deliver
the results with explanations of Excel-related problems, where most of my interpretations
will be provided to you in the form of Visual Basic for Applications (VBA) programming
language. Being a programmer and a constant solution seeker, made me interested in
assisting the world with top-notch innovations and evaluations of data analysis.
https://www.exceldemy.com/merge-multiple-excel-files-into-one-sheet-vba/ 18/22
8/2/22, 10:47 AM How to Merge Multiple Excel Files into One Sheet by VBA (3 Criteria)
Related Articles
LEAVE A REPLY
https://www.exceldemy.com/merge-multiple-excel-files-into-one-sheet-vba/ 19/22
8/2/22, 10:47 AM How to Merge Multiple Excel Files into One Sheet by VBA (3 Criteria)
Submit
Affiliate Disclosure
This post may contain affiliate links, meaning when you click the links and make a purchase, we may earn an
affiliate commission, but this never influences our opinion.
https://www.exceldemy.com/merge-multiple-excel-files-into-one-sheet-vba/ 20/22
8/2/22, 10:47 AM How to Merge Multiple Excel Files into One Sheet by VBA (3 Criteria)
About ExcelDemy.com
ExcelDemy is a place where you can learn Excel, Data Analysis, and other Office related programs. We provide
tips, how to guide and also provide Excel solutions to your business problems.
ExcelDemy.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program. I
earn a small commission if you buy any products using my affiliate links to Amazon.
https://www.exceldemy.com/merge-multiple-excel-files-into-one-sheet-vba/ 21/22
8/2/22, 10:47 AM How to Merge Multiple Excel Files into One Sheet by VBA (3 Criteria)
Categories
Select Category
Search in ExcelDemy.com
Search
https://www.exceldemy.com/merge-multiple-excel-files-into-one-sheet-vba/ 22/22