+ Reply to Thread
Results 1 to 16 of 16

Need formula that finds the last ", " in a text string & substitutes it w/ the word "and"

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

    Need formula that finds the last ", " in a text string & substitutes it w/ the word "and"

    Hello, I have a text string that might have any number of words in it, all separated by a comma and a space. So one text string might be "dog, cat, boy, girl" and another one might be "dog, cat". I need a formula that will find the last comma and space, and substitute that with the word "and". So instead of the text string stating "dog, cat, boy, girl" it will read "dog, cat, boy and girl".

    Any help would be much appreciated!
    Last edited by danielneedssomehelp; 08-21-2014 at 10:08 PM.

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Need formula that finds the last ", " in a text string & substitutes it w/ the word "a

    Put your text in A1

    Please Login or Register  to view this content.

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

    Re: Need formula that finds the last ", " in a text string & substitutes it w/ the word "a

    Oh shoot, forgot one thing... if there is no ", " in the text string, then it leaves the text as it was. Sorry!

  4. #4
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Need formula that finds the last ", " in a text string & substitutes it w/ the word "a

    Try this...

    =IF(COUNT(FIND(",",A1)),SUBSTITUTE(A1,","," and",LEN(A1)-LEN(SUBSTITUTE(A1,",",""))),T(A1))
    Biff
    Microsoft MVP Excel
    Keep It Simple Stupid

    Let's Go Pens. We Want The Cup.

  5. #5
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: Need formula that finds the last ", " in a text string & substitutes it w/ the word "a

    Try this..
    Inspired from TONY..

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


    Don't forget to click *

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

    Re: Need formula that finds the last ", " in a text string & substitutes it w/ the word "a

    Thanks Tony, the only problem with that formula is that it only finds "," and not ", ". So I need it to find and substitute the comma with the space after it.

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

    Re: Need formula that finds the last ", " in a text string & substitutes it w/ the word "a

    Ding, ding, ding!!! That did it! Thank you Vikas, Tony and mehmetcik!!! I'll big up you guys!

  8. #8
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Need formula that finds the last ", " in a text string & substitutes it w/ the word "a

    Quote Originally Posted by Vikas_Gautam View Post

    =SUBSTITUTE(A1,", "," and ",LEN(A1)-LEN(SUBSTITUTE(A1,", "," ")))
    If there's no comma that returns a VALUE! error.

  9. #9
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Need formula that finds the last ", " in a text string & substitutes it w/ the word "a

    Quote Originally Posted by danielneedssomehelp View Post
    Thanks Tony, the only problem with that formula is that it only finds "," and not ", ". So I need it to find and substitute the comma with the space after it.
    Well, based on the samples you posted it didn't matter.

    I see you're satisfied so that's all that matters!

  10. #10
    Valued Forum Contributor
    Join Date
    04-22-2014
    Location
    Auckland, New Zealand
    MS-Off Ver
    Office 365 (work) and Excel 2013 (home)
    Posts
    1,167

    Re: Need formula that finds the last ", " in a text string & substitutes it w/ the word "a

    Quote Originally Posted by danielneedssomehelp View Post
    Thanks Tony, the only problem with that formula is that it only finds "," and not ", ". So I need it to find and substitute the comma with the space after it.
    Are there instances of comma's without the space? If not, it should make no difference.

    "dog, cat" with the "," replaced with " and" will be 'dog and cat".

    "dog, cat" with the ", " replaced with " and " will be 'dog and cat".
    Please add reputation by clicking on the * if I have helped.
    Please mark the thread SOLVED if your issue has been resolved.
    Thanks, Glenn.

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

    Re: Need formula that finds the last ", " in a text string & substitutes it w/ the word "a

    Tony, you caught that!!! Yes, there are instances where there is no comma with a space, and I get an error. Can you help??

  12. #12
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: Need formula that finds the last ", " in a text string & substitutes it w/ the word "a

    Try this..

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


    Don't forget to click *, again
    Last edited by JBeaucaire; 08-29-2014 at 01:15 PM.

  13. #13
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Need formula that finds the last ", " in a text string & substitutes it w/ the word "a

    Quote Originally Posted by danielneedssomehelp View Post
    Tony, you caught that!!! Yes, there are instances where there is no comma with a space, and I get an error. Can you help??
    Can you post SEVERAL representative examples?

    Based on your explanation in post #1 the formula in post #4 does what you asked for.

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

    Re: Need formula that finds the last ", " in a text string & substitutes it w/ the word "a

    Yes! That is it!!! Thank you Vikas- works perfect! Thank you to all!

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

    Re: Need formula that finds the last ", " in a text string & substitutes it w/ the word "a

    My fault for not adding that last detail, but you caught it Tony, so thank you!!!! I got it sorted, but couldn't have done it without you!

  16. #16
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Need formula that finds the last ", " in a text string & substitutes it w/ the word "a

    Good deal. Thanks for the feedback!

+ 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. Replies: 1
    Last Post: 08-15-2014, 06:00 AM
  2. Replies: 0
    Last Post: 03-21-2014, 09:58 AM
  3. Replies: 5
    Last Post: 01-23-2014, 11:02 AM
  4. [SOLVED] How to Count number of "Error" and "OK" after the word "Instrument" found in table row
    By eltonlaw in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-17-2012, 06:26 AM
  5. Replies: 5
    Last Post: 10-12-2010, 06:46 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