+ Reply to Thread
Results 1 to 17 of 17

Replacing multiple values across multiple consecutive workbooks in different directories

  1. #1
    Registered User
    Join Date
    05-15-2017
    Location
    La Jolla, CA
    MS-Off Ver
    2016
    Posts
    9

    Question Replacing multiple values across multiple consecutive workbooks in different directories

    Hello All,

    I'm trying to replace one cell with another across multiple workbooks in Excel 2016. The details are:

    1. There are 10 files in one directory and correspond in order to 10 other files in another directory (i.e. the first file in each corresponds to the same client, but the workbooks have different names).
    2. I would like to replace multiple cells from the first group of files to multiple cells in the corresponding file in the other directory. I.e cell A3 to cell B8, cell D4 to cell C2.
    3. I would like to do this to all files in the directories at once and was hoping that because they are in 1 to 1 order within each directory it might help.
    4. Lastly, some of the values I'm copying are actually formulas that I would like to convert to values when they are copies between files.

    I hope I was clear. Thanks in advance for any help!

  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: Replacing multiple values across multiple consecutive workbooks in different directori

    1) Open a blank workbook.
    2) Turn on the macro recorder
    3) Open one sample target workbook
    4) Open the matching source workbook
    5) Copy a cell from the source workbook and paste it into the proper place in the target
    6) Save and close both workbooks.
    7) Turn off the macro recorder.
    8) Press Alt-F11 to open the VBEditor and locate the recorded macro.

    Paste that macro here. From that, I can tweak that into a form you should be able to adjust as needed to do the same process on any number of cells for the matching workbooks.
    _________________
    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
    05-15-2017
    Location
    La Jolla, CA
    MS-Off Ver
    2016
    Posts
    9

    Re: Replacing multiple values across multiple consecutive workbooks in different directori

    Thanks for the help. Here is the requested code.

    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 05-20-2017 at 08:47 AM.

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

    Re: Replacing multiple values across multiple consecutive workbooks in different directori

    Sorry for the delay, I've been away.

    A few things:

    1) I notice the two folder paths are
    "C:\Users\ccribari\Desktop\Exceltest\"
    "C:\Users\ccribari\Desktop\Exceltest\testsub\"


    Those will be easy to spot at the top of the macro I'm writing, you will edit those folder paths the ones you really need.

    2) The 1-1 names between the books are not really 1-1
    "Book1.xlsx"
    Book1_sub.xlsx"


    So, is the exact scenario we have to duplicate? Look for any filename in the FIRST path, then find the similar name in SECOND path with _Sub added into the filename?

    3) Oddly, the macro above did not record you clicking on any specific cells. We'll use the ones from your post #1

    4) The macro did not record you selecting any specific sheets within the workbooks, so we will use the FIRST sheet found in both of them, ok?

  5. #5
    Registered User
    Join Date
    05-15-2017
    Location
    La Jolla, CA
    MS-Off Ver
    2016
    Posts
    9

    Re: Replacing multiple values across multiple consecutive workbooks in different directori

    Perfect. Also, just a point to keep in mind, I would want to copy multiple cells. I.e. D1 in file1 to C3 file1_sub and at the same time A3 file1 to B7 file1_sub, etc. Not sure if I made that clear, but your assumptions above are what I'm looking for. Also, the files will be 1:1 match in their order in each folder, i.e. the first file in the main folder corresponds to the first file in the _sub folder.

    Thanks!
    Last edited by Crobaro; 05-23-2017 at 02:33 PM.

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

    Re: Replacing multiple values across multiple consecutive workbooks in different directori

    Orders of file won't work. We must go by filenames.

    Please confirm, whatever file we locate with any random name in path 1, we will add _sub to the name and that WILL be the source file from path 2?


    Cat.xlsx > cat_sub.xlsx
    Book22.xls > Book22_sub.xls
    Data.xlsb > Data_sub.xlsb

  7. #7
    Registered User
    Join Date
    05-15-2017
    Location
    La Jolla, CA
    MS-Off Ver
    2016
    Posts
    9

    Re: Replacing multiple values across multiple consecutive workbooks in different directori

    Thanks for this. Quick question, how do I define the cells whose data will be put in to other cells from book1 to book1_sub (i.e. C3 in book1 to D4 in book1_sub). As well, does this go on down the line copying defined cell data from one workbook to another for book2 to book2_sub, book3 to book3_sub, etc. ?

  8. #8
    Registered User
    Join Date
    05-15-2017
    Location
    La Jolla, CA
    MS-Off Ver
    2016
    Posts
    9

    Re: Replacing multiple values across multiple consecutive workbooks in different directori

    "Cat.xlsx > cat_sub.xlsx
    Book22.xls > Book22_sub.xls
    Data.xlsb > Data_sub.xlsb"

    That would work fine as I can change the naming conventions as needed, thanks!

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

    Re: Replacing multiple values across multiple consecutive workbooks in different directori

    OK, if you confirm that will work, then I will put it together.

    The code will be pretty obvious what it's doing and where you will edit the mapping of source cells > target cells.

  10. #10
    Registered User
    Join Date
    05-15-2017
    Location
    La Jolla, CA
    MS-Off Ver
    2016
    Posts
    9

    Re: Replacing multiple values across multiple consecutive workbooks in different directori

    Confirmed and looking forward to seeing it, thanks!

  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: Replacing multiple values across multiple consecutive workbooks in different directori

    This is what I have put together. It will fall to you to test it....

    Read it over an let me know if any of it is unclear before you try it out.
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    05-15-2017
    Location
    La Jolla, CA
    MS-Off Ver
    2016
    Posts
    9

    Re: Replacing multiple values across multiple consecutive workbooks in different directori

    May be a silly question, this works for the first workbook. How do I make it do all workbooks automatically in these folders?

    Thanks again!

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

    Re: Replacing multiple values across multiple consecutive workbooks in different directori

    It's a looping macro, it should work for every pair of matching workbooks.

    fPATH1 = target workbooks
    fPATH2 = source workbooks (with _sub added to name that matches to target workbooks)


    The only concern I have is that the fNAME1 code does see more than one file. So use F8 to step through this macro one line at a time. When you get to this line of code
    Please Login or Register  to view this content.
    pause for a moment once you pass it, hover your mouse over the fNAME1 to see what the filename it found is... is that different each time through the loop, or does it keep finding the same file over and over?

  14. #14
    Registered User
    Join Date
    05-15-2017
    Location
    La Jolla, CA
    MS-Off Ver
    2016
    Posts
    9

    Re: Replacing multiple values across multiple consecutive workbooks in different directori

    fName1 =Book1.xlsx, then on second loop does not see any more workbooks (there are 6 in each folder per the naming conventions). The second time through the loop, fname = "".

    Does that help?

    Thanks!

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

    Re: Replacing multiple values across multiple consecutive workbooks in different directori

    Ok, I think it is the second use of the DIR() function to test wb2's existence is messing up the natural behavior of a single instance of DIR(). So I've removed the second instance. Try this:
    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    05-15-2017
    Location
    La Jolla, CA
    MS-Off Ver
    2016
    Posts
    9

    Re: Replacing multiple values across multiple consecutive workbooks in different directori

    I think that little tear of joy is on its way. This is perfect, thank you JBeaucaire!

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

    Re: Replacing multiple values across multiple consecutive workbooks in different directori

    Awesome! If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ 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. Need help replacing values in multiple cells
    By philwalker87 in forum Excel General
    Replies: 10
    Last Post: 03-10-2017, 07:48 PM
  2. Replies: 1
    Last Post: 06-20-2015, 04:31 PM
  3. Multiple directories based on cell values
    By SoothSailor in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-23-2014, 09:19 AM
  4. [SOLVED] Changing workbooks and directories based on cell values
    By DMumme in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-12-2014, 01:43 AM
  5. Replies: 20
    Last Post: 03-13-2013, 04:15 PM
  6. Dynamic Linking of Workbooks in Multiple Directories
    By Wyer in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-14-2009, 04:19 AM
  7. Search text in multiple files in multiple directories
    By Andrew in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-31-2006, 10:48 PM

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