+ Reply to Thread
Results 1 to 9 of 9

Help - Replace a string in a whole workbook inside formulas

  1. #1
    Registered User
    Join Date
    05-29-2020
    Location
    Chisinau
    MS-Off Ver
    2019
    Posts
    39

    Question Help - Replace a string in a whole workbook inside formulas

    Please help with a VBA code for replacing a string (for example ABC123) with another string (ABC456) in a whole workbook, however inside formulas (case sensitive).

  2. #2
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,210

    Re: Help - Replace a string in a whole workbook inside formulas

    Perhaps...
    Please Login or Register  to view this content.
    Last edited by sintek; 05-29-2020 at 07:22 AM.
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  3. #3
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,704

    Re: Help - Replace a string in a whole workbook inside formulas

    @sintek - if you go that route, (assuming constants ok), you can avoid the ws iteration by specifying workbook level replace

    ignoring Constants, case sensitive etc... perhaps:

    Please Login or Register  to view this content.

  4. #4
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,908

    Re: Help - Replace a string in a whole workbook inside formulas

    Is the string part of a link to another workbook? If so, it would likely be faster to alter the link source path, rather than all formulas individually.
    Rory

  5. #5
    Registered User
    Join Date
    05-29-2020
    Location
    Chisinau
    MS-Off Ver
    2019
    Posts
    39

    Re: Help - Replace a string in a whole workbook inside formulas

    Thank you, sintek and XLent! I don't know why both codes don't work, but I assume it is the way I do "the job".
    I keep the macros in another workbook (xlsm). I open it...
    Then I open also the target file (also xlsm that has its own macros) and run the macros stored in the first workbook inside the target.
    I have many target files to do the change.

    Am I doing wrong for keeping "my tools" inside other file to avoid messing up the target files?

    I actually need to do 3 tasks on the target workbook:

    1) Unprotect all worksheets with macro nr. 1
    2) Replace a part of a formula inside few worksheets (like 7 worksheets)... with macro nr. 2.... but I think it is easier to tell it to replace in the whole workbook...
    3) Protect all worksheets back with a password with macro nr. 3

    I have the codes for tasks nr. 1 and 3 - and they do what it is supposed to do.
    The task nr 2 is the one that does not work yet. Thank you rorya for asking... maybe this is the reason the codes do not work.
    Last edited by Alexander2020; 05-29-2020 at 09:58 AM.

  6. #6
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,704

    Re: Help - Replace a string in a whole workbook inside formulas

    both codes use ThisWorkbook, ie. the wb in which the code resides, rather than ActiveWorkbook, the wb active at the point the code is executed
    alternatively, depending on how you're invoking, you can pass the workbook reference to these routines.

    I would be mindful of rorya's comments however, if this replace is to updating a collection of external links.

  7. #7
    Registered User
    Join Date
    05-29-2020
    Location
    Chisinau
    MS-Off Ver
    2019
    Posts
    39

    Re: Help - Replace a string in a whole workbook inside formulas

    Changing ThisWorkbook into ActiveWorkbook does not solve my problem, but only partially. Then the codes above work OK for replacing a value inside the cells on Active Workbook, but not inside the formulas.

    I have seen in other places on the net they use also the function FIND and LookIn:=xlFormulas to specify where to look for.
    The code should maybe perform use of the Find function before the Replace?

  8. #8
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,908

    Re: Help - Replace a string in a whole workbook inside formulas

    Quote Originally Posted by Alexander2020 View Post
    Thank you rorya for asking... maybe this is the reason the codes do not work.
    Hard to say, as you haven't actually answered...

  9. #9
    Registered User
    Join Date
    05-29-2020
    Location
    Chisinau
    MS-Off Ver
    2019
    Posts
    39

    Re: Help - Replace a string in a whole workbook inside formulas

    Quote Originally Posted by rorya View Post
    Hard to say, as you haven't actually answered...
    I didn't answer to your question cause I didn't understand it. As I explained in other post, I have one workbook with the macro that should make the change inside a formula located in other workbook. I do not want to add this macro inside the target workbook, just run it when both workbooks are opened (active). I proceed like that with many other macros, cause I do not want to add something inside the target workbooks (I have many workbooks that need the change and I send them to other people). If this is what it means that "the string is part of a link to another workbook"... then the answer is "Yes".

    Meantime one friend sent me this code:

    Please Login or Register  to view this content.
    I should mention that I do not know how to adapt this code to my situation. The formula that need to be changed is located in the cell "C8" inside any of the 7 worksheets of my target workbook. And it goes down for many cells only in the column "C". The real formula that need to change looks like that (I didn't want to complicate the things before): =IF(B8<>"";VLOOKUP(B8;N_Servicii!$A$4:$K$5003;9;FALSE);"")
    It looks inside a list with name of streets, while it should look at a list with name of cities. The difference is simple. I need to replace "9" with "11".
    My first approach was to use a macro that search inside the whole target workbook for the string "Servicii!$A$4:$K$5003;9" and replace it with "Servicii!$A$4:$K$5003;11".

    I was not able to make the macro from my friend work for me, but I was able to make myself a code that works, but instead of searching inside the whole workbook, it looks specifically at the location of the formulas that need to be changed. This is my code (of course, inspired by search on the net):

    Please Login or Register  to view this content.
    If you ask why I put this inside my code: "Worksheets("Alpha").Activate"... My code without it was working only if I was selecting the Alpha worksheet before running the macro. Why? Because before the 7 worksheets (my targets) I have other three worksheets... lets call them "Sheet1", "Sheet2" and "Sheet3" that has nothing to do with my problem. Excel by default selects the activated sheet to be "Sheet1". I do not know why or how... but telling that worksheet "Alpha" is the first to be activated, then all goes well. Otherwise it replaces wrong cells.
    Also... about my code... I put the range of formulas to be changed between cells C8:C100. I never get so many cells to modify in the "C" column, but I didn't know how to tell Excel to do the modification only where is a formula inside the cell and stop when the cell is empty.
    Of course I have no idea how to make the code to look better and I am certain in can be made shorter instead of repeating.

    What is important for me is that I have solved my problem. If you know how to improve my code or my friend's code (that search inside the whole workbook) then I thank you in advance and try it.
    Last edited by Alexander2020; 06-02-2020 at 04:14 PM.

+ 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 replace text string containing "&" in formulas
    By modytrane in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-27-2019, 01:33 PM
  2. [SOLVED] replace string in code in module1 when opening workbook
    By k1dr0ck in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-07-2018, 10:57 PM
  3. [SOLVED] Macro to change 1) part of string, 2) the order of characters inside a string, 3) format
    By Karl Gustaf Karsten in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-16-2015, 11:51 AM
  4. [SOLVED] VBA find and replace inside array formulas? Possible?
    By phpolicylady in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-01-2015, 12:52 PM
  5. Replace External Links with Values inside Formulas
    By k64 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-22-2015, 03:01 PM
  6. [SOLVED] Find and Replace for Inside Formulas
    By VICEROY in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-13-2013, 01:03 AM
  7. Find/Replace inside text string using a formula
    By SEMMatt in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-21-2013, 04:24 PM

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