+ Reply to Thread
Results 1 to 37 of 37

Copy sheets with the same name from multiple workbooks

  1. #1
    Registered User
    Join Date
    09-21-2012
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    39

    Copy sheets with the same name from multiple workbooks

    Hi guys!

    I found a really useful forum post here, but I don’t really know what I need to amend to get the VBA code to perform in the way that I would like it to.

    I have a significant number of workbooks which I would like to collect data from. Each workbook is structured in the same way with the sheets named ‘Key Stage 1’, ‘Key Stage 2’, ‘Key Stage 3’ and ‘Key Stage 4’.

    The workbooks are closed but saved in one folder. I need all the data from the sheet ‘Key Stage 1’ from all the workbooks consolidated into a new sheet in a new workbook in the same folder.

    I need to repeat this process for Key Stage 2, 3 and 4, so that having run the code/macro I have a workbook in the same folder with 4 sheets consolidating all the data (Key Stage 1, 2, 3 and 4) from all the workbooks.

    How do I do this?

    I look forward to hearing from you.

    Christian :)

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy sheets with the same name from multiple workbooks

    Try this code
    Please Login or Register  to view this content.
    Copy the Excel VBA code
    Select the workbook in which you want to store the Excel VBA code
    Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
    Choose Insert | Module
    Where the cursor is flashing, choose Edit | Paste

    To run the Excel VBA code:
    Choose Tools | Macro | Macros
    Select a macro in the list, and click the Run button
    Last edited by arlu1201; 11-16-2012 at 10:36 AM.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    09-21-2012
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Copy sheets with the same name from multiple workbooks

    Thank you!!!

    I will try this and let you know how I fare

    Chris

  4. #4
    Registered User
    Join Date
    09-21-2012
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Copy sheets with the same name from multiple workbooks

    Hi Arlette

    I'm getting the following run-time error:

    Attachment 194201

    What do I need to do?

    Thank you!

    Chris

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy sheets with the same name from multiple workbooks

    Sorry, there was a small typo in that code. I have edited it, so you should be able to test now.

  6. #6
    Registered User
    Join Date
    09-21-2012
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Copy sheets with the same name from multiple workbooks

    Hi Arlette

    The macro works, but I wondered if you could help me tweak it so that it will perform the way I would like it to.
    I would like the macro:

    - to also retrieve data from a sheet titled 'EYFS'
    - to copy data from the second row onwards, as the spreadsheets have row headers
    - to ignore a worksheet if it does not contain any data in the 2nd row onwards

    Attached is the workbook produced as a result of running the macro.

    Key Stage 4 - should have three rows of data
    Key Stage 3 - should have two rows of data
    Key Stage 2 - should have no data (because the target source does not have data)
    Key Stage 1 - should have no data (same as above)

    Thank you for your help in developing this macro so far - I really appreciate it :)

    Christian
    Attached Files Attached Files

  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy sheets with the same name from multiple workbooks

    So each of the files in that folder also contain a sheet called EYFS?

    Where should this data go? Into which sheet of your master file?

  8. #8
    Registered User
    Join Date
    09-21-2012
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Copy sheets with the same name from multiple workbooks

    Each of the target workbooks, contain the following sheets: EYFS, Key Stage 1, 2, 3 & 4.

    Different users will only access one or two of the sheets which are relevant to them, so if I received three workbooks they may contain data as follows:

    Workbook 1 - Key Stage 1
    Workbook 2 - Key Stage 1 & 2
    Workbook 3 - Key Stage 3
    Workbook 4 - Key Stage 3 & 4
    Workbook 5 - EYFS & Key Stage 1

    All of the data should go into the relevant sheet in the master workbook.

    Does that make sense?

  9. #9
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy sheets with the same name from multiple workbooks

    Sure. So there are chances that 1 workbook may not have all the Key stage sheets, right?

    You should have mentioned about the EYFS sheet at the beginning itself, to avoid double work.

  10. #10
    Registered User
    Join Date
    09-21-2012
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Copy sheets with the same name from multiple workbooks

    Sorry - my bad. I figured I would be able to tweak the code, but on seeing it I was baffled.

    Each workbook will have some data recorded - might be lots or very little - but all the workbooks will have some data. If a workbook has ten data entries on the KS2 sheet, the code will need to search all the sheets, realise that they are blank and ignore them - only transferring the data on the KS2 sheet.

    Thank you for all your help so far - I appreciate your time, effort and expertise

    Chris

  11. #11
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy sheets with the same name from multiple workbooks

    Try this updated code
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    09-21-2012
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Copy sheets with the same name from multiple workbooks

    Hi Arlette

    I will try this and let you know how I go.

    Thank you for all your help.

    Christian

  13. #13
    Registered User
    Join Date
    09-21-2012
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Copy sheets with the same name from multiple workbooks

    Hi Arlette

    I got the following compile error when I ran the macro:

    'Variable not defined'

    It identified
    Please Login or Register  to view this content.
    Does it matter my spreadsheets use iRows?

    Christian

  14. #14
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy sheets with the same name from multiple workbooks

    Oops i missed out on the declaration for lrow.

    Yes you can change the lrow in the code to iRow. Just ensure that you declare it - Dim iRow as long.

  15. #15
    Registered User
    Join Date
    09-21-2012
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Copy sheets with the same name from multiple workbooks

    Hi Arlette

    I declared the iRow, but then got the same error as seen in Post #4.

    I think you said it was a typo - what do I need to adjust?

    (I feel like Thomas Edison on the cusp of a breakthrough!)

  16. #16
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy sheets with the same name from multiple workbooks

    Where are you using iRow? I have used lrow in my code which needs to be declared.

    If you are using lrow in the code, just declare it - Dim lrow as long.

    If instead of lrow, you are using iRow, then declare iRow as long.

  17. #17
    Registered User
    Join Date
    09-21-2012
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Copy sheets with the same name from multiple workbooks

    I've done that - the runtime error is 1004:

    The method 'Open' of object 'Workbooks' failed.

  18. #18
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy sheets with the same name from multiple workbooks

    Are you using the same code from post 11? Or have you made some changes? If you have, please post your code here.

  19. #19
    Registered User
    Join Date
    09-21-2012
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Copy sheets with the same name from multiple workbooks

    Hi Arlette

    I am using the same code as post #11. I have declared the lRow as asked. When I run the macro, a runtime error '1004' saying there is an issue in the method 'Open' of object 'Workbooks'.

    Is it possible that Excel 2003 does not accept that method?

    Whereas before the macro, would return data, currently all that is happening is that the workbook is being populated with blank sheets called 'Key Stage 1, 2, 3 and 4'. There is no EYFS sheet being populated.

  20. #20
    Registered User
    Join Date
    09-21-2012
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Copy sheets with the same name from multiple workbooks

    I will upload the documents shortly.

  21. #21
    Registered User
    Join Date
    09-21-2012
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Copy sheets with the same name from multiple workbooks

    Hi Arlette

    This is the code I am using:

    Please Login or Register  to view this content.
    If the following three workbooks (Test 1, Test 2, Test 3) were the target sources, MasterBook demonstrates how I would like the macro to retrieve the data from each. At present, it's not doing that.

    Where am I going wrong?

    Chris
    Attached Files Attached Files

  22. #22
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy sheets with the same name from multiple workbooks

    I had asked the code to create a new workbook and name the sheets but since this is not required, here is the updated code -
    Please Login or Register  to view this content.

  23. #23
    Registered User
    Join Date
    09-21-2012
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Copy sheets with the same name from multiple workbooks

    Hi Arlette

    I'm still having difficulties - nothing is happening now.

    I have a workbook called 'Summary.xls' in the same folder as the other workbooks. I have created the worksheets for the data to be stored in. I have placed the code in 'Summary' and when I run the macro, nothing happens.

    The workbooks are password protected and when I would run the initial macro from post #2, I would be asked to type in the password for each workbook - so it was clear the workbooks were been accessed. At present, it doesn't seem as though they are.

    Is there something I am missing?

    Here is the code I am using:

    Please Login or Register  to view this content.

  24. #24
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy sheets with the same name from multiple workbooks

    So is the macro prompting you to put in the password for each file? You can even have that included in the code.

    Is your path correct where the files reside?

    Updated code to ensure it does not open the summary file -
    Please Login or Register  to view this content.
    Last edited by arlu1201; 11-20-2012 at 01:17 PM.

  25. #25
    Registered User
    Join Date
    09-21-2012
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Copy sheets with the same name from multiple workbooks

    Hi Arlette

    Still no joy.

    I have made sure the path is correct. The macro is not prompting me to enter the password; before it was.

    I'm totally baffled.

  26. #26
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy sheets with the same name from multiple workbooks

    The macro resides in the master file right which is open at the time of running the macro?

  27. #27
    Registered User
    Join Date
    09-21-2012
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Copy sheets with the same name from multiple workbooks

    Hi Arlette

    Yes - the macro resides in the master workbook ('Summary') and that is where the macro is being run from. The master is the only workbook open at teh time of running. Previously, the macro opened the closed workbooks - now it doesn't.

  28. #28
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy sheets with the same name from multiple workbooks

    Can you please upload your file here?

  29. #29
    Registered User
    Join Date
    09-21-2012
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Copy sheets with the same name from multiple workbooks

    Hi Arlette

    Please see the attached files.

    Thank you

    Chris :)
    Attached Files Attached Files

  30. #30
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy sheets with the same name from multiple workbooks

    Try the code in post 24. I have edited it. The KS tabs have a space in them and the code was missing it.

  31. #31
    Registered User
    Join Date
    09-21-2012
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Copy sheets with the same name from multiple workbooks

    Arlette,

    I think you may have cracked it!!!!!! Will test on my live document when I get into work.

    Thank you for all your help so far!

    Chris

  32. #32
    Registered User
    Join Date
    09-21-2012
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Copy sheets with the same name from multiple workbooks

    Hi Arlette

    The macro is working, although when I run it the following dialogue box appears:

    Attachment 195516

    Having clicked no, the following dialogue box appears:

    Attachment 195517

    When I click 'End', the macro completes the transfer.

    Is there something I need to adjust to make sure that I don't get the second dialogue box?

    Thank you for all your help.

    Chris :)

  33. #33
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy sheets with the same name from multiple workbooks

    Your attachments didnt load properly. Please try again.

  34. #34
    Registered User
    Join Date
    09-21-2012
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Copy sheets with the same name from multiple workbooks

    Hi Arlette

    The first dialogue box says:

    'Summary.xls is already open. Reopening will cause any changes you made to be discarded. Do you want to reopen Summary.xls?'

    The second dialogue box says:

    'Run-time error '1004':
    Method 'Open' of object 'Workbooks' failed

  35. #35
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy sheets with the same name from multiple workbooks

    Isnt the macro saved in the summary.xls file?

  36. #36
    Registered User
    Join Date
    09-21-2012
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Copy sheets with the same name from multiple workbooks

    Yes - the macro is saved in Summary.xls.

  37. #37
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy sheets with the same name from multiple workbooks

    Updated code -
    Please Login or Register  to view this content.

+ 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