Return Characters from Text Cell

a2a

New Member
Joined
Dec 20, 2009
Messages
37
Very frustrated with what I thought was a simple function, however cannot get this working and would really kindly appreciate any help here:)

I have a sheet of names in various forms that I am trying to reconcile, however some are in the form FIRST NAME SURNAME and some are FIRST NAME MIDDLE NAME SURNAME.

I'd like simply just to return the surname from that cell, and do this by finding the last occurence of a space " " in the cell and give me that value.

Initially I used: =MID(A1,FIND(" ",A1)+1,LEN(A1)-(FIND(" ",A1))), but this was only so good for the first occurence.

Thank you :cool:
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Wow - that algorithm function is truly awesom - great bit of expert coding there. Thanks for sharing that link :)
 
Upvote 0
Hi,

Thanks for the compliments :)

If possible, I'd stick to the the formulae supplied , otherwise, maybe this bespoke UDF mightr do it:
Code:
Option Explicit

Function FindName(ByVal Name As Range, _
                  ByVal LookupTable As Range, _
                  ByVal IndexNum As Integer) As Variant
Dim bMatch As Boolean
Dim iPtrName As Integer, iPtrLookup As Integer
Dim iPtrNameUB As Integer, iPtrLookupUB As Integer
Dim rCur As Range, rLookup As Range
Dim sName As String, saName() As String
Dim sCurEntry As String, saCurEntry() As String

FindName = CVErr(xlErrNA)
sName = ""
For Each rCur In Name
    sName = sName & " " & LCase$(CStr(rCur.Value))
Next rCur

If Trim(sName)<> "" Then
    saName = Split(WorksheetFunction.Trim(sName), " ")
    iPtrNameUB = UBound(saName)
    Set rLookup = Intersect(Sheets(LookupTable.Parent.Name).UsedRange, LookupTable.Resize(, 1))
    If Not (rLookup Is Nothing) Then
        For Each rCur In rLookup
            sCurEntry = LCase$(WorksheetFunction.Trim(CStr(rCur.Value)))
            If sCurEntry<> "" Then
                saCurEntry = Split(WorksheetFunction.Trim(sCurEntry), " ")
                iPtrLookupUB = UBound(saCurEntry)
                If saName(iPtrNameUB) = saCurEntry(iPtrLookupUB) Then
                     If iPtrNameUB< iPtrLookupUB Then
                        bMatch = MatchingForenames(saName, saCurEntry)
                     Else
                        bMatch = MatchingForenames(saCurEntry, saName)
                     End If
                     If bMatch Then
                        If IndexNum = 0 Then
                            FindName = rCur.Row
                        Else
                            FindName = rCur.Offset(, IndexNum - 1).Value
                        End If
                        Exit Function
                     End If
                 End If
            End If
        Next rCur
    End If
End If

End Function
Private Function MatchingForenames(ByRef Forenames1() As String, _
                                    ByRef Forenames2() As String) As Boolean
'** NOTE: Assumes ForeNames1 has<= elements than ForeNames2 **
Dim bMatch As Boolean
Dim iPtr As Integer, iLen1 As Integer, iLen2 As Integer

bMatch = True
For iPtr = 0 To UBound(Forenames1) - 1
    iLen1 = Len(Forenames1(iPtr))
    iLen2 = Len(Forenames2(iPtr))
    If iLen1 > iLen2 Then
        bMatch = Left$(Forenames1(iPtr), iLen2) = Forenames2(iPtr)
    Else
        bMatch = Left$(Forenames2(iPtr), iLen1) = Forenames1(iPtr)
    End If
    If bMatch = False Then Exit For
Next iPtr
MatchingForenames = bMatch
End Function

For example:
Excel Workbook
ABCDEF
1Our DataSupplier Data
2Jean Claude Van DammeForename(s)SurnameMatch Row
3Bruce Ignateous WillisJean ClaudeDamme2
4Another HeroS XSeagal5
5Steven Xavier SeagalBWillis3
6P A O'ToolePeterO'Toole6
7John SmithUnknownWarrior#N/A
8IanSmith#N/A
Sheet1
Excel 2003
Cell Formulas
RangeFormula
F3=FindName(D3:E3,A:A,0)
 
Upvote 0

Forum statistics

Threads
1,223,925
Messages
6,175,423
Members
452,641
Latest member
Arcaila

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