+ Reply to Thread
Results 1 to 4 of 4

how to replace last comma in string with "and"

  1. #1
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    how to replace last comma in string with "and"

    String example: "cats, dogs, birds, chickens, cows"
    I am looking for a VBA script that replaces the last comma in that string:
    Output string: "cats, dogs, birds, chickens and cows"

    Assume that the number of characters before and after the last comma changes from string to string.
    Also the number of commas before the last comma changes from string to string.

    How can I script that?

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,944

    Re: how to replace last comma in string with "and"

    Try this...
    =SUBSTITUTE(H16,","," and ",LEN(H16)-LEN(SUBSTITUTE(H16,",","")))

    Change H16 as needed
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: how to replace last comma in string with "and"

    thanks. With some trial and error I was able to convert it to the VBA script.

    Please Login or Register  to view this content.

  4. #4
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,944

    Re: how to replace last comma in string with "and"

    Happy to help and 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. [SOLVED] replace "," comma with "." period
    By k1dr0ck in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 10-11-2023, 05:03 PM
  2. Replies: 3
    Last Post: 05-02-2018, 08:03 AM
  3. VBA to replace everything after LAST instace of "+" in string
    By Josiah in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-04-2017, 02:26 PM
  4. Replies: 1
    Last Post: 08-15-2014, 06:00 AM
  5. [SOLVED] VBA help needed to remove all "/" then replace with "-" from cell "B3"and "B5"
    By krjoshi in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-17-2014, 02:11 PM
  6. Separate cell which have "Comma" into different row & multiple each row "n" times
    By Arun ebl in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-09-2013, 11:22 PM
  7. [SOLVED] Can we use "*" to replace string in VBA excel macros ?
    By Tomek in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-13-2005, 02:06 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