- To combine multiple Excel workbooks, you need to use a code that can either let you choose the files that you want to combine or you can refer to a particular path location and combine all Excel files there in the current workbook. VBA Combine Multiple Workbooks.
- Merge Excel files code Use the MergeExcelFiles Sub procedurebelow to merge any number of Workbooks: Sub MergeExcelFiles(fileNames As String, Optional worksheetName As String = vbNullString, Optional mergedFileName As String = 'merged.xlsx') Dim fileName As Variant, wb As Workbook, ws As Worksheet, destWb As Workbook, excelApp As Application.
Easily combine multiple workbooks or specified sheets of workbooks to one workbook. Create a new workbook and click Kutools Plus Combine. Then a dialog pops to remind you that all combined workbooks should be saved and the. In the Combine Worksheets wizard, select Combine multiple.
Having your data spread across multiple Excel files? Want to merge Excel files into a single Excel Workbook? Today we going to explore just that – how to merge multiple Excel Workbook spreadsheets into a single Workbook. The obvious tool for this task is Visual Basic for Applications so let us jump right in.
Merge Excel files manually
Combine Multiple Excel Files Into 1
Open the destination Workbook
Open the destination Excel Workbook (in our example Destination.xlsx) to which you want to copy the Worksheets of the remaining Workbooks (in our example Source.xlsx).
The further steps need to be repeated for each Excel Workbook you want to copy to the destination Workbook.
Open (each) the Workbook you want to merge and copy Worksheets
Open each Excel Workbook you want to merge with the destination Workbook.
Next right-click on each Worksheet you want to copy, click Move or Copy.... In the Move or Copy Window select the destination Workbook (Destination.xlsx in our case). Next:
- If you want to move (cut & paste) the Worksheet – simply click the OK button to proceed
- If you want to copy (copy & paste) the Worksheet – select the Create a Copy checkbox and click the OK button
Repeat Step 2 for each Workbook you want to copy to the destination Workbook.
Merge Excel files using VBA
Let us assume we have a couple of files listed in our directory (in my example these are File1 and File2.xlsx). What we want to do is create a new Excel Workbook and copy Worksheets to this new Workbook. I wanted however to account for 2 typical scenarios:
- Copy ALL worksheets
- Copy only a single worksheet – with a specific name
The code below supports both these scenarios.
Merge Excel files code
Use the MergeExcelFiles Sub procedure below to merge any number of Workbooks:
How to use the procedure above? Below I create a simple Test procedure that lists the Excel files within the Workbook directory and merges the Workbooks.
Pretty simple right?
Combine Multiple Excel Files Into 1
Merge Excel files within a directory
The scenario above works pretty well for situations where we want to list explicitly files we want to merge into a single Excel Workbook. How about when we have tons of files? Or to make it more simple, if we want to merge all files within a SINGLE directory. As this is also a typical scenario I have modified the above Test procedure to accommodate just that:
What happens is that we use the VBA Dir function to loop through all files within a selected directory. As we go along we modify our VBA Array redefining its size and adding additional items.
Download the code
Want to download the code above and the example?
Merge Excel files in a whole directory structure
One last scenario that came to mind is when we have a whole directory structure (directories within other directories) containing files with want to merge. As the VBA Dir function works only for a single directory and does not traverse any directories within we would need to amend additionally the code above. If needed I recommend reading my post on How to traverse directories using the VBA Dir function.