Tuesday 14 February 2017

Excel VBA: How to Merge Multiple Workbooks to One from a Folder

Ever Wonder to Copy all the Workbooks into one Workbook.
In this post i will share the script to copy all the excel files from one folder to Single Excel File.

Below is the Script

Sub MergeMultipleWorkbooks()

'Define Variables
Dim Path, FileName As String

'Assign Values to Variables
Path = Assign a Folder  which contains excel files for example "C:\Merge\"
FileName = Dir(Path & "*.xlsx")

'Check FileName in the Given Location
Do While FileName <> ""

'Open Excel File
Workbooks.Open FileName:=Path & FileName, ReadOnly:=True

'Copy all the sheet to this workbook
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet

'Close the ActiveWorkbook
Workbooks(FileName).Close
'Assign a Excel FileName

'Assign Next Excel FileName
FileName = Dir()
Loop

'Display a Message
MsgBox "Files has been copied Successfull", , "MergeMultipleExcelFiles"
End Sub

See it in Action.

4 comments:

  1. Hello,
    i did copy the script into Module and modified the Path information as explained above, though none of the documents being copied into the workbook and i just get the pop up : Files has been copied successfully. Please advise.
    thank you

    ReplyDelete
  2. Same thing happened to me, but it turned out I needed to add the end backslash (\) to my path folder.

    ReplyDelete
  3. Hi, tried your script but keep getting an error message saying Sub or Function not defined. Please help.

    ReplyDelete