Link to home
Start Free TrialLog in
Avatar of kbay808
kbay808Flag for United States of America

asked on

Merge multiple excel workbooks into 1

I’m using the below script to merge multiple excel workbooks into 1 using Excel 2010.  It works great, but I need it to merge more than 65536 rows.   I tried changing “A65536” to “A1000000” and “A1500000”, but I receive the following error.  “Run-time error ‘1004’: Application-defined or object-defined error”  How can I fix this scrip to increase to the maximum number of rows allowed?

Sub simpleXlsMerger()
Dim bookList As Workbook
Dim mergeObj As Object, dirObj As Object, filesObj As Object, everyObj As Object
Application.ScreenUpdating = False
Set mergeObj = CreateObject("Scripting.FileSystemObject")
 

Set dirObj = mergeObj.Getfolder("D:\Merge Files")
Set filesObj = dirObj.Files
For Each everyObj In filesObj
Set bookList = Workbooks.Open(everyObj)
 
Range("A2:IV" & Range("A65536").End(xlUp).Row).Copy
ThisWorkbook.Worksheets(1).Activate
 
Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial
Application.CutCopyMode = False
bookList.Close
Next
End Sub
Avatar of etech0
etech0
Flag of United States of America image

Have you double checked that all the files involved are .xlsx and not .xls?
.xls files can only have up to 65536 rows.
Avatar of kbay808

ASKER

All of the files that I want to merge are .xls files, but I’m merging them into a .xlsx file.
If the source files are .xls, they can't have more than 65536 rows. What do you mean by "I need it to merge more than 65536 rows"?
Avatar of kbay808

ASKER

Sorry, I had a brain fart.  I was thinking that the “A65536” was the maximum amount of rows in the output file.  I should have noticed the amount of rows that were actually merged.  When I run the scrip, I don’t have all of the data.  I should have 169,176 rows, but only 117,351 rows were merged.
Aha!
Try changing this line
Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial

Open in new window

to
Range("A2").End(xlDown).Offset(1, 0).PasteSpecial

Open in new window


If that fixes it I can explain it to you.
Avatar of kbay808

ASKER

I'm receiving the same error.
Run-time error ‘1004’: Application-defined or object-defined error”
Hmmm. Can you post example files that give the error?
ASKER CERTIFIED SOLUTION
Avatar of Rob Brockett
Rob Brockett
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial