13
OA Ultimate About Us Privacy Policy Disclaimer Home Computers Hardware Internet Networking Office Search Home Office Merge multiple excel files into a single spreadsheet (MS Excel 2007) Last updated on November 11, 2013 @ M Riza 21 Comments Recently I’ve got something to work on with several excel files. More than 150 excel files generated by a web based application have to be merged into one file and then create a summary from it. Usually i did this by doing copy paste all values one at a time into a new empty spreadsheet, or copy to merge sheets to another excel files one by one. But yesterday my friend show me a simple way to combine or merge multiple excel files with macros inside Excel 2007 (edit macros with MS Visual Basic editor and no download required). Although I’m not familiar with office macros, but i can use it easily by just write a simple xls file merger code on vb editor, change the working folder path and cell starter reference name inside the code to suit your reference, and then click RunSub. All excel (xls or xlsx) files inside working folder will be merged into current worksheet. Merge multiple excel files into a single spreadsheet (MS Excel 2007) http://www.oaultimate.com/office/merge-multiple-excel-files-into-a-single-spreadsheet-ms-exce... 1 of 13 18/10/2014 03:43 PM

Merge Multiple Excel Files Into a Single Spreadsheet (MS Excel 2007)1

  • Upload
    keen

  • View
    88

  • Download
    5

Embed Size (px)

DESCRIPTION

Merge Multiple Excel Files Into a Single Spreadsheet (MS Excel 2007)1

Citation preview

Page 1: Merge Multiple Excel Files Into a Single Spreadsheet (MS Excel 2007)1

OA Ultimate ›About UsPrivacy PolicyDisclaimer

HomeComputersHardwareInternetNetworkingOffice

Search

HomeOfficeMerge multiple excel files into a single spreadsheet (MS Excel 2007)

Last updated on November 11, 2013 @ M Riza → 21 Comments

Recently I’ve got something to work on with several excel files. More than 150 excel files generated by a web based application have to be merged into one file and then create a summary from it.Usually i did this by doing copy paste all values one at a time into a new empty spreadsheet, or copy to merge sheets to another excel files one by one. But yesterday my friend show me a simple wayto combine or merge multiple excel files with macros inside Excel 2007 (edit macros with MS Visual Basic editor and no download required).

Although I’m not familiar with office macros, but i can use it easily by just write a simple xls file merger code on vb editor, change the working folder path and cell starter reference name inside thecode to suit your reference, and then click RunSub. All excel (xls or xlsx) files inside working folder will be merged into current worksheet.

Merge multiple excel files into a single spreadsheet (MS Excel 2007) http://www.oaultimate.com/office/merge-multiple-excel-files-into-a-single-spreadsheet-ms-exce...

1 of 13 18/10/2014 03:43 PM

Page 2: Merge Multiple Excel Files Into a Single Spreadsheet (MS Excel 2007)1

Watch it on YouTube

For more detail, here’s what i did to merge multiple excel files with MS Excel 2007.

Gather all xls or xlsx files that you wanted to merge into a folder. Remember that this merger macros will only grab the first worksheet on spreadsheet files. So make sure that all contents is onthe first worksheet before continue.

Merge multiple excel files into a single spreadsheet (MS Excel 2007) http://www.oaultimate.com/office/merge-multiple-excel-files-into-a-single-spreadsheet-ms-exce...

2 of 13 18/10/2014 03:43 PM

Page 3: Merge Multiple Excel Files Into a Single Spreadsheet (MS Excel 2007)1

Close all working excel files so you can focus only on merging files.On MS Excel, create new spreadsheet by simply pressing CTRL+N.And open Microsoft Visual Basic editor by pressing ALT+F11, you’ll see a blank text editor.Now open by doubleclicking ThisWorkBook on the left sheet menu.Paste the following macros code:

12345678910111213141516171819202122232425

Sub simpleXlsMerger()Dim bookList As WorkbookDim mergeObj As Object, dirObj As Object, filesObj As Object, everyObj As ObjectApplication.ScreenUpdating = FalseSet mergeObj = CreateObject("Scripting.FileSystemObject") 'change folder path of excel files hereSet dirObj = mergeObj.Getfolder("D:\change\to\excel\files\path\here")Set filesObj = dirObj.FilesFor Each everyObj In filesObjSet bookList = Workbooks.Open(everyObj) 'change "A2" with cell reference of start point for every files here'for example "B3:IV" to merge all files start from columns B and rows 3 'If you're files using more than IV column, change it to the latest column'Also change "A" column on "A65536" to the same column as start pointRange("A2:IV" & Range("A65536").End(xlUp).Row).CopyThisWorkbook.Worksheets(1).Activate 'Do not change the following column. It's not the same column as aboveRange("A65536").End(xlUp).Offset(1, 0).PasteSpecialApplication.CutCopyMode = FalsebookList.CloseNextEnd Sub

