SIGforum.com    Main Page  Hop To Forum Categories  The Lounge    Excel macro that combines multiple files into one sheet
Go
New
Find
Notify
Tools
Reply
  
Excel macro that combines multiple files into one sheet Login/Join 
Eating elephants
one bite at a time
Picture of ffips
posted
It might be worth reading from the bottom to the top in this thread as the solution was found...


In the past, I scrounged a macro from the internet that took multiple workbooks (single sheet) and combined them into one workbook. The process was basically to place all workbooks into a folder and then run the macro. The end result was a new workbook with all the data in one place.

I am trying to do this again with the same macro, but something is broken. It is only grabbing two workbooks(first two in folder) and combining them. It also errors out at the end.

As I am not a macro guru, is there any chance anyone would like to take a crack at it?

Here is the macro:
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")

'change folder path of excel files here
Set dirObj = mergeObj.Getfolder("C:\path")
Set filesObj = dirObj.Files
For Each everyObj In filesObj
Set 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 point
Range("A3:IV" & Range("A65536").End(xlUp).Row).Copy
ThisWorkbook.Worksheets(1).Activate

'Do not change the following column. It's not the same column as above
Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial
Application.CutCopyMode = False
bookList.Close
Next
End Sub



Thanks in advance. Smile

This message has been edited. Last edited by: ffips,
 
Posts: 3586 | Location: in the southwest Atlanta metro area | Registered: September 10, 2006Reply With QuoteReport This Post
Eating elephants
one bite at a time
Picture of ffips
posted Hide Post
Please stand by, I might have found the issue.

I sort of found the issue. It is a data standardization issue on my side. I found some sheets with a gap of about 400 rows that were empty. I am sorting through that.

At this time, it has pulled data into one location, but is throwing an error at the end:

Run-Time error '1004':
Application-defined or object-defined error

I have no clue what that means.

I also thought it used to work by opening a blank spreadsheet and running the macro. At this time, I am opening the macro and it is saving the spreadsheet as the macro at the end of the routine?
 
Posts: 3586 | Location: in the southwest Atlanta metro area | Registered: September 10, 2006Reply With QuoteReport This Post
His Royal Hiney
Picture of Rey HRH
posted Hide Post
I haven't used "Scripting.FileSystemObject" before so I can't help at his time.

I also don't understand how you "open a blank spreadsheet and run the macro" as you have to open the spreadsheet with the macro and then run the macro. I also don't understand "saving the spreadsheet as the macro at the end of the routine." Are you saying it tries to save the created file as simpleXIsMerger.xls?

One thing I see is the reference "A65536" cell. this is the old limitation of 65536. If the files you are trying to copy have more than 65536 rows of data, this would cause an issue and it's saying start from row 65536 and find the first row above it with data which would be row 1.

I'd offer to help more but I'm in the middle of working on my retirement macro and it's been slow going and I think your need is time sensitive.

I know you described your problem in your OP. I'd like to ask a question a different way and maybe I can help you. And I'm asking you the question because I have a library of macros that I created and maybe I can put a couple together for you. So here is my question:
"What is it that you want to accomplish? What does it look like when you have succeeded in what you are trying to accomplish?"

You start with several Excel files with single sheets and you want those sheets copied into a single workbook? Is that all that you want to accomplish?

The closest experience I have is I have the user open several excel files and then the macro searches for specific file names and copy data from them into a workbook.

I'll wait for your answer and in the meantime, i'll look up how to copy whole sheets.

Are you overwriting old data?
Are you appending data to existing data?

You might want to try those excel vba forums. I've done that in the past. Sorry, i'm not much help at this time.

ETA: one other thing is you could open the macro and step through each line an d see where you get the error. You do this by F8 in the macro. where it errors may give you a clue as to what is causing it to break.



"It did not really matter what we expected from life, but rather what life expected from us. We needed to stop asking about the meaning of life, and instead to think of ourselves as those who were being questioned by life – daily and hourly. Our answer must consist not in talk and meditation, but in right action and in right conduct. Life ultimately means taking the responsibility to find the right answer to its problems and to fulfill the tasks which it constantly sets for each individual." Viktor Frankl, Man's Search for Meaning, 1946.
 
Posts: 20113 | Location: The Free State of Arizona - Ditat Deus | Registered: March 24, 2011Reply With QuoteReport This Post
Eating elephants
one bite at a time
Picture of ffips
posted Hide Post
I will try to put my answers in your quote below in blue. Thanks for the help. Smile

quote:
Originally posted by Rey HRH:
I haven't used "Scripting.FileSystemObject" before so I can't help at his time.

I also don't understand how you "open a blank spreadsheet and run the macro" as you have to open the spreadsheet with the macro and then run the macro.

The last time I used this macro was two + years ago. Sadly, if I don't do something often, I lose the ability. As I recall, I used to open a new spreadsheet then point it at the macro, but this could be bad memory.

I also don't understand "saving the spreadsheet as the macro at the end of the routine." Are you saying it tries to save the created file as simpleXIsMerger.xls?

