+ Reply to Thread
Results 1 to 11 of 11

Appending / Copying worksheets from many excel files into one master file

  1. #1
    Registered User
    Join Date
    06-10-2009
    Location
    Massachusetts
    MS-Off Ver
    Excel 2003
    Posts
    4

    Question Appending / Copying worksheets from many excel files into one master file

    Hey everyone –

    I have a folder of ~250 excel files containing similar formatted data tables that I would like to combine / merge / append all into one long master file.

    The data tables are on the first worksheet of each file, and occupy the columns A-H. Depending on the file, the data occupies anywhere from the first ~20 rows to the first ~40 rows.

    Columns ABC are strings, columns DEF are entered numbers, and columns GH are formulated averages referencing pairs of rows in DEF. Because GH are averages of pairs, there is always half as many rows as A-F.

    I have attached a screenshot of one of the files to show what it looks like (also linked here): \1

    I would like to copy the data table from each of the files into one master file, making one very “tall” table (the first row of one table would be directly below the last row of the one before it). This means that the data would still occupy only columns A-H, with no empty rows in A-F.

    If anyone could tell me how to accomplish this without manually copying each one into the master, it would be much appreciated. I do not have much experience with macros or VBA, but am a quick learner…

    Thanks!

    - Adam.
    Attached Images Attached Images

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Appending / Copying worksheets from many excel files into one master file

    On the sheets you are copying from, is there data to be IGNORED below the tables to be copied? The reason I ask is because the simplest way to spot the lastrow is to jump UP to it from the bottom of column A. But if there is other data in the way, we wouldn't reach the bottom of the data you want copied. See the green coded section in the macro to see what I mean.

    So, can we jump up from the bottom of column A on those sheets, or is there other stuff in the way?

    I have a standard compilation macro I use for this, you will need to tweak the "strPath" variable for your needs.

    Also, it imports EVERY file in the folder, so make sure the ones you don't want are taken out of the folder, at least temporarily.
    Please Login or Register  to view this content.
    How to use the macro:

    1. Open up your workbook
    2. Get into VB Editor (Press Alt+F11)
    3. Insert a new module (Insert > Module)
    4. Copy and Paste in your code (given above)
    5. Get out of VBA (Press Alt+Q)
    6. Save your sheet

    The macro is installed and ready to use. Press Alt-F8 and select it from the macro list.
    Last edited by JBeaucaire; 06-10-2009 at 01:33 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    06-10-2009
    Location
    Massachusetts
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Appending / Copying worksheets from many excel files into one master file

    wow, JBeaucaire, thanks so much. the data tables are the only things in the worksheet, so i guess the jump up command should work.

    I'll try out the code and let you know...

    Thanks again!

  4. #4
    Registered User
    Join Date
    06-10-2009
    Location
    Massachusetts
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Appending / Copying worksheets from many excel files into one master file

    unfortunately, the macro did not work. I followed your instructions, pressed Alt+F8, clicked the macro, and pressed run.

    Though it looks like nothing happened, the name of the now-blank worksheet did change to "Report" and all other worksheets were deleted.

    I realized that I was running the macro from my new "compilation" worksheet, which happened to be inside that folder... but realized that might cause issues, so i moved it out of the folder and ran it but the results are still the same...

    Lemme know how we can make this work, I appreciate you helping me out.

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Appending / Copying worksheets from many excel files into one master file

    Post back up the code after you tweaked it. Be sure to wrap it in code tags.

  6. #6
    Registered User
    Join Date
    06-10-2009
    Location
    Massachusetts
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Appending / Copying worksheets from many excel files into one master file

    Quote Originally Posted by JBeaucaire View Post
    Post back up the code after you tweaked it. Be sure to wrap it in code tags.
    Here's my modified code, for a folder full of excel files at C:/Compilation

    Please Login or Register  to view this content.
    I ran the debugger and put a break inside the "do while" loop, but the program never stopped there. So then I put a watch on strFileName and throughout the entire code, it's always an empty string... which is probably why the code never enters the loop. I'm guessing there's something wrong with the file name assignment, but I don't know much about this stuff...

    thanks for any help!

  7. #7
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Appending / Copying worksheets from many excel files into one master file

    I think you just need to put a \ at the end of the line where you define
    Please Login or Register  to view this content.
    Does that work?
    ChemistB
    My 2?

    substitute commas with semi-colons if your region settings requires
    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Appending / Copying worksheets from many excel files into one master file

    Yep, forgot the closing "\"

  9. #9
    Registered User
    Join Date
    05-26-2006
    Posts
    36

    Re: Appending / Copying worksheets from many excel files into one master file

    thanks!!!!! this worked for me also

  10. #10
    Registered User
    Join Date
    08-22-2012
    Location
    geneve
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: Appending / Copying worksheets from many excel files into one master file

    Hi JBeaucaire
    i am very interesting in your simple and very straight forward macro !
    i have tried it but it recalls always the common first row .
    How can i have it only once and then data underneath (no duplication of first commun row - titles)

    thanks a lot for your help

  11. #11
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Appending / Copying worksheets from many excel files into one master file

    It's normally against the rules to post your own questions in other people's threads, but this seems a useful clarification on the existing macro:

    Please Login or Register  to view this content.
    I suppose you change that to A2 to skip row1.

+ 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