+ Reply to Thread
Results 1 to 37 of 37

Writing a Macro for combining individual workbooks into one.

  1. #1
    Registered User
    Join Date
    12-10-2012
    Location
    orem, ut
    MS-Off Ver
    Excel 2003
    Posts
    17

    Writing a Macro for combining individual workbooks into one.

    In my work I run a report using 200 individual reports in separate workbooks and I was hoping to make a macro that combines all 200 into one workbook that I can use the vlookup function on. Currently I am manually looking at each individual report and typing in the four needed numbers from each. I would use the vlookup function but it won't carry over to each workbook I need to search from. I know about dim but don't know how to use it to create a macro that copies, pastes in a "Master Workbook", then closes or minimizing the workbook and moving on to the next one and repeating the process until I have every report on one "Master Workbook" that I can use vlookup. How I would write that, I don't know. Anybody?
    DotWFM

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Writing a Macro for combining individual workbooks into one.

    You want to "run the directory" - here's a routine that you can alter to your specifics:

    Please Login or Register  to view this content.
    Change "Database" to "Master" etc. etc.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    12-10-2012
    Location
    orem, ut
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Writing a Macro for combining individual workbooks into one.

    Thank you for your quick response, I appreciate your help. Could you explain the specifics of each step of your response? Can you, or someone with a lot of time, walk me through what the steps are actually doing? Sorry, this is my first time here and I'm feeling like the ol' Jalopy stuck in 1st trying to merge into fast lane.
    DotWFM

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Writing a Macro for combining individual workbooks into one.

    Maybe the old jalopy is a hot rod:

    'Running a Directory to extract data

    Please Login or Register  to view this content.
    I broke out the code and commented it - hope this helps!

  5. #5
    Registered User
    Join Date
    12-10-2012
    Location
    orem, ut
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Writing a Macro for combining individual workbooks into one.

    So if I wanted to copy cells A1:D10 and paste it on the Master where would I insert that into the macro? Where can I edit is what I am asking.
    DotWFM

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Writing a Macro for combining individual workbooks into one.

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    12-10-2012
    Location
    orem, ut
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Writing a Macro for combining individual workbooks into one.

    K, I am running into this error:
    Capture.PNG

    Here is what I have so far on the Macro
    Formula: copy to clipboard
    Please Login or Register  to view this content.



    What do I need to change to make this work?
    Also, can I just hit run when the books are open or do I need to run it on the Master? Because so far nothing has changed. I follow it through the steps but no workbook moves, nothing is copied, nothing.
    Last edited by DotWFM; 12-11-2012 at 06:20 PM.

  8. #8
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Writing a Macro for combining individual workbooks into one.

    You don't need to run it on the Master.
    Please Login or Register  to view this content.
    is valid code iff the
    Please Login or Register  to view this content.
    above yielded a non-null - step (F8) through the code and see whether the directory is initiated.

  9. #9
    Registered User
    Join Date
    12-10-2012
    Location
    orem, ut
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Writing a Macro for combining individual workbooks into one.

    Okay, I changed the the U=Dir() to U=Dir(P) and it now gives no error. I still don't have any movement at all within the workbook.
    I believe it is because of the names of the reports. First, they are all .txt files. I generally run a basic macro that adds some formulas and hides a bunch of columns and then I save them as .xlsx files. If possible I would like to tack my macro onto yours to have them run in sequence, but that might be putting the buggy in front of the horse at this point.
    Second, all these individual reports start with "report_list_bcms_agent" and then have a different 4 digit number for each agent, and then it ends with the dates. So, I am guessing the macro is running perfectly but cannot find anything that matches to copy and paste. I stepped through it and when it came down to the part about
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    it just skips that step and goes to the next one and then finishes. Am I right in assuming that the names are where my problem lies?

    DotWFM

  10. #10
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Writing a Macro for combining individual workbooks into one.

    I think you should change this line:

    Please Login or Register  to view this content.
    The U=Dir(P) must occur just once:
    Please Login or Register  to view this content.
    Then at the procedure:

    Please Login or Register  to view this content.
    Perhaps you should change this:

    Please Login or Register  to view this content.

    And get rid of this:

    Please Login or Register  to view this content.
    .
    .
    .
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    12-10-2012
    Location
    orem, ut
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Writing a Macro for combining individual workbooks into one.

    So it goes to the right path now and everything works perfectly except what I actually need to run. Here's the whole thing:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Highlighted in red is just skipped when I step through it. Then it does an infinite loop at the blue highlighted. What is wrong?
    Last edited by DotWFM; 12-12-2012 at 06:10 PM. Reason: I figured some more things out and updated the formula.

  12. #12
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Writing a Macro for combining individual workbooks into one.

    You can only have U=Dir(P) once, at the beginning - Change this
    Please Login or Register  to view this content.
    to this
    Please Login or Register  to view this content.
    And take these lines out:

    Please Login or Register  to view this content.
    then see if it at least gets into the red!

  13. #13
    Registered User
    Join Date
    12-10-2012
    Location
    orem, ut
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Writing a Macro for combining individual workbooks into one.

    No, still skips it. But it is now accessing the Master correctly and pulls it into the book.

  14. #14
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Writing a Macro for combining individual workbooks into one.

    What do you get for
    Please Login or Register  to view this content.
    ?????

  15. #15
    Registered User
    Join Date
    12-10-2012
    Location
    orem, ut
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Writing a Macro for combining individual workbooks into one.

    I don't really understand what that means. It's the path of the active workbook, correct?
    Is this it?
    Formula: copy to clipboard
    Please Login or Register  to view this content.

  16. #16
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Writing a Macro for combining individual workbooks into one.

    OK - that means that the directory is being accessed - are there any files in the directory that you don't want to process???

  17. #17
    Registered User
    Join Date
    12-10-2012
    Location
    orem, ut
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Writing a Macro for combining individual workbooks into one.

    No, I made sure that the path had no files but the macro, master, and individual reports.

  18. #18
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Writing a Macro for combining individual workbooks into one.

    Actually it should be the first workbook in the folder - not including the path!

    That's the critical part of the routine - if you don't get a book name for Dir(P) then the whole directory isn't being accessed and the routine is moot. Try running it from the Master (with the Master activated) and step to the Dir(P) and play with the Path and Backslash until you get a book name for Dir(P).
    Last edited by xladept; 12-13-2012 at 01:06 PM.

  19. #19
    Registered User
    Join Date
    12-10-2012
    Location
    orem, ut
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Writing a Macro for combining individual workbooks into one.

    Okay, I changed some things and now I am getting the error 'Subscript out of range' right here:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    I moved the Master into a different folder than the macro and it is now hitting more of the steps. I believe it will work now if we can figure out why it's stuck on the wb.sheets.
    Last edited by DotWFM; 12-13-2012 at 02:29 PM. Reason: Highlighted red, not yellow.

  20. #20
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Writing a Macro for combining individual workbooks into one.

    The subscript will fire if there is no "Sheet1" - Change that to Whatever the sheet name actually is!

  21. #21
    Registered User
    Join Date
    12-10-2012
    Location
    orem, ut
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Writing a Macro for combining individual workbooks into one.

    OOoooh, got it. It worked!
    Thank you so much for your thorough help.

  22. #22
    Registered User
    Join Date
    12-10-2012
    Location
    orem, ut
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Writing a Macro for combining individual workbooks into one.

    Second question,
    I generally run a macro on these files before I compile them, any way I can work that macro into this macro? I've been researching "Call" so I could call the macro but it hasn't clicked yet.

    DotWFM

  23. #23
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Writing a Macro for combining individual workbooks into one.

    Hi DotWFM,

    Glad you got it running -

    Please Login or Register  to view this content.

  24. #24
    Registered User
    Join Date
    12-10-2012
    Location
    orem, ut
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Writing a Macro for combining individual workbooks into one.

    Beautiful! Thank you so much.
    P.S. I know I should give you reputation points or something, how do I do that?

  25. #25
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Writing a Macro for combining individual workbooks into one.

    You're welcome! To give rep just click on the star icon to the left of my post.

  26. #26
    Registered User
    Join Date
    12-13-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Exclamation Re: Writing a Macro for combining individual workbooks into one.

    Hi Guys,
    I am attempting to do the exact same thing just different ranges, but mine doesn't seem to work. Could you publish the final code? what I have is the following but the Dir() value goes through all the files and becomes invalid before the code even gets to the U=Dir() portion

    Any help is much appreciated.
    Cheers,
    Eli

    Please Login or Register  to view this content.

  27. #27
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Writing a Macro for combining individual workbooks into one.

    Hi tsinovoi,

    The whole routine depends on the first directory call - Dir(P) - play with that until it yields a book name.

    (BTW You should start your own thread for this!)

  28. #28
    Registered User
    Join Date
    12-13-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Writing a Macro for combining individual workbooks into one.

    It does yield a book name, it yields "Master Worksheet.xlsm"

  29. #29
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Writing a Macro for combining individual workbooks into one.

    Try it like this:

    Please Login or Register  to view this content.
    BTW I think SubMerge is quite clever!
    Last edited by xladept; 12-14-2012 at 08:53 PM.

  30. #30
    Registered User
    Join Date
    12-13-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Writing a Macro for combining individual workbooks into one.

    Yes its is ! very clever.

    still having the same issue though. When i put a watch on the Dir() expression it seems that the expression iterates through each file in the folder before it is even mentioned in the code. Maybe I dont have the file in the right directory? or am pasting the code in the wrong place? currently its module 1 of the master worksheet. The file directory itself has the following files Master Worksheet.xlsm,W1.XLSM, W2.xlsm, W3.xlsm.

    What do you think?

  31. #31
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Writing a Macro for combining individual workbooks into one.

    Try commenting this out:

    Please Login or Register  to view this content.
    and see if it gets to the open

  32. #32
    Registered User
    Join Date
    12-13-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Writing a Macro for combining individual workbooks into one.

    Doesnt work, same issue.

  33. #33
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Writing a Macro for combining individual workbooks into one.

    It never gets to the open???

  34. #34
    Registered User
    Join Date
    12-13-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Writing a Macro for combining individual workbooks into one.

    it gives an error on U=Dir()
    So no it doesnt ever get to any file besides the master

  35. #35
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Writing a Macro for combining individual workbooks into one.

    I set up a test with your basic information (Book Names & Sheet Names) and it ran for me?????

  36. #36
    Registered User
    Join Date
    12-13-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Writing a Macro for combining individual workbooks into one.

    Are you keeping all of the excel files in the same folder?
    Also where are you pasting the code for Sub Merge

  37. #37
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Writing a Macro for combining individual workbooks into one.

    Yes - the files are all in the same folder(directory). The code is in a module in macro-enabled workbook.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1