If I simply open the macro (starts Excel instance) then do alt f11 and "play" the macro, it goes along merging things together. When it comes to the last file, it asks if I want to save "combininator.xlsm" (yep, named the macro with a play from Dr. Doofenshmirtz) and open it. Yes opens an empynsheet named combininator.xlms which no shows all the date merged into a single sheet of the currently open Excel instance (combininator.xlsm).

One thing I see is the reference "A65536" cell. this is the old limitation of 65536. If the files you are trying to copy have more than 65536 rows of data, this would cause an issue and it's saying start from row 65536 and find the first row above it with data which would be row 1.

Not an issue with current data set. Columns A-J and less than 50 rows in a sheet.

I'd offer to help more but I'm in the middle of working on my retirement macro and it's been slow going and I think your need is time sensitive.

I hope to resolve it by mid day tomorrow, and will likely keep at it for a bit this evening.

I know you described your problem in your OP. I'd like to ask a question a different way and maybe I can help you. And I'm asking you the question because I have a library of macros that I created and maybe I can put a couple together for you. So here is my question:
"What is it that you want to accomplish?

I have a folder. Inside that are a number (2-50) of workbooks that contain a single sheet. The goal is to smash all that data into a single sheet. I know how to get all the sheets into one book the long drawn out way, but that isn't the goal as that would be a workbook with 2-50 sheets in it. Though I suppose I could do the summary macro to create the sngle sheet summary.

What does it look like when you have succeeded in what you are trying to accomplish?"

Single workbook with single sheet that has all data from all other files in it.


You start with several Excel files with single sheets and you want those sheets copied into a single workbook? Is that all that you want to accomplish?

See answer above. If only I could remember how I used the posted macro in the past...

The closest experience I have is I have the user open several excel files and then the macro searches for specific file names and copy data from them into a workbook.

I'll wait for your answer and in the meantime, i'll look up how to copy whole sheets.

Are you overwriting old data?

No

Are you appending data to existing data?

Not sure how to answer. In my head, the macro opens file 1 and creates sheet "combine" then opens file two and adds/appends file 2 data to the "combine" sheet.

You might want to try those excel vba forums. I've done that in the past. Sorry, i'm not much help at this time.

You might jog something loose with your questioning that will spark my memory.

ETA: one other thing is you could open the macro and step through each line an d see where you get the error. You do this by F8 in the macro. where it errors may give you a clue as to what is causing it to break.

I will give that a try.


Thank you. Smile
 
Posts: 3586 | Location: in the southwest Atlanta metro area | Registered: September 10, 2006Reply With QuoteReport This Post
Eating elephants
one bite at a time
Picture of ffips
posted Hide Post
It appears I found this when I was using an older version of Excel on this website. I am going to root around there for a bit and see if it gets me on track.

Here is a video of how it used to work:



Link to original video: https://www.youtube.com/watch?v=MsbdMiruX68

I need to confirm if something has changed in Excel or if I have a data issue that is causing some "extra" pop ups and errors when I run this macro. I'll report back my findings.
 
Posts: 3586 | Location: in the southwest Atlanta metro area | Registered: September 10, 2006Reply With QuoteReport This Post
Eating elephants
one bite at a time
Picture of ffips
posted Hide Post
Okay, memory was jogged enough by site. I also realized that some formulas in the spreadsheets were part of what was causing the "random pop-ups" when running the macro.

My solution was to copy the values in the existing sheet to a new sheet and delete the original sheet from within each Excel file. Then, I opened the macro and ran it. SUCCESS (except for that pesky final error mentioned previously). Even with that error, the data is getting combined as desired and I am not missing any records. So, error be damned, I am ignoring that.

Rey HRH, I don't know if you want to add this one to you library of macros, but it has helped me many times in the past, and now it will likely be a help again currently. I didn't write it, I simply found it at the above mentioned website.

I am good to call this one a wrap.

THANKS Smile I guess I was "thinking out loud."
 
Posts: 3586 | Location: in the southwest Atlanta metro area | Registered: September 10, 2006Reply With QuoteReport This Post
His Royal Hiney
Picture of Rey HRH
posted Hide Post
Good that you figured it out. I literally talk myself through macro development out loud.

I googled "scripting.filesystemobject" and captured a page on objects. I'm sure it'll be useful someday.

I just usually manually open all the files I want to work with. it's easy for me to do when they're in a directory or even a zip file. Just cntrl+a to select all then right click open.



"It did not really matter what we expected from life, but rather what life expected from us. We needed to stop asking about the meaning of life, and instead to think of ourselves as those who were being questioned by life – daily and hourly. Our answer must consist not in talk and meditation, but in right action and in right conduct. Life ultimately means taking the responsibility to find the right answer to its problems and to fulfill the tasks which it constantly sets for each individual." Viktor Frankl, Man's Search for Meaning, 1946.
 
Posts: 20113 | Location: The Free State of Arizona - Ditat Deus | Registered: March 24, 2011Reply With QuoteReport This Post
  Powered by Social Strata  
 

SIGforum.com    Main Page  Hop To Forum Categories  The Lounge    Excel macro that combines multiple files into one sheet

© SIGforum 2024