Macro to just leave first names in column

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,
Column C holds a list of names thats not very clean,
I was hoping there was a way to remove everything but the first name from each cell?
so I need a macro then when run is quite cleaver and leaves me with just first names,
I've tried doing it with a list of things to remove but quite honestly its such a mess its hard as the list get longer and longer so how about this?
a macro to run in column C that remove everything but the first name by looking at the cell finding the first string of letters longer than 1 that has a space after it or is the only word and removes everything else?

I'm thinking I cant be the first person to have this problem so hopfully someone know a great way to do it?
thanks
Tony
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
How about ...

VBA Code:
Public Sub tonywatsonhelp()

    Dim Sht As Worksheet
    Set Sht = ThisWorkbook.Worksheets("Sheet1")     ' <<< change sheet name to suit
    With Sht
        Dim Rng As Range
        Set Rng = .Range("C2:C" & .Cells(.Rows.Count, "C").End(xlUp).Row)
        Dim arr As Variant
        arr = Rng.Value
    End With
    Dim i As Long
    For i = 1 To UBound(arr)
        arr(i, 1) = Split(arr(i, 1), " ")(0)
    Next i
    Rng.Value = arr
End Sub
 
Upvote 0
Here is another macro that you can try...
VBA Code:
Sub GetFirstNames()
  With Range("C2", Cells(Rows.Count, "C").End(xlUp))
    .Value = Evaluate(Replace("IF(MID(@,2,1)="" "",MID(@,3,LEN(@)),@)", "@", .Address))
    .Replace " *", "", xlPart, , , , False, False
  End With
End Sub
Note: My code assumes the worksheet with your names is active when the code is run.
 
Upvote 0
Solution
Thank you GWteB, for your help,
Rick, this looks like it will deal with the fact that the first name is not easy ti find as its not always the first word,
I'll need to test it out but looks amazing thanks very much.

Tony
 
Upvote 0
You are welcome. Note that I designed this for what you posted... single letter first initial to be ignored, but note that it will not work if that first initial has a period after it. If you could have such an occurrence, let me know and I'll modify the code to account for periods after first initials.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,716
Members
448,985
Latest member
chocbudda

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