I have an Excel spreadsheet containing a list of strings. Each string is made up of several words, but the number of words in each string is different.
Using built in Excel functions (no VBA), is there a way to isolate the last word in each string?
Examples:
Are you classified as human? -> human? Negative, I am a meat popsicle -> popsicle Aziz! Light! -> Light!
This is the technique I've used with great success:
To get the first word in a string, just change from RIGHT to LEFT
Also, replace A1 by the cell holding the text.
This is very robust--it works for sentences with no spaces, leading/trailing spaces, multiple spaces, multiple leading/trailing spaces... and I used char(7) for the delimiter rather than the vertical bar "|" just in case that is a desired text item.
To add to Jerry and Joe's answers, if you're wanting to find the text BEFORE the last word you can use:
With 'My little cat' in A1 would result in 'My little' (where Joe and Jerry's would give 'cat'
In the same way that Jerry and Joe isolate the last word, this then just gets everything to the left of that (then trims it back)
Imagine the string could be reversed. Then it is really easy. Instead of working on the string:
you work with
With
=LEFT(A1;FIND(" ";A1)-1)
in A2 you get"My"
with (1) and"tac"
with (2), which is reversed"cat"
, the last word in (1).There are a few VBAs around to reverse a string. I prefer the public VBA function
ReverseString
.Install the above as described. Then with your string in A1, e.g.,
"My little cat"
and this function in A2:you'll see
"cat"
in A2.The method above assumes that words are separated by blanks. The
IF
clause is for cells containing single words = no blanks in cell. Note:TRIM
andCLEAN
the original string are useful as well. In principle it reverses the whole string from A1 and simply finds the first blank in the reversed string which is next to the last (reversed) word (i.e.,"tac "
).LEFT
picks this word and another string reversal reconstitutes the original order of the word (" cat"
). The-1
at the end of theFIND
statement removes the blank.The idea is that it is easy to extract the first(!) word in a string with
LEFT
andFIND
ing the first blank. However, for the last(!) word theRIGHT
function is the wrong choice when you try to do that because unfortunately FIND does not have a flag for the direction you want to analyse your string.Therefore the whole string is simply reversed.
LEFT
andFIND
work as normal but the extracted string is reversed. But his is no big deal once you know how to reverse a string. The firstReverseString
statement in the formula does this job.