+ Reply to Thread
Results 1 to 16 of 16

Need a function to replace text that meets condition located in a specific area of string

  1. #1
    Registered User
    Join Date
    03-23-2012
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    10

    Need a function to replace text that meets condition located in a specific area of string

    Hey there,

    I'm trying to figure out how to create a function that replaces the sequence " CR " with " CIR ". Here's the catch - there are a number of " CR " 's that I want to preserve; it is only the " CR " 's whose first character is located 6, 7, or 8 places from the right that I want to replace.

    I've managed to replace all the " CR " 's that are on the end using the Right function:
    =IF(RIGHT(H2,4)=" CR ",REPLACE(H2,SEARCH(RIGHT(H2,4),H2,4),5," CIR "),H2)

    Now I would like to do this with the Mid function for those located 6,7,and 8 places from the right. However, they haven't worked as planned:
    =IF(MID(H2,LEN(H2)-6,4)=" CR ",REPLACE(H2,SEARCH(MID(H2,LEN(H2)-6,4),5," CIR ")H2)

    Can anybody help me on this?

    Thanks!

  2. #2
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Need a function to replace text that meets condition located in a specific area of str

    Please Login or Register  to view this content.
    CTRL + H

    find CR

    replace CIR

    replace all
    Notice my main language is not English.

    I appreciate it, if you reply on my solution.

    If you are satisfied with the solution, please mark the question solved.

    You can add reputation by clicking on the star * add reputation.

  3. #3
    Valued Forum Contributor
    Join Date
    03-16-2012
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    992

    Re: Need a function to replace text that meets condition located in a specific area of str

    Just for clarification: Might " CR " be present more than once in each string?
    Sincerely
    S?ren Larsen

    "Give a man a fish, and you'll feed him for a day. Give a man a fishing rod, and he'll steal your yacht!"

  4. #4
    Registered User
    Join Date
    03-23-2012
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Need a function to replace text that meets condition located in a specific area of str

    Soren,

    It's unlikely, but it could happen.
    I've found a workaround by splitting the column by position, but would still be interested to know if anybody has any ideas. Oeldere, I don't think that's quite what I'm looking for. Thanks though.

  5. #5
    Valued Forum Contributor
    Join Date
    03-16-2012
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    992

    Re: Need a function to replace text that meets condition located in a specific area of str

    If there is only 1 " CR " in each string, then you could probably use something like this:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    If there are mulitple " CR "s and you only want replace the ones on position 6, 7 and 8, and the length of the string varies, then you'll need a macro solution i think.
    Last edited by Søren Larsen; 07-03-2012 at 04:06 PM. Reason: Elaboration

  6. #6
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Need a function to replace text that meets condition located in a specific area of str

    Please Login or Register  to view this content.
    You think, but have you tried?

  7. #7
    Registered User
    Join Date
    03-23-2012
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Need a function to replace text that meets condition located in a specific area of str

    Soren,

    Unfortunately that did not work. It changed all the " CR " 's, not just the ones positioned 6,7,and 8 places from the right. Additionally, some simply generated a "FALSE".

  8. #8
    Registered User
    Join Date
    03-23-2012
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Need a function to replace text that meets condition located in a specific area of str

    Oeldere, that would replace all " CR "s. I want to only replace those that are positioned 6, 7, and 8 places from the right.

  9. #9
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Need a function to replace text that meets condition located in a specific area of str

    I received an answer on my question
    Last edited by oeldere; 07-03-2012 at 04:15 PM. Reason: I received.

  10. #10
    Valued Forum Contributor
    Join Date
    03-16-2012
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    992

    Re: Need a function to replace text that meets condition located in a specific area of str

    Quote Originally Posted by bauman312 View Post
    Soren,

    Unfortunately that did not work. It changed all the " CR " 's, not just the ones positioned 6,7,and 8 places from the right. Additionally, some simply generated a "FALSE".
    Yes, firstly I forgot something; change it to this:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    And with regards to it changing too many instances, then yes, that will happen if you have multiple " CR "s. Notice I edited my post #5. Could you give an example of strings containing only 1 " CR " and strings containing multiple?

  11. #11
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Need a function to replace text that meets condition located in a specific area of str

    Please Login or Register  to view this content.
    And even better, give also the wanted result.

  12. #12
    Valued Forum Contributor
    Join Date
    03-16-2012
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    992

    Re: Need a function to replace text that meets condition located in a specific area of str

    Quote Originally Posted by bauman312 View Post
    Oeldere, that would replace all " CR "s. I want to only replace those that are positioned 6, 7, and 8 places from the right.
    I only just realised that it was those positioned from the right. In that case the formula should be like so:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    03-23-2012
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Need a function to replace text that meets condition located in a specific area of str

    Perfect! That's exactly what I need. Out of curiosity, how come I can't add additional arguments to your formula? Say I wanted to apply the same logic to those " CR " s that are positioned 5 places from the right. When I try to do so, it says that there are too many arguments. Why is that so?

  14. #14
    Valued Forum Contributor
    Join Date
    03-16-2012
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    992

    Re: Need a function to replace text that meets condition located in a specific area of str

    Then you've probably missed a "(", ")" or a "," or misplaced something. In the formula below I've added an extra argument and highlighted the changes in red:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    03-23-2012
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Need a function to replace text that meets condition located in a specific area of str

    Great, thanks Soren.

  16. #16
    Valued Forum Contributor
    Join Date
    03-16-2012
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    992

    Re: Need a function to replace text that meets condition located in a specific area of str

    You're welcome; remember to mark your thread as solved...

+ 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