Find text after last space in string

Mark77

Board Regular
Joined
Aug 27, 2004
Messages
66
Hi everyone

I wonder if you can help with this issue, I receive data about a name in one column, a sample of this data is as follows:

Column A
Row 1 Mr M A One
Row 2 Mr Mark A Two
Row 3 Mrs Maria Angela Three
Row 4 Miss Four

Each row of the above appears in one cell and I need to split this out into Title and Surname. I have a function to find the title from the above:

=LEFT(A1,FIND(" ",A1&" "))

This finds the information before the first space, I would like to know if it is possible to do the same to find the information after the last space. It sounds simple but as you can see from the above there are not a set number of spaces for each row and I cannot see how to find the last space where the number is variable.

Please could you help?

Thanks

Mark
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi Mark,

You can use following function to extract Title and Surname from given string.


Code:
Function TitleSurname(TS As String) As String
Dim Str As Variant
Str = Split(TS, " ")
If UBound(Str) < 2 Then MsgBox "No Surname": Exit Function
       TitleSurname = Str(LBound(Str)) & " " & Str(UBound(Str))
End Function
Hope it helps
 
Upvote 0
or you can use this -

Code:
=LEFT(A1,FIND(" ",A1)) &" "&TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",LEN(A1))),LEN(A1)))

HTH
 
Upvote 0
Thank you for your fast replies, all three work perfectly and as decribed.

Can't thank you all enough

Best regards

Mark
 
Upvote 0
Is there a way to do this for a large range of cells ('Time Card'!G8:G679), then count the number of times it would come up with "185"?
 
Upvote 0
Is there a way to do this for a large range of cells ('Time Card'!G8:G679), then count the number of times it would come up with "185"?
If you really do not need the split out values after the last space for anything else, you can elimate doing that step and count the entries directly from the original range...

=COUNTIF('Time Card'!G8:G679,"* 185")

If you have the 185 value stored in a cell (rather than hard-coding into the formula), say it is stored in 'Time Card'!G1, then the formula would look like this...

=COUNTIF('Time Card'!G8:G679,"* "&'Time Card'!G1)
 
Upvote 0
Hello Mark, try this formula

=TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",LEN(A1))),LEN(A1)))


Trying to understand what is going on here. It works. It finds the word to the far right no matter how many spaces. But I walk through, and when you do the REPT part, I stutter step.

So substitute A1, where there are spaces, with the LEN of A1...so if LEN is 22, replace each space with 22 spaces?

Yes. Fine.

Now find the word with RIGHT, 22 spaces.....ok, so this is telling me that because we inserted 22 spaces each time, that it is now only possible to pull back the far most word to the right, because only space exists in front of it right?
Then Trim it and we are done.

I think I talked that out correctly. I don't like to see cell formulas where I don't understand what is going on. Am I right?
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,039
Members
448,940
Latest member
mdusw

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