+ Reply to Thread
Results 1 to 6 of 6

Need help completing a formula to find and substitute a character in a text string...

  1. #1
    Registered User
    Join Date
    08-06-2014
    Location
    Tucson, Arizona
    MS-Off Ver
    XP
    Posts
    43

    Need help completing a formula to find and substitute a character in a text string...

    I have a formula which does the following: it finds the last comma followed by a space (, ) in a text string and replaces it with the word "and", and if there isn't a comma followed by a space, it leaves the text string as is.

    For example, I have the following words "dog, cat, boy, girl", and this formula changes the text to "dog, cat, boy and girl" as long as there is at least one comma followed by a space.

    That formula looks like this: =IF(LEN(B123)-LEN(SUBSTITUTE(B123,",", ""))>=1,IFERROR(TRIM(SUBSTITUTE($B123,","," and",LEN($B123)-LEN(SUBSTITUTE($B123,",","")))),$B123),TRIM(B123))

    Here is the addition I want to make:

    If there is exactly one comma followed by a space in a text string, it finds that comma and space and replaces it with the word "and". That formula would look like this:

    =IF(LEN(B123)-LEN(SUBSTITUTE(B123,",", ""))=1,IFERROR(TRIM(SUBSTITUTE($B123,","," and",LEN($B123)-LEN(SUBSTITUTE($B123,",","")))),$B123),TRIM(B123))

    However, I'd like to add to that if there are TWO or greater commas followed by a space, it will find the last comma with a space and replace it with ", and" [a comma, space and the word "and"].

    The other formula I have that I need to make this adjustment to which basically does the same thing is this: =IFERROR(SUBSTITUTE(B123,", "," and ",LEN(B123)-LEN(SUBSTITUTE(B123,", "," "))),B123)

    But I have the same problem... If there are more than two commas followed by a space, I need it to replace the last ", " with ", and". If there is only one comma followed by a space, it replaces the ", " with " and". And if there are no commas followed by a space, it leaves the text as it is.

    Thank you!
    Last edited by danielneedssomehelp; 08-27-2014 at 11:35 AM.

  2. #2
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Need help completing a formula to find and substitute a character in a text string...

    assuming cell a1

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    its using the instance num of substitute to determine where the " and" is placed

    Formula: copy to clipboard
    Please Login or Register  to view this content.

    this will give you the instance num
    Last edited by humdingaling; 08-26-2014 at 09:21 PM.
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  3. #3
    Valued Forum Contributor
    Join Date
    12-02-2012
    Location
    Melbourne, VIC
    MS-Off Ver
    Excel 2016
    Posts
    750

    Re: Need help completing a formula to find and substitute a character in a text string...

    does this formula work for you:
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    HTH!

  4. #4
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: Need help completing a formula to find and substitute a character in a text string...

    Another......
    Assuming your string is in A1, then

    Please Login or Register  to view this content.
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  5. #5
    Registered User
    Join Date
    08-06-2014
    Location
    Tucson, Arizona
    MS-Off Ver
    XP
    Posts
    43

    Re: Need help completing a formula to find and substitute a character in a text string...

    Yes, jewelsharma, that was it. Brilliant! It works perfectly! Thank you VERY much!

  6. #6
    Registered User
    Join Date
    08-06-2014
    Location
    Tucson, Arizona
    MS-Off Ver
    XP
    Posts
    43

    Re: Need help completing a formula to find and substitute a character in a text string...

    Thank you!

+ 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] Find text left or right of a specific character in a text string
    By Locopete99 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 05-14-2014, 05:55 AM
  2. Replies: 4
    Last Post: 04-06-2014, 01:21 PM
  3. Find last repeated character position from a text string
    By krjoshi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-17-2011, 02:14 PM
  4. Text parsing, find string after : character
    By proepert in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-14-2010, 10:46 PM
  5. [SOLVED] Find last occurance of character in text string
    By JDay01 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-14-2006, 12:35 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