+ Reply to Thread
Results 1 to 19 of 19

Macro to copy two Sheets matching Source to each workbook in matching Destination Folder

  1. #1
    Forum Contributor
    Join Date
    06-03-2004
    Location
    India
    MS-Off Ver
    2013
    Posts
    205

    Macro to copy two Sheets matching Source to each workbook in matching Destination Folder

    Hello All
    I am using Excel 2010

    I have a many workbooks in Folder C:\Source\Master\

    The workbook has many sheets out of which two Sheets are Dept and Result

    The names of the workbook in C:\Source\Master\ folder are in the following format.

    A number followed by Alpha characters.
    1. Abcd
    2. Efgh
    3. Hij
    4. Klm
    5. Nopq
    6. Rstu
    7. Vx
    8. Wyzab
    9. Cdefghi
    10. Jkl
    ….
    ….
    And many more

    I have different folder eg: C:\Destination\ which has the same naming conventions as shown above.

    This will have one to many workbooks

    I wish to have a macro which should run as follows:

    a) Go to Folder C:\Source\Master\
    b) Open eg: 1. Abcd
    c) Copy only sheets Dept and Result
    d) Save and Close
    e) Go to C:\Destination\
    f) Open the folder matching the name in b) above eg 1. Abcd
    g) Paste the two sheets Dept and Result in the last
    h) Save and close
    i) Do this for all workbooks
    j) Repeat steps b) to h) for others
    k) Continue till all the folders are finished

    There may also other folders and files which I don’t want in C:\Destination\
    Only names matching in both Source and Destination are required

    It should match a numeric followed by Alpha characters and do the copy paste in matching folders

    Hope to get some help

    Thanks in advance

    Rashid Khan

  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: Macro to copy two Sheets matching Source to each workbook in matching Destination Fold

    Hi Rashid, give this a try. I did not test it, so post back with any anomalies that occur.

    Please Login or Register  to view this content.
    Last edited by JLGWhiz; 10-01-2017 at 01:36 PM.

  3. #3
    Forum Contributor
    Join Date
    06-03-2004
    Location
    India
    MS-Off Ver
    2013
    Posts
    205

    Re: Macro to copy two Sheets matching Source to each workbook in matching Destination Fold

    Hello JLGWhiz

    Thanks for the code but it is not doing anything
    I tested the macro thru F8
    It went thru the following lines only before the MsgBox is popping up

    Please Login or Register  to view this content.
    Can you please check?

    Thanks once again.

    Rashid
    Last edited by JBeaucaire; 10-01-2017 at 08:18 PM. Reason: Added missing CODE /CODE tags, please read and follow the Forum Rules, a link to them in blue menu bar above. Thanks!

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

    Re: Macro to copy two Sheets matching Source to each workbook in matching Destination Fold

    What version of Excel are you using? The indication is that it is not finding a file name. Check your path for spelling errors or upper and lower case letter differences.
    Last edited by JLGWhiz; 10-02-2017 at 09:54 AM.

  5. #5
    Forum Contributor
    Join Date
    06-03-2004
    Location
    India
    MS-Off Ver
    2013
    Posts
    205

    Re: Macro to copy two Sheets matching Source to each workbook in matching Destination Fold

    Hi JLGWhiz


    I am using Excel 2010.

    I have tested the path and the spelling..they are all fine.

    I am sending you a sample folder 1. Abcd with two files for sources as well as destination

    Kindly try it on your side please

    Thanks for your time once again.
    Attached Files Attached Files

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

    Re: Macro to copy two Sheets matching Source to each workbook in matching Destination Fold

    I am confused. The original post indicates that 1. Abcd is a file name, but the link in Post #5 shows it as a subdirectory containing two files. The code will not work unless the directory path and the files are properly referenced. So you need to be specific about which is which and then maybe I can do something with the code. As I said, right now I am confused and not sure what to try.

  7. #7
    Forum Contributor
    Join Date
    06-03-2004
    Location
    India
    MS-Off Ver
    2013
    Posts
    205
    Quote Originally Posted by JLGWhiz View Post
    I am confused. The original post indicates that 1. Abcd is a file name, but the link in Post #5 shows it as a subdirectory containing two files. The code will not work unless the directory path and the files are properly referenced. So you need to be specific about which is which and then maybe I can do something with the code. As I said, right now I am confused and not sure what to try.
    Yes you are right.
    My sincere apologies for that.

    1, Abcd etc etc are folders in both places which have workbooks.

    Can you please amend the code to match folder names on Source and Destination and do the copying.

    Thanks and sorry once again for the confusion

    Regards

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

    Re: Macro to copy two Sheets matching Source to each workbook in matching Destination Fold

    Quote Originally Posted by prkhan56 View Post
    Yes you are right.
    My sincere apologies for that.

    1, Abcd etc etc are folders in both places which have workbooks.

    Can you please amend the code to match folder names on Source and Destination and do the copying.

    Thanks and sorry once again for the confusion

    Regards
    If you can give the correct path, workbook and sheet names in a manner that I can understand, then I can fix the code. At present, I feel that it would be futile to try.

  9. #9
    Forum Contributor
    Join Date
    06-03-2004
    Location
    India
    MS-Off Ver
    2013
    Posts
    205

    Re: Macro to copy two Sheets matching Source to each workbook in matching Destination Fold

    Dear JLGWhiz,

    Please see the scenario

    Path 1 - C:\Source\Master\ with following folders as follows

    1. Abcd
    2. Efgh
    3. Hij
    4. Klm
    5. Nopq
    6. Rstu
    7. Vx
    8. Wyzab
    9. Cdefghi
    10. Jkl

    The above folders will each have a workbook which has many sheets names can be any name out of which two Sheet names are Dept and Result. I want the Dept and Result sheets only to be copied to all workbooks in matching folders in Path 2

    Path 2 - C:\Destination\ with following folders as follows

    1. Abcd
    2. Efgh
    3. Hij
    4. Klm
    5. Nopq
    6. Rstu
    7. Vx
    8. Wyzab
    9. Cdefghi
    10. Jkl

    The above folders each will have one to many workbooks which has many sheets where the two Sheets (viz. Dept and Result) from Path 1 should be copied in the last in each workbook


    There may also be other folders and files in Path 2 which I don’t want.


    Only folder names matching in both Path 1 and Path 2 are required for copying

    My objective is to match the folder names in both Path 1 and Path 2 and do the copying from Path 1 to Path 2

    Hope it is clear now

    Thanks for all your time and help

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

    Re: Macro to copy two Sheets matching Source to each workbook in matching Destination Fold

    See if this will work.
    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    06-03-2004
    Location
    India
    MS-Off Ver
    2013
    Posts
    205

    Re: Macro to copy two Sheets matching Source to each workbook in matching Destination Fold

    Hi
    When I run the code it says compile error: For without Next

    Thanks and regards

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

    Re: Macro to copy two Sheets matching Source to each workbook in matching Destination Fold

    put 'Next' where shown below

    Please Login or Register  to view this content.

  13. #13
    Forum Contributor
    Join Date
    06-03-2004
    Location
    India
    MS-Off Ver
    2013
    Posts
    205

    Re: Macro to copy two Sheets matching Source to each workbook in matching Destination Fold

    Hi JLGWhiz,
    I changed the code.

    It gave me error - see Macro Error 1004
    and highlighted the code - see Error Code

    My path is correct - see Path

    Hope you can please see into this problem
    Thanks for all your time and help
    Attached Images Attached Images

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

    Re: Macro to copy two Sheets matching Source to each workbook in matching Destination Fold

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

  15. #15
    Forum Contributor
    Join Date
    06-03-2004
    Location
    India
    MS-Off Ver
    2013
    Posts
    205

    Re: Macro to copy two Sheets matching Source to each workbook in matching Destination Fold

    Hi JLGWhiz,
    I changed the code.

    Now two issues are coming.

    See the screenshot 1004 Error.

    Your code is looking for the same file name where as I explained in the scenario it is not
    the same name for workbooks both in Path 1 and Path 2. The workbook names are different in both Paths
    I want to have the folder names matching for copying purposes. Hope it is clear now.

    For sake of testing I made the filenames same in both Path 1 and Path 2 then other error comes up

    See the screenshot - Error code if same file names

    I think you have to tweak the code further please

    I am also enclosing 1. Abcd folder both (Source and Destination) for testing purpose

    Thanks once again for all your time and help
    Attached Images Attached Images
    Attached Files Attached Files

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

    Re: Macro to copy two Sheets matching Source to each workbook in matching Destination Fold

    This should get the workbook from the second path instead of the first;
    Please Login or Register  to view this content.
    Last edited by JLGWhiz; 10-07-2017 at 12:29 PM.

  17. #17
    Forum Contributor
    Join Date
    06-03-2004
    Location
    India
    MS-Off Ver
    2013
    Posts
    205

    Re: Macro to copy two Sheets matching Source to each workbook in matching Destination Fold

    Hello JLGWhiz

    I tested your code it works fine if there is only one workbook in the Path 2 - Destination folder.
    I have mentioned in the Scenario that I want the Dept and Result sheets to be copied in all the workbooks in the Destination Folder matching folder names of Path 1 and Path 2.

    I have tested it with single workbook in each folder of Destination then the code works fine but when there are more than one workbook in the Destination folder then the macro gives sheet object error - see the pic and highlights the code (see pic).

    Can you please amend it accordingly?


    Also one small favor please if you could add an error trap as while testing I noticed that if there is no workbook in the Source then it pops up with the error 'File not found' window.

    Thanks a lot for all your time and help
    Attached Images Attached Images

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

    Re: Macro to copy two Sheets matching Source to each workbook in matching Destination Fold

    It is obvious that I don't have a good understanding of how your workbooks are set up within each directory, and I have already spent several hours on this, so I am going to pass on this thread. Maybe someone else will pick up on it and give it a try. Sorry I couldn't resolve the issue for you.
    Regards, JLG

  19. #19
    Forum Contributor
    Join Date
    06-03-2004
    Location
    India
    MS-Off Ver
    2013
    Posts
    205

    Re: Macro to copy two Sheets matching Source to each workbook in matching Destination Fold

    Hello JLG,

    Thanks for all the time and help.

    I hope someone will come up with the final solution.

    Regards
    Rashid

+ 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] Need a Macro for two sheets, matching and looping, to copy data over.
    By jana_wire in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-12-2017, 03:45 PM
  2. [SOLVED] VBA: Looking for a Macro to Match and Copy from Source to Destination Sheet
    By AliGW in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-01-2016, 03:16 AM
  3. [SOLVED] Copy certain files from Source folder to Destination folder
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-16-2015, 08:56 PM
  4. VBA Copy Paste Cuts From Source, Sends to Ether - Not To Destination Workbook
    By jcaynes in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-23-2014, 11:05 AM
  5. Macro to copy entire columns with matching dates from another workbook
    By rocksan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-28-2012, 01:23 PM
  6. how to copy succeeding rows from source workbook to destination w.
    By chris in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-08-2005, 10:06 AM

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