How to do I change last name to last intial

Ivn68

Board Regular
Joined
Aug 21, 2015
Messages
82
Inside a cell I have First name, Last name
I want to use a vba to convert a it to First name, and Last initial for a whole list of names
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Will there always be a comma & one space between the first name and last name?
 
Upvote 0
If my assumption above is true, simply select the entries you want to update and run this code:
VBA Code:
Sub FixNames()

    Dim cell As Range
    Dim x As Long
    
    Application.ScreenUpdating = False
    
'   Loop through each cell in selection
    For Each cell In Selection
'       Locate comma and space
        x = InStr(cell, ", ")
'       If found, remove end of last name
        If x > 0 Then cell.Value = Left(cell, x + 2)
    Next cell
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Do I copy the whole thing or the green part is explaining what's happening


If my assumption above is true, simply select the entries you want to update and run this code:
VBA Code:
Sub FixNames()

    Dim cell As Range
    Dim x As Long
   
    Application.ScreenUpdating = False
   
'   Loop through each cell in selection
    For Each cell In Selection
'       Locate comma and space
        x = InStr(cell, ", ")
'       If found, remove end of last name
        If x > 0 Then cell.Value = Left(cell, x + 2)
    Next cell
   
    Application.ScreenUpdating = True
   
End Sub
 
Upvote 0
If you hit CTRL+Down Arrow, it will select all the cells down to the bottom of the column.

If you want the VBA code to automatically select the data range, please let me know the first cell in the range (what column/row).
 
Upvote 0
Try this:
VBA Code:
Sub FixNames()

    Dim lr As Long
    Dim cell As Range
    Dim x As Long
    
    Application.ScreenUpdating = False
    
'   Find last row in column A with data
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Loop through each cell in selection
    For Each cell In Range("A2:A" & lr)
'       Locate comma and space
        x = InStr(cell, ", ")
'       If found, remove end of last name
        If x > 0 Then cell.Value = Left(cell, x + 2)
    Next cell
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Can you say for certain that no record will ever have more than one comma in it?
Like, you won't ever have a record which looks like this:
Jones, Jr, Roy
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

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