Sunday, 18 September 2011

Extracting the last word of a string

Extracting the last word of a string is more complicated because the FIND function only works
from left to right. Therefore, the problem rests with locating the last space character. The formula
that follows, however, solves this problem. It returns the last word of a string (all the text follow-
ing the last space character):
=RIGHT(A1,LEN(A1)-FIND(“*”,SUBSTITUTE(A1,” “,”*”,LEN(A1)-LEN(SUBSTITUTE(A1,” “,””)))))
This formula, however, has the same problem as the first formula in the preceding section: It fails
if the string does not contain at least one space character. The following modified formula uses
the IFERROR function to avoid the error value:
=IFERROR(RIGHT(A1,LEN(A1)-FIND(“*”,SUBSTITUTE(A1,” “,”*”,LEN(A1)-LEN(SUBSTITUTE
(A1,” “,””))))),A1)
For compatibility with versions prior to Excel 2007, use this formula:
=IF(ISERR(FIND(“ “,A1)),A1,RIGHT(A1,LEN(A1)-FIND(“*”,SUBSTITUTE(A1,” “,”*”,LEN(A1)-
LEN(SUBSTITUTE(A1,” “,””))))))

No comments:

Post a Comment