Page 262 - Excel 2007 Bible
P. 262
17_044039 ch12.qxp 11/21/06 11:05 AM Page 219
Creating Formulas That Manipulate Text
Following is a modification that doesn’t use the IFERROR function. This formula works for all versions of
Excel.
=IF(ISERR(FIND(“ “,A1)),A1,RIGHT(A1,LEN(A1)-FIND(“*”,SUBSTITUTE(A1,
” “,”*”,LEN(A1)-LEN(SUBSTITUTE(A1,” “,””))))))
Extracting all but the first word of a string
The following formula returns the contents of cell A1, except for the first word:
=RIGHT(A1,LEN(A1)-FIND(“ “,A1,1))
If cell A1 contains 2007 Operating Budget, the formula returns Operating Budget.
The formula below, which uses the new IFERROR function, returns the entire contents of cell A1 if the cell
doesn’t have a space character:
=IFERROR(RIGHT(A1,LEN(A1)-FIND(“ “,A1,1)),A1)
A modification that works in all versions of Excel is
=IF(ISERR(FIND(“ “,A1)),A1,RIGHT(A1,LEN(A1)-FIND(“ “,A1,1)))
Extracting first names, middle names, and last names 12
Suppose you have a list consisting of people’s names in a single column. You have to separate these names
into three columns: one for the first name, one for the middle name or initial, and one for the last name.
This task is more complicated than you may think because it must handle the situation for a missing middle
initial. However, you can still do it.
NOTE The task becomes a lot more complicated if the list contains names with titles (such as Mr. or
NOTE
Dr.) or names followed by additional details (such as Jr. or III). In fact, the following formulas
will not handle these complex cases. However, they still give you a significant head start if you’re willing to
do a bit of manual editing to handle special cases.
The formulas that follow all assume that the name appears in cell A1.
You can easily construct a formula to return the first name:
=LEFT(A1,FIND(“ “,A1)-1)
This formula returns the last name:
=RIGHT(A1,LEN(A1)-FIND(“*”,SUBSTITUTE(A1,” “,”*”,LEN(A1)-
LEN(SUBSTITUTE(A1,” “,””)))))
The next formula extracts the middle name and requires that you use the other formulas to extract the first
name and the last name. It assumes that the first name is in B1 and the last name is in D1. Here’s what it
looks like:
=IF(LEN(B1&D1)+2>=LEN(A1),””,MID(A1,LEN(B1)+2,LEN(A1)-LEN(B1&D1)-2))
As you can see in Figure 12.5, the formulas work fairly well. There are a few problems, however, notably
names that contain four “words.” But, as I mentioned earlier, you can clean these cases up manually.
219