

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. The scenario above works pretty well for situations where we want to list explicitly files we want to merge into a single Excel Workbook. Pretty simple right? Merge Excel files within a directory MergeExcelFiles fileNames, "SomeWs", "test.xlsx" 'Merge only worksheets named "SomeWs" in listed files and save the merged file as "test.xlsx" 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.įileNames(0) = ThisWorkbook.Path & "\File1.xlsx"įileNames(1) = ThisWorkbook.Path & "\File2.xlsx" Set destWb = Nothing: Set excelApp = Nothing Ws.Copy After:=destWb.Sheets()ĭestWb.SaveAs ThisWorkbook.Path & "\" & mergedFileName If ws.Name = worksheetName Then ws.Copy After:=destWb.Sheets() Sub MergeExcelFiles(fileNames() As String, Optional worksheetName As String = vbNullString, Optional mergedFileName As String = "merged.xlsx")ĭim fileName As Variant, wb As Workbook, ws As Worksheet, destWb As Workbook, excelApp As Application Use the MergeExcelFiles Sub procedure below to merge any number of Workbooks:
#MICROSOFT EXCEL HOW TO MERGE WORKBOOKS CODE#
The code below supports both these scenarios. Copy only a single worksheet – with a specific name.I wanted however to account for 2 typical scenarios: What we want to do is create a new Excel Workbook and copy Worksheets to this new Workbook. Let us assume we have a couple of files listed in our directory (in my example these are File1 and File2.xlsx). Repeat Step 2 for each Workbook you want to copy to the destination Workbook. If you want to copy (copy & paste) the Worksheet – select the Create a Copy checkbox and click the OK button.If you want to move (cut & paste) the Worksheet – simply click the OK button to proceed.In the Move or Copy Window select the destination Workbook (Destination.xlsx in our case). Next right-click on each Worksheet you want to copy, click Move or Copy. Open each Excel Workbook you want to merge with the destination Workbook. Open (each) the Workbook you want to merge and copy Worksheets The further steps need to be repeated for each Excel Workbook you want to copy to 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). Merge Excel files manually Open the destination Workbook Want to merge Worksheets or CSV files instead?:
