4
How to Combine Multiple Workbooks into One Excel Workbook I got a call from a friend who wanted to combine multiple workbooks into one workbook. He had a lot of workbooks in a folder and he wanted to get all the worksheets from all the workbooks into one single workbook. While this can be done manually, it would be time-consuming and error-prone. However, a simple VB code can do this in a few seconds. Combine Multiple Workbooks into One Excel Workbook Here is the code that can combine all the workbooks in a specified folder into a single !"cel workbook# Sub ConslidateWorkbooks() 'Created by Sumit Bansal from http://trumpexcel.com im !older"ath #s Strin$ im !ilename #s Strin$ im Sheet #s Worksheet #pplication.Screen%pdatin$ & !alse !older"ath & niron(userprofile) * +esktop+,est+ !ilename & ir(!older"ath * -.xls-) o While !ilename  Workbooks.0pen !ilename:&!older"ath * !ilename1 2ead0nly:&,rue  !or ach Sheet 3n #ctieWorkbook.Sheets  Sheet.Copy #fter:&,hisWorkbook.Sheets(4)  5ext Sheet  Workbooks(!ilename).Close  !ilename & ir() 6oop #pplication.Screen%pdatin$ & ,rue nd Sub

How to Combine Multiple Workbooks Into One Excel Workbook

Embed Size (px)

Citation preview

Page 1: How to Combine Multiple Workbooks Into One Excel Workbook

8/16/2019 How to Combine Multiple Workbooks Into One Excel Workbook

http://slidepdf.com/reader/full/how-to-combine-multiple-workbooks-into-one-excel-workbook 1/4

How to Combine Multiple Workbooks into One

Excel Workbook

I got a call from a friend who wanted to combine multiple workbooks into one workbook. He had a lot of

workbooks in a folder and he wanted to get all the worksheets from all the workbooks into one singleworkbook. While this can be done manually, it would be time-consuming and error-prone.

However, a simple VB code can do this in a few seconds.

Combine Multiple Workbooks into One Excel Workbook 

Here is the code that can combine all the workbooks in a specified folder into a single !"cel workbook#

Sub ConslidateWorkbooks()

'Created by Sumit Bansal from http://trumpexcel.com

im !older"ath #s Strin$

im !ilename #s Strin$

im Sheet #s Worksheet

#pplication.Screen%pdatin$ & !alse

!older"ath & niron(userprofile) * +esktop+,est+

!ilename & ir(!older"ath * -.xls-)o While !ilename

 Workbooks.0pen !ilename:&!older"ath * !ilename1 2ead0nly:&,rue

 !or ach Sheet 3n #ctieWorkbook.Sheets

 Sheet.Copy #fter:&,hisWorkbook.Sheets(4)

 5ext Sheet

 Workbooks(!ilename).Close

 !ilename & ir()

6oop

#pplication.Screen%pdatin$ & ,rue

nd Sub

Page 2: How to Combine Multiple Workbooks Into One Excel Workbook

8/16/2019 How to Combine Multiple Workbooks Into One Excel Workbook

http://slidepdf.com/reader/full/how-to-combine-multiple-workbooks-into-one-excel-workbook 2/4

How to Use this Code?

Here are the steps to use this code#

• $ut all the !"cel files that you want to combine into a folder. %or the purpose of this tutorial, I

have created a folder named &est and have si" files in it '( !"cel workbooks and ) $ower $oint

and Word each*.

• +pen a new !"cel workbook.

• $ress & %)) 'or go to eveloper /0 1ode /0 Visual Basic*. &his will open the Visual Basic

!ditor.

• In the VB !ditor, in the $ro2ect !ditor, right-click on any of the ob2ects for the workbook and go to

Insert /0 3odule. &his will insert a module for the workbook.

• ouble click on the module. It will open the code window on the right.

Page 3: How to Combine Multiple Workbooks Into One Excel Workbook

8/16/2019 How to Combine Multiple Workbooks Into One Excel Workbook

http://slidepdf.com/reader/full/how-to-combine-multiple-workbooks-into-one-excel-workbook 3/4

• 1opy and paste the above code into the code window.

• In the code, you need to change the following line of code#

!older"ath & niron(userprofile) * \Desktop\Test\

In this line, change the part in double 4uotes 'highlighted in orange* with the location of the folder in which you have the files that you want to combine. In the code used above, the folder is on the

esktop. In case you have it in some other location, specify that path here.

• $lace the cursor anywhere in the code and click on the green play button in the &oolbar options 'or 

 press the %5 key*.

&his will run the code and all the worksheets from all the !"cel files in the folder would get consolidated

into a single workbook.

Page 4: How to Combine Multiple Workbooks Into One Excel Workbook

8/16/2019 How to Combine Multiple Workbooks Into One Excel Workbook

http://slidepdf.com/reader/full/how-to-combine-multiple-workbooks-into-one-excel-workbook 4/4

How this Code Works?

• &he code uses the I6 function to get the file names from the specified folder.

• &he following line assigns the first e"cel file name to the variable 7%ilename8.

%ilename 9 ir'%older$ath : ;<."ls<=*

• &hen the o While loop is used to check whether all the files have been covered.

• Within the o While loop, %or !ach loop is used to copy all the worksheets to the workbook

in which we are running the code.

• t the end of the o oop, following line of code is used# %ilename 9 ir'*. It assigns the ne"t

!"cel file name to the %ilename variable and the loop starts again.

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

Here is an e"planation of the I6 function in the 3>? 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 (“”).