+ Reply to Thread
Results 1 to 7 of 7

Find & Replace All Occurances After The First One In A String

  1. #1
    Registered User
    Join Date
    01-10-2011
    Location
    Oakland, USA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Find & Replace All Occurances After The First One In A String

    In Excel, I often have lists like this:

    1815 Juniper Street - Mary - Janice - Ming
    1754 Maple street - Richard - William
    445 8th Avenue, Richmond - Dana - Stephanie - Gloria - Andrea

    The format is basically Address - Name - Name

    I'm trying to build a macro that would allow me to select a few cells on my worksheet, and reformat the strings so they look like this:

    1815 Juniper Street - Mary, Janice, Ming
    1754 Maple street - Richard, William
    445 8th Avenue, Richmond - Dana, Stephanie, Gloria, Andrea

    I want the format to be Address - Name, Name. That means I want to leave the first occurrence of the dash, but find every subsequent occurrence of the dash and replace it with a comma.

    I have some basic macro experience, and I have a feeling that Instr will be involved here, but I don't really know how to do it. Any suggestions, links or ideas would be appreciated.

    Thanks,
    k8
    Last edited by k8_dog; 01-11-2011 at 09:02 PM.

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,607

    Re: Find & Replace All Occurances After The First One In A String

    Hi.. try this:

    =SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"-",CHAR(147),1),"-",","),CHAR(147),"-")

  3. #3
    Registered User
    Join Date
    01-10-2011
    Location
    Oakland, USA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Find & Replace All Occurances After The First One In A String

    Hi Zbor,
    Thanks for this quick reply. However, I am really looking for a way to do this in VBA with a macro. I want to select a few cells and do the transformation rather than paste in the formula. Any ideas?
    -K8

  4. #4
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Find & Replace All Occurances After The First One In A String

    see attachment, run macro "test"
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    01-10-2011
    Location
    Oakland, USA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Find & Replace All Occurances After The First One In A String

    Hi,
    Thanks, watersev, for posting this. When I run the macro, it replaces the correct dashes with commas, but the corrected cells are pasted one column over to the right from the original, and always in starting at row 1. I would like the changes to occur in place. From what you supplied, I can't figure out why this is happening or how to fix it. Can anybody help?

    Here's what watersev posted.

    Please Login or Register  to view this content.
    Last edited by k8_dog; 01-11-2011 at 05:56 PM.

  6. #6
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Find & Replace All Occurances After The First One In A String

    Please use tags when you quote code, the following line will make change in place:

    Please Login or Register  to view this content.
    you need just remove offset(,1)

  7. #7
    Registered User
    Join Date
    01-10-2011
    Location
    Oakland, USA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Find & Replace All Occurances After The First One In A String

    Hi watersev,
    I tried this and nothing happened. I tried changing the offset to (,0), and it worked, but only if the range started in cell A1. I changed "[a1].CurrentRegion" to "Selection" and that seemed to do it.

    With your help, here's what i came up with. Thank you so much!

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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