vba help - extract last name

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
983
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

How to extract Last Name/Last Word from Column A. Space is the seperator between Names.
if Single Name then it will be Last Name if blank then column B should be dash.

Below is a table with expected output.
Book47
AB
1NameExpected Last Name
2Sachin TendulkarTendulkar
3M S DhoniDhoni
4-
5Virat KohliKohli
6RahulRahul
7Chris GayleGayle
Sheet1



Thanks
mg
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Another option
+Fluff New.xlsm
AB
1NameExpected Last Name
2Sachin TendulkarTendulkar
3M S DhoniDhoni
4-
5Virat KohliKohli
6RahulRahul
7Chris GayleGayle
8
Main
Cell Formulas
RangeFormula
B2:B7B2=IF(A2="","-",TRIM(RIGHT(SUBSTITUTE(A2," ",REPT(" ",100)),100)))
 
Upvote 0
Hi Fluff and Mole,

Thank you for your help, formula worked in excel.(y)
I tried using below code This method is also worked.


VBA Code:
Sub LastName(ByVal ws As Worksheet)

Dim rng As Range
Dim v As Variant

Set rng = ws.Range("A2", ws.Range("A" & ws.Rows.Count).End(xlUp))
Dim c As Range

On Error Resume Next
For Each c In rng
    If c.value <> "" Then
        v = Split(c.value, " ")
        c.Offset(, 1).value = v(UBound(v))
    Else
        c.Offset(, 1).value = "No Record"
    End If

Next

END SUB

Thanks
mg
 
Last edited by a moderator:
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
May be
VBA Code:
 v = ws.Range("A2", ws.Range("A" & ws.Rows.Count).End(xlUp)).Resize(, 2)
    For i = 1 To UBound(a)
        If v(i, 1) <> "" Then
            v(i, 2) = Split(v(i, 1), " ")(UBound(Split(v(i, 1), " ")))
        Else
            v(i, 2) = "No Record"
        End If
    Next
    ws.Range("A2").Resize(UBound(v, 1), UBound(v, 2)) = v
 
Upvote 0
...and one more method. Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Split Column by Delimiter" = Table.SplitColumn(Source, "Name", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, true), {"Name.1", "Name.2"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Name.1", type text}, {"Name.2", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Last Name", each if[Name.2]= null then [Name.1] else [Name.2]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Name.2"})
in
    #"Removed Columns"

Load to PQ editor. Split column with space delimiter --right most only.
Add column that if new column is null then take value from first column else split column
Remove first split column
 
Upvote 0
Hi Mohadin and AlanSidman,

Thank you both for your help ! and sharing multiple option.


Thanks
mg
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,219
Members
453,024
Latest member
Wingit77

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