+ Reply to Thread
Results 1 to 17 of 17

Copy & rename sheets across multiple workbooks into one master.

  1. #1
    Registered User
    Join Date
    01-17-2012
    Location
    New Jersey
    MS-Off Ver
    Excel 2010
    Posts
    40

    Copy & rename sheets across multiple workbooks into one master.

    I have dozens of workbooks with multiple sheets per workbook, however I only need one sheet from each - ironically, always named "Sheet1." Is there a script that could copy all the sheet1's into one workbook, and (optimally) assign them the name of the value in a static cell, for instance C3? (the date is always in C3)

    Otherwise, manually moving and renaming them using excel is rather tedious.
    Last edited by CapnSef; 01-25-2012 at 12:30 AM.

  2. #2
    Registered User
    Join Date
    01-17-2012
    Location
    New Jersey
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Copy & rename sheets across multiple workbooks into one master.

    Bump no response

  3. #3
    Valued Forum Contributor MaczaQ's Avatar
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    510

    Re: Copy & rename sheets across multiple workbooks into one master.

    hello,

    I thought and finaly created following code - I hope in excel 2010 it will work; I tested it under 2003 and works fine
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Best Regards
    MaczaQ
    ---------------------------------------------------------------------------------------------------------------------------
    If you are satisfied with the solution(s) provided, please mark your thread as Solved
    If you are pleased with my answer consider to rate it. To thank someone who has helped you, click on the star icon below their name.
    - This way you will add him some reputation points ... thanks in advance.

  4. #4
    Registered User
    Join Date
    01-17-2012
    Location
    New Jersey
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Copy & rename sheets across multiple workbooks into one master.

    maczaq:

    I appreciate the code you've contributed greatly - however I don't quite understand what its doing. When I run it in a workbook, I get another sheet called "sheet1_old." It appears to be identical to sheet_1. I was looking for a macro to trawl all the other workbooks in a directory and take Sheet1 from all of them at once. Can this code be modified to pull from other workbooks? I apologize for not being more understandable!

    Edit: it appears as if its combinining sheets within the workbook into one continuous sheet and renaming it sheet1 - I was looking for it to go through the directory, opening each document and searching for a sheet1 then compiling into one new workbook holding all of the sheet1s (named in any way would do.)
    Last edited by CapnSef; 01-21-2012 at 11:31 PM.

  5. #5
    Valued Forum Contributor MaczaQ's Avatar
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    510

    Re: Copy & rename sheets across multiple workbooks into one master.

    hi CapnSef,

    previously I understand that you want to join data from all sheets in active workbook into one sheet.
    So I created macro which executed following scenario:
    1. check if "sheet1" exist
    2. if yes then rename it
    3. create new sheet 1
    4. copy all data from sheets (excluding sheet1) and paste it in new sheet1

    and that's all.

    additional question: Am I right that You have some folder in your system where are many XLS files ? Each file have "Sheet1" and you want to open one file after another and copy all data from sheet1 directly to your main file workbook into sheet 1 ?

  6. #6
    Registered User
    Join Date
    01-17-2012
    Location
    New Jersey
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Copy & rename sheets across multiple workbooks into one master.

    Yes exactly maczaq - the sheets are coming from many XLS files, however if they could be copied into separate sheets that is my goal. If I have 10 XLS files to start, I'd like 1 XLS file with 10 sheets at the end.

  7. #7
    Valued Forum Contributor MaczaQ's Avatar
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    510

    Thumbs up Re: Copy & rename sheets across multiple workbooks into one master.

    Hi again,

    One problem is that you can not have many sheets in the same workbook called "sheet1" (in Polish Arkusz1 - presented in test file).
    So I prepared example which copy all sheets from XLS files which are in the same directory as main.xls and after copy always rename copied sheet like (name of workbook + "_" + name of sheet in source workbook)

    Try to test my example, I hope we are very close to your goal

    [unzip all files into one directory on your hard drive and after open "main.xls" press the button]

    whole code here:
    Please Login or Register  to view this content.
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    01-17-2012
    Location
    New Jersey
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Copy & rename sheets across multiple workbooks into one master.

    We are almost there my friend! Your example runs great for your test sample in Excel 2010 - however, when I go to use my set of worksheets instead of your test sheets I get an error on this line:


    Please Login or Register  to view this content.
    And the error is:

    Run-time error '1004':

    Method 'Copy' of object'_Worksheet' failed

    I'm curious what is causing this to fail, pardon my poor debugging.

  9. #9
    Valued Forum Contributor MaczaQ's Avatar
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    510

    Re: Copy & rename sheets across multiple workbooks into one master.

    hmm ... probable your workbooks / worksheets are protected ? (if yes copy method could be not possible)
    My code should works under all version of Excel. Of course it is a bit difficult for me when I don't see your files to copy. Could you attach any source workbook (could be empty) because we copy whole sheets - content is not require.

  10. #10
    Registered User
    Join Date
    01-17-2012
    Location
    New Jersey
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Copy & rename sheets across multiple workbooks into one master.

    Thank you again so much for your patience in this matter! I attached two excel sheets with your main file that gives the error I described earlier. Let me know what your diagnosis is!

    Thanks so much
    Attached Files Attached Files

  11. #11
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Copy & rename sheets across multiple workbooks into one master.

    Please Login or Register  to view this content.



  12. #12
    Registered User
    Join Date
    01-17-2012
    Location
    New Jersey
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Copy & rename sheets across multiple workbooks into one master.

    snb:

    Your code looks like it worked... incredibly elegantly, might I say. The one thing is, what is the output? It looks like it closes everything without saving at the end... its certainly opening everything smoothly and doing something, however at the end there is no master workboko and nothing remains open.

  13. #13
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Copy & rename sheets across multiple workbooks into one master.

    If you put this macro into the workbook where all the sheets have to be assembled; run the code.
    After that all sheets will be copied to this workbook
    The workbooks that have been opened will be closed after copying.
    The workbook the sheets have been copied into, remains open.

    You can check the code using F8 in the vbeditor

  14. #14
    Registered User
    Join Date
    01-17-2012
    Location
    New Jersey
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Copy & rename sheets across multiple workbooks into one master.

    I headed home from the office, and the code runs on my personal machine - I can see it adding sheets to the workbook (excellent!) however, as it finishes looping through it closes the final workbook. I've opened and run it countless ways, however the original workbook won't stay open.

    I've debugged and determined when it closes out:
    Closes.JPG

    The next step closes both the open loop and also the entire document - so it closes the workbook (without saving the added sheets) and the VB Project. I apologize again for being such a pain.

  15. #15
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Copy & rename sheets across multiple workbooks into one master.

    I had hoped you to analyse what the code does and would have realized, that in she same folder in which the macro looks for files, also the open workbook resides. Opening that and closing at the same time isn't what we want.

    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    01-17-2012
    Location
    New Jersey
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Copy & rename sheets across multiple workbooks into one master.

    snb

    I cannot thank you enough for your patience in this matter and understanding in my limited grasp of scripting. You've delivered exactly what I envisioned and it runs beautifully. Thank you so much! I've added reputation to you and will mark this thread as [SOLVED] quite happily!

  17. #17
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Copy & rename sheets across multiple workbooks into one master.

    That's what it's all about...

+ 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