How to COMBINE Multiple Excel WORKBOOKS into One Workbook

How to COMBINE Multiple Excel WORKBOOKS into One Workbook

Combine Multiple Excel Workbooks into One

Combine multiple excel workbooks into one workbook

We might need to combine multiple excel workbooks into one workbook in order to analyse or present the information better. Though this can be done manually, by moving or copying all the worksheets from different workbooks into one workbook, but it would be time consuming and prone to errors. Moreover if there are a large number of workbooks/worksheets, then the problem is even bigger.

However, we have a solution to this and it would not take more than a few seconds to get all workbooks merged into one. Here is the VBA code that can help us achieve this. Even those who are not well versed with VBA can easily get this done by simply following the steps on how to use this code:

VBA CODE

Sub GetSheets()

'Update ExcelJunction.com

Path = ""

Filename = Dir (Path & "*.xls")

Do While Filename <> ""

Workbooks.Open Filename:= Path & Filename, ReadOnly:=True

For Each Sheet In ActiveWorkbook.Sheets

Sheet.Copy After:=ThisWorkbook.Sheets(1)

Next Sheet

Workbooks(Filename).Close

Filename = Dir()

Loop

End Sub

How to Use this Code?

Here are the steps to use this code:

  • All the Excel workbooks that you want to combine should be placed in a folder. For the purpose of this tutorial, we have created a folder named “Excel Workbooks” and have three files in it.
  • Open a new Excel workbook.
  • Press ALT + F11 (or go to Developer –> Visual Basic). This will open the Visual Basic Editor.
  • In the VB Editor menu go to Insert –> Module. This will insert a module for the workbook with a blank code window.
  • Copy and paste the above code into the code window.
  • In the code, you need to change the following line of code:
          Path = ""
  • In this line, insert the location of the folder in which you have placed the files that you want to combine, between quotes. In the code used above, the folder is in “Documents”. Get the path by visiting the folder and clicking on the address bar dropdown. We insert C:\Users\Nitesh\Documents\Excel Workbooks\ between the quotes. (Note that a back slash has been added at the end of the folder path)
  • Place the cursor anywhere in the code and click on the green play button in the Toolbar options (or press the F5 key).
  • This will activate the code and all worksheets from all the Excel workbooks in the folder would get consolidated into a single workbook.
  • Save the Master Workbook, and you’re done.

 

How does this Code Work?

The code uses the DIR function to get the file names from the specified folder.

The following line assigns the first excel file name to the variable ‘Filename’.

Filename = Dir(Path & “*.xls*”)

Then the Do While loop is used to check whether all the files have been covered.

Within the Do While loop, For Each loop is used to copy all the worksheets to the workbook in which we are running the code.

At the end of the Do Loop, following line of code is used: Filename = Dir(). It assigns the next Excel file name to the Filename variable and the loop starts again.

When all the files are covered, DIR function returns an empty string, which is when the loop ends.

Here is an explanation of the DIR function in the MSDN library:

Dir returns the first file name that matches pathname. To get any additional file names that match pathname, call Dir again with no arguments. When no more file names match, Dir returns a zero-length string (“”).

 

We hope you found this interesting. Your comments/feedback would be appreciated.

 

 
Comments

No comments yet.

Leave a Reply

You must be logged in to post a comment.