+ Reply to Thread
Results 1 to 16 of 16

copy corresponding sheet from three workbooks to one file

  1. #1
    Forum Contributor
    Join Date
    10-24-2012
    Location
    Chicago
    MS-Off Ver
    O365
    Posts
    311

    copy corresponding sheet from three workbooks to one file

    Hello,

    I have the following workbooks and code. I need the loop to go through the three years of workbooks 2016 through 2018 and take the tab from each of the years files and save it down to one file per tab name so the ending file would have the three years from the one entity in one file and move to the next tabs etc. I have the code working to do two years but when I add a third year i get errors. The files are attached and the code is below. Can someone help please? The actual files have about 50 tabs in each.

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

  2. #2
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: copy corresponding sheet from three workbooks to one file

    Try this version and see if it produces what you want.

    Please Login or Register  to view this content.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  3. #3
    Forum Contributor
    Join Date
    10-24-2012
    Location
    Chicago
    MS-Off Ver
    O365
    Posts
    311

    Re: copy corresponding sheet from three workbooks to one file

    Thanks. That did not work as designed. The way it works is I have the template and three files one for each year for a total of four files. I open the template macro and click run and it opens the three years files and copies each year from the three files for a specific tab number and drops it into one file and saves the file down with the tab name as the file name and then moves to the second tab and then the third and so on. It did work when I had just two years in the macro but as soon as I added the third year it is breaking down. I know it is probably a simple fix and I am not seeing it. It is where I added the 2018 workbook into the file where it already has a set variable so doesn't open the 2018 file to grab the tab I need. Thanks again for the help.

  4. #4
    Forum Contributor
    Join Date
    10-24-2012
    Location
    Chicago
    MS-Off Ver
    O365
    Posts
    311

    Re: copy corresponding sheet from three workbooks to one file

    Your code is close to working. It is stacking all of the tabs for 2016 to one tab in the template and then 2017 to one tab and so on. I need it to grab Sheet A in the 2016 file, sheet A in the 2017 file and Sheet a in the 2018 file and paste them into the appropriate year tab and then save the file down with the tab name and then move to Tab B for 2016, 2017 and 2018 and paste each sheet into one file with the second tab name save it down and keep moving through until all unique tabs have files with a 2016, 2017 and 2018 tab in it.

  5. #5
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: copy corresponding sheet from three workbooks to one file

    Sorry, but I am confused as to what your final output is expected to be. Do you want to first copy columns A:B of each sheet from the OPIS files to the corresponding year sheet in the template file and then once that is done, make a separate file of each sheet in the template file? I think the reason your original code balked is because Dictionary has a limit of about 32K characters. If you can provide a clearer picture of what you want to do with the OPIS files with respect to copying and pasting, and then once that is done, what you want done with the sheets in the template file, then maybe I can revise the code to do the full job.

  6. #6
    Forum Contributor
    Join Date
    10-24-2012
    Location
    Chicago
    MS-Off Ver
    O365
    Posts
    311

    Re: copy corresponding sheet from three workbooks to one file

    So there are three years...2016, 2017 and 2018. Each year has the same company numbers one company on each tab. I want the main template to open the three years of files and I want it to go to the 2016 file and grab the first company and paste it into the 2016 tab on the template. Then go to the 2017 file and grab the same company tab and copy it to the 2017 tab of the template then go to the 2018 file and grab the same company tab and copy it to the 2018 tab of the template. Once the one company has all three years in the template I want the vba to save the file down to the same folder the four files are in (template;2016;2017;2018 files) with the file name being the company number that the tab was named. Then I want it to loop and do the second company consolidating the three years save and close the file down and continue until all of the tabs for all the companies are complete. The end result in the folder would be the template, 2016, 2017, 2018 and then a file for each company by itself with the three years. So if there were 100 companies there would be 104 files in the folder. Hopefully that is making sense. The macro as it is works for 2 years but when I add the third it is breaking down. I updated the code and got it working kind of however the 2017 tab in the template isn't working right. I will post those files. The macro works creating all of the files however it isn't moving to the next sheet for the loop. If you run it you will see it copies company tab 00000 to all three years for every single file.
    Attached Files Attached Files
    Last edited by amartin575; 09-14-2019 at 10:08 PM.

  7. #7
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: copy corresponding sheet from three workbooks to one file

    See if this is more what you are looking for.
    Please Login or Register  to view this content.
    Last edited by JLGWhiz; 09-15-2019 at 11:53 AM.

  8. #8
    Forum Contributor
    Join Date
    10-24-2012
    Location
    Chicago
    MS-Off Ver
    O365
    Posts
    311

    Re: copy corresponding sheet from three workbooks to one file

    Hi,

    Thanks wb16 through wb17 and j were not defined. Once I defined them wb16 through wb17 as Workbook and j As long I end up getting subscript out of range on ThisWorkbook.Sheets(j).Copy Before:=ActiveWorkbook.Sheets(1)

    I hope I explained myself appropriately.

    An example is

    Open 2016 file and grab tab 10003 and paste it to the 2016 tab of the template
    Open 2017 file and grab tab 10003 and paste it to the 2017 tab of the template
    Open 2018 file and grab tab 10003 and paste it to the 2018 tab of the template
    Save as on the template naming the file 10003.
    Then loop through the same steps for all of the tabs of the workbook so that I have one file for every company number showing their 3 years of financials.

  9. #9
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: copy corresponding sheet from three workbooks to one file

    It might be a while. I am trying to revise it without hard coding the sheet or file names and allow for future additions of years or changes in companies without having to revise the code.

  10. #10
    Forum Contributor
    Join Date
    10-24-2012
    Location
    Chicago
    MS-Off Ver
    O365
    Posts
    311

    Re: copy corresponding sheet from three workbooks to one file

    thanks for the help. I really appreciate it. I started the file from another file I built out that did something similar but different with a summary and detail income statement. It has worked for probably the past 7 years. Now I have a new request for three year views. I altered the code from the other file I have and changed a few things on it. I tested it with two years and it works perfect. Problem is I have three years and I can't figure out what the issue is. I can post the two year version if that helps.

  11. #11
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: copy corresponding sheet from three workbooks to one file

    It is not the best code I have written, but it tested OK and produced the results as I understand that you want them. It was the complexity of getting the company sheets to map to the years sheets that had me stumped for a while. But I finally figured a way to get the count I needed to make sure all the company sheets would result in a new workbook within each year sheet. You can tweak it, revise it, do what you want with it. I'm done. Regards, JLG

    Please Login or Register  to view this content.
    Last edited by JLGWhiz; 09-15-2019 at 07:55 PM.

  12. #12
    Forum Contributor
    Join Date
    10-24-2012
    Location
    Chicago
    MS-Off Ver
    O365
    Posts
    311

    Re: copy corresponding sheet from three workbooks to one file

    thanks. it created a file for every company that had its own tab however all of the files had company 00000 on each tab in every single file. The macro didn't loop to the next tab on the second file and then the third tab on the third file and so on. I will take a look at it. I appreciate the help.

  13. #13
    Forum Contributor
    Join Date
    10-24-2012
    Location
    Chicago
    MS-Off Ver
    O365
    Posts
    311

    Re: copy corresponding sheet from three workbooks to one file

    wait let me check something. I had all the tabs with the data showing 00000. I will test it on the live data now.

  14. #14
    Forum Contributor
    Join Date
    10-24-2012
    Location
    Chicago
    MS-Off Ver
    O365
    Posts
    311

    Re: copy corresponding sheet from three workbooks to one file

    yeah it is almost there but not working. Thanks anyway. it is throwing an error on the first file with the live data saying it can't do that to a merged cell and in that file the first two tabs have the right property tab in it but the third tab for 2018 has the property that is supposed to be in the next file and the workbooks have all of the same tab counts and names. I appreciate the help. It is just frustrating because I have it working for 2 years. the third messes me all up.

  15. #15
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: copy corresponding sheet from three workbooks to one file

    See if this is how you wanted.
    Please Login or Register  to view this content.

  16. #16
    Forum Contributor
    Join Date
    10-24-2012
    Location
    Chicago
    MS-Off Ver
    O365
    Posts
    311

    Re: copy corresponding sheet from three workbooks to one file

    that is exactly what i was looking for. Thank you! the code is much cleaner than the code that I had built in the original file I was using. Thanks for all the help and your time.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] vba to copy a specific sheet from multiple workbooks to summary sheet
    By JEAN1972 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-27-2019, 02:41 PM
  2. Updating a workbooks own copy of data from a master file.
    By kadeo in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-10-2016, 06:48 AM
  3. How to copy a fix range from multiple workbooks to a master file
    By elderL in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-07-2014, 07:32 AM
  4. How to copy a sheet from one workbook as a sheet onto multiple workbooks
    By hld142 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-25-2014, 10:11 AM
  5. Copy sheets from multiply workbooks into one file.
    By meldev in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 07-24-2014, 10:10 AM
  6. Copy / Paste Values Only one sheet into another file from a multi sheet excel file
    By srisri201012 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-27-2013, 12:56 PM
  7. copy worksheets to new workbooks with file name as tab
    By sammar12 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-30-2008, 01:59 AM

Tags for this Thread

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