Search/Replace multiple chars with multiple values

tbcomputerguy

New Member
Joined
Mar 27, 2019
Messages
5
I have been given a list like so;
stan smith joe wilson ken hanson mike jablonski

I would to have the list look like so:
stan smith | joe wilson | ken hanson | mike jablonski

the substitute value only works with a specific set value. so it would set the first delimited between smith and joe but will not carry on to the next set of values. this probably need some vba.

Dave
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Select the range that holds your list, then run this macro:
Code:
Sub computerguy()
'select the range to be altered then run this macro
Dim c As Range, V As Variant, i As Long, x As String, ct As Long
For Each c In Selection
    If InStr(c.Value, " ") > 0 Then
        V = Split(c.Value, " ")
        For i = LBound(V) To UBound(V)
            ct = ct + 1
            If ct Mod 2 <> 0 Then
                x = x & V(i) & " "
            Else
                x = x & V(i) & " | "
            End If
        Next i
    Else
        GoTo Nx
    End If
    Application.ScreenUpdating = False
    c.Value = Mid(x, 1, Len(x) - 2)
Nx:    x = ""
Next c
Application.ScreenUpdating = True
End Sub
 
Upvote 0
That worked perfectly. I posted the last reply almost at the same time you posted yours.
There's a trailing space in what I gave you. To remove it change this line:

c.Value = Mid(x, 1, Len(x) - 2)

to this:

c.Value = Trim(Mid(x, 1, Len(x) - 2))
 
Upvote 0

Forum statistics

Threads
1,223,723
Messages
6,174,108
Members
452,544
Latest member
aush

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top