Change the folder as mentioned on comment on the macros codeChange also column start reference to suit your need (usually first row used by column header, so i used A2 as start point).For example to start merging all files from column “B” row “1”.Change “IV” only if you have files using column wider than column “IV”. Basically, it will try to copy values on all available columns. If you notice the latest column on new worksheet is “IV”,it is the default available column on until your columns growth more than that.

Range("B1:IV" & Range("B65536").End(xlUp).Row).Copy

Merge multiple excel files into a single spreadsheet (MS Excel 2007) http://www.oaultimate.com/office/merge-multiple-excel-files-into-a-single-spreadsheet-ms-exce...

3 of 13 18/10/2014 03:43 PM

Page 4: Merge Multiple Excel Files Into a Single Spreadsheet (MS Excel 2007)1

If everything configured already, press “F5″ or click on play icon to run the code (RunSub). You’ll see working progress on left sheet menu.If all done, you can now switch to worksheet to see the result.

If you set a new folder within the code, and then hit “F5″ or press SubRun button, the result will be added into current worksheet bellow the previous data. That’s mean it will not overwritten the lastresult but as another merge to previous merged data. In conclusion, you can spam change folder path and hit F5 to run the code in order to merge all files on provided folder into current worksheet. Ifyou want to start new merge for multiple xls or xlsx files you have to clear the current worksheet, or create new file for merge. I hope there’s also a way to merge spreadsheet similar to this but forLibreOffice, since i also work on several ods files.

Update

Read also merge excel with simple merger tool for small files — merge without macros.

Article by M Riza

Is a blogger since 2007, founder and editor of Oa Ultimate. Working as a network administrator, computer technician, database and web maintainer | Twitter | G+

Merge multiple excel files into a single spreadsheet (MS Excel 2007) http://www.oaultimate.com/office/merge-multiple-excel-files-into-a-single-spreadsheet-ms-exce...

4 of 13 18/10/2014 03:43 PM

Page 5: Merge Multiple Excel Files Into a Single Spreadsheet (MS Excel 2007)1

Related PostsThis CategoryMore Articles..

Find duplicates in excel and LibreOffice CalcUnhide windows files and folders hidden by virus without attribSimple excel calculate date with excel formSeveral things about MS Excel 2007 you might want to knowVlookup, Microsoft Excel and LibreOffice CalcConverting excel time format, 24 to 12 hours vise versa2 ways to show and recover hidden files by virus

Advertisement

Oa BlogGoogle+FacebookWrite a comment..

21 Comments → “Merge multiple excel files into a single spreadsheet (MS Excel 2007)”

Emmi · Thursday, Jun 13, 2013 at 9:54 AM · Reply

What if the sheets I want to consolidate is in Sheet3 with Sheet Name “FT DEBITS”? Is it possible that this sheet will be the one to be merged instead of the first sheet?

M Riza · Thursday, Jun 13, 2013 at 1:40 PM · Reply

Hi Emmi,For example you want to merge Sheet3, set active Sheet 3 using the following code:

bookList.Worksheets(3).Activate

1.

Merge multiple excel files into a single spreadsheet (MS Excel 2007) http://www.oaultimate.com/office/merge-multiple-excel-files-into-a-single-spreadsheet-ms-exce...

5 of 13 18/10/2014 03:43 PM

Page 6: Merge Multiple Excel Files Into a Single Spreadsheet (MS Excel 2007)1

Or if you want to use sheet name instead:

bookList.Worksheets("FT DEBITS").Activate

Put after/bellow the following code (line 11).

1112

Set bookList = Workbooks.Open(everyObj)bookList.Worksheets("FT DEBITS").Activate

Emmi · Friday, Jun 14, 2013 at 2:52 AM · Reply

Hi M Riza,Thank you very much for this. I’m now able to generate the report I exactly wanted without spending so much time. Your article is really a big help. Thank you.

M Riza · Friday, Jun 14, 2013 at 3:37 AM · Reply

You’re welcome.I am glad to be of help, have a nice day.

Sreedhar · Tuesday, Jun 25, 2013 at 8:17 AM · Reply

Its saved my lot of time…Thank you very much for posting this article….!

M Riza · Tuesday, Jun 25, 2013 at 2:19 PM · Reply

Hi, You’re welcome, i’m happy to have been of help. Thanks also for visiting my blog.

2.

Chinh · Monday, Jul 22, 2013 at 7:51 AM · Reply

What if I want to copy one column from one workbook to another and place them next to each other, as opposed to on top of each other. For example, comlumn B in workbook 1 to column C ofthe new workbook, column B of workbook 2 to column D of the new workbook, column B of workbook 3 to Column E of the new workbook, and so on.

M Riza · Tuesday, Aug 13, 2013 at 1:37 AM · Reply

Hi, I’m not sure if this is what you mean, but try this

3.

Merge multiple excel files into a single spreadsheet (MS Excel 2007) http://www.oaultimate.com/office/merge-multiple-excel-files-into-a-single-spreadsheet-ms-exce...

6 of 13 18/10/2014 03:43 PM

Page 7: Merge Multiple Excel Files Into a Single Spreadsheet (MS Excel 2007)1

Set a new variable for number of columns you have on sources files.Write the following lines before loop

Dim ii = 0

And then change

Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial

into

Range("A1").Offset(0, i).PasteSpecial

increase i value with number of columns you have on source xls files (for example you have 9 columns on sources files)

i = i + 9

So the script will be:

...

...Dim ii = 0For Each everyObj In filesObj......Range("A1").Offset(0, i).PasteSpeciali = i + 9Application.CutCopyMode = False......

dacaprice · Friday, Aug 02, 2013 at 3:19 PM · Reply

Nice work! What if I would like a new sheet for every file I import?

M Riza · Tuesday, Aug 13, 2013 at 2:08 AM · Reply

Hi, i see you’re familiar with coding so, please try this:Add new variable as Object before loop (for example WSA)

Dim WSA as Object

4.

Merge multiple excel files into a single spreadsheet (MS Excel 2007) http://www.oaultimate.com/office/merge-multiple-excel-files-into-a-single-spreadsheet-ms-exce...

7 of 13 18/10/2014 03:43 PM

Page 8: Merge Multiple Excel Files Into a Single Spreadsheet (MS Excel 2007)1

On the loop set WSA to add new sheet and then set activate

Set WSA = ThisWorkbook.Worksheets.AddWSA.Activate

kyle · Wednesday, Sep 04, 2013 at 9:46 PM · Reply

i am unable to get this to work could you be more specific?

Isa · Tuesday, Aug 06, 2013 at 12:11 PM · Reply

Hi, I tried using this code to merge 50 files (all same format) into a sheet- the macro is working through all the files, I can see that, but in the end there is no data- can you help? where am I goingwrong? Many thanks

M Riza · Tuesday, Aug 13, 2013 at 2:17 AM · Reply

Hi, please make sure you set the right range for source and destination cells on the macros. Read on comments lines for more detail.

5.

Chris · Thursday, Aug 15, 2013 at 4:13 PM · Reply

Script is awesome, thank you. However, it gets through about 20 of the 111 files and then gives me a “1004” Runtime Error. Any ideas why?

Thanks!

M Riza · Thursday, Aug 15, 2013 at 11:40 PM · Reply

Hi, is there any message shown after runtime error message? for example :

Run-time error '1004'. Application-defined or operation-defined error.

According to Microsoft help forum, the above message occur when you have cell that contain more than 911 characters in one of your source files.More info : http://goo.gl/aQJnTn

6.

Lal · Thursday, Aug 22, 2013 at 5:46 AM · Reply

What if I have to combine multiple worksheets in multiple workbooks to one sheet,and the number of columns is not uniform in all the worksheets?

7.

Merge multiple excel files into a single spreadsheet (MS Excel 2007) http://www.oaultimate.com/office/merge-multiple-excel-files-into-a-single-spreadsheet-ms-exce...

8 of 13 18/10/2014 03:43 PM

Page 9: Merge Multiple Excel Files Into a Single Spreadsheet (MS Excel 2007)1

I have 12 workbooks with 60/62 worksheets each. The last column is AI/AJ/AL in different worksheets.

Nagaraju. Gampa · Sunday, Sep 01, 2013 at 2:42 PM · Reply

Hi,

using this website i have collatted different excel files into to new file.

finally i got the result.

Thank you very much for this.our article is really a big help for me

. Thank you. Nagaraju G

8.

swsisack · Wednesday, Sep 11, 2013 at 12:19 AM · Reply

M Riza, could you post the full code that dacaprice is asking for, I do not understand where to past those two new lines of code. I too want to combine seperate sheets into a single file, whereeach file comes in as a new tab at the bottom. Thanks in advance

M Riza · Wednesday, Sep 11, 2013 at 3:44 AM · Reply

Hi,Insert

Dim WSA As Object

Just bellow this line:

Dim mergeObj As Object, dirObj As Object, filesObj....

And then insert the following 2 lines

Set WSA = ThisWorkbook.Worksheets.AddWSA.Activate

just bellow this line:

For Each everyObj In filesObj

9.

Merge multiple excel files into a single spreadsheet (MS Excel 2007) http://www.oaultimate.com/office/merge-multiple-excel-files-into-a-single-spreadsheet-ms-exce...

9 of 13 18/10/2014 03:43 PM

Page 10: Merge Multiple Excel Files Into a Single Spreadsheet (MS Excel 2007)1

The code will be:

...

...Dim mergeObj As Object, dirObj As Object, filesObj...Dim WSA As ObjectApplication.ScreenUpdating = False......For Each everyObj In filesObjSet WSA = ThisWorkbook.Worksheets.AddWSA.ActivateSet bookList = Workbooks.Open(everyObj)......

faiz · Tuesday, Dec 03, 2013 at 7:02 AM · Reply

Hi RizaMany Thanks for this code.Can You please advise how can I add codes for making the rows height = 18 & coulmn width to autoselect.Thanks

M Riza · Tuesday, Dec 03, 2013 at 8:10 AM · Reply

Hi, try add the following code just before the last line (End Sub)

Range([A1], [A65536].End(xlUp)).EntireRow.RowHeight = 18Cells.EntireColumn.AutoFit

10.

Leave a Reply

Name *

Mail *

Website

Merge multiple excel files into a single spreadsheet (MS Excel 2007) http://www.oaultimate.com/office/merge-multiple-excel-files-into-a-single-spreadsheet-ms-exce...

10 of 13 18/10/2014 03:43 PM

Page 11: Merge Multiple Excel Files Into a Single Spreadsheet (MS Excel 2007)1

CAPTCHA Code*

Subcribe

Popular Posts

Install WhatsApp on Windows 7 [Aug 26, 2012]

Step-by-step Install and Configure Stereoscopic 3D media player with MPC [Aug 25, 2012]

Playing 3D SBS Videos as 3D Anaglyph with Media Player Classic [Dec 24, 2011]

Photo sharing Android, Bluestacks and PC for WhatsApp [Dec 14, 2012]

Change WhatsApp profile picture with image from DropBox folder [Dec 27, 2012]

Merge multiple excel files into a single spreadsheet (MS Excel 2007) http://www.oaultimate.com/office/merge-multiple-excel-files-into-a-single-spreadsheet-ms-exce...

11 of 13 18/10/2014 03:43 PM

Page 12: Merge Multiple Excel Files Into a Single Spreadsheet (MS Excel 2007)1

© 2014 OA Ultimate.

Tweet

46

Merge multiple excel files into a single spreadsheet (MS Excel 2007) [Mar 18, 2013]

Install PhotoScape 3.6 on Ubuntu 11.10 [Feb 05, 2012]

Recent Posts

Edit PDF for Free with LibreOffice [Feb 14, 2014]

Problems install BBM on Windows PC [Nov 23, 2013]

Simple Excel merger (single worksheet and small files only) [Nov 05, 2013]

Computer slow boot and disable bluestacks startup [Jun 28, 2013]

Lazarus FreePascal, make.exe idepkg Error 255 [Jun 24, 2013]

5 easy ways send picture to WhatsApp chat on Windows PC [Jun 13, 2013]

Download Apps from Play Store in BlueStacks [Jun 06, 2013]

Protect WhatsApp on your Windows PC with Password [Jun 02, 2013]

Internet safety tips: how to safe on the internet [Jun 02, 2013]

Create pdf files from MS Word in 2 ways [Jun 01, 2013]

Merge multiple excel files into a single spreadsheet (MS Excel 2007) http://www.oaultimate.com/office/merge-multiple-excel-files-into-a-single-spreadsheet-ms-exce...

12 of 13 18/10/2014 03:43 PM

Page 13: Merge Multiple Excel Files Into a Single Spreadsheet (MS Excel 2007)1

10

31

LikeLike

Merge multiple excel files into a single spreadsheet (MS Excel 2007) http://www.oaultimate.com/office/merge-multiple-excel-files-into-a-single-spreadsheet-ms-exce...

13 of 13 18/10/2014 03:43 PM