vba to add comma after first word in each cell in column

kit99

Active Member
Joined
Mar 17, 2015
Messages
352
Got names in column F, written like: Lennon John Winston.
Column holds headers.

Anyone that got a vba that can run through column F and add comma after the first word?
When name is Lennon-Smith John Winston the comma must be placed like this: "Lennon-Smith, John Winston".

Tried this:
Code:
    Columns("F:F").Select
    Selection.Replace What:=" ", Replacement:=", ", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
but it adds comma after every word...
 
Last edited:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Re: Help on vba to add comma after first word in each cell in column

Code:
Public Sub AddNameComma()
Dim vNam, vFirst, vLast
Dim i As Integer


Range("F2").Select
While ActiveCell.Value <> ""
   vNam = ActiveCell.Value
   i = InStr(vNam, " ")
   If i > 0 Then
     vLast = Left(vNam, i - 1)
     vFirst = Mid(vNam, i + 1)
   
      ActiveCell.Value = vLast & ", " & vFirst
   End If
   
   ActiveCell.Offset(1, 0).Select   'next row
Wend
End Sub
 
Upvote 0
Re: Help on vba to add comma after first word in each cell in column

Code:
Public Sub AddNameComma()
Dim vNam, vFirst, vLast
Dim i As Integer


Range("F2").Select
While ActiveCell.Value <> ""
   vNam = ActiveCell.Value
   i = InStr(vNam, " ")
   If i > 0 Then
     vLast = Left(vNam, i - 1)
     vFirst = Mid(vNam, i + 1)
   
      ActiveCell.Value = vLast & ", " & vFirst
   End If
   
   ActiveCell.Offset(1, 0).Select   'next row
Wend
End Sub


Fantastic! Works great!
Thanks a lot for helping out. :)
 
Upvote 0
Re: Help on vba to add comma after first word in each cell in column

Code:
Public Sub AddNameComma()
Dim vNam, vFirst, vLast
Dim i As Integer


Range("F2").Select
While ActiveCell.Value <> ""
   vNam = ActiveCell.Value
   i = InStr(vNam, " ")
   If i > 0 Then
     vLast = Left(vNam, i - 1)
     vFirst = Mid(vNam, i + 1)
   
      ActiveCell.Value = vLast & ", " & vFirst
   End If
   
   ActiveCell.Offset(1, 0).Select   'next row
Wend
End Sub


Hi again.
I've run into a little problem with your code... :)
If column to scan for some reason holds blank cells here and there, the code stops after finding the first blank one.
Is it possible to change the code in such a way that it runs all the way of used range in column F, even 'tho there may be blank cells in between?
Or perhaps you can put in range to scan based on another column? Column A will always hold data in every cell in used range.
 
Upvote 0
Re: Help on vba to add comma after first word in each cell in column

put an end row....

while activecell.row < 9000
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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