Page 261 - Excel 2007 Bible
P. 261

17_044039 ch12.qxp  11/21/06  11:05 AM  Page 218
                                   Part II
                                              Working with Formulas and Functions
                                             For example, if cell A1 contains the text Blonde On Blonde and B1 contains the text Blonde, the for-
                                             mula returns 2.
                                             The comparison is case sensitive, so if B1 contains the text blonde, the formula returns 0. The following
                                             formula is a modified version that performs a case-insensitive comparison by converting the characters to
                                             uppercase:
                                                  =(LEN(A1)-LEN(SUBSTITUTE(UPPER(A1),UPPER(B1),””)))/LEN(B1)
                                             Extracting a filename from a path specification
                                             The following formula returns the filename from a full path specification. For example, if cell A1 contains
                                             c:\windows\important\myfile.xlsx, the formula returns myfile.xlsx.
                                                  =MID(A1,FIND(“*”,SUBSTITUTE(A1,”\”,”*”,LEN(A1)-
                                                  LEN(SUBSTITUTE(A1,”\”,””))))+1,LEN(A1))
                                             This formula assumes that the system path separator is a backslash (\). It essentially returns all text that fol-
                                             lows the last backslash character. If cell A1 doesn’t contain a backslash character, the formula returns an
                                             error.
                                             Extracting the first word of a string
                                             To extract the first word of a string, a formula must locate the position of the first space character and then
                                             use this information as an argument for the LEFT function. The following formula does just that:
                                                  =LEFT(A1,FIND(“ “,A1)-1)
                                             This formula returns all of the text prior to the first space in cell A1. However, the formula has a slight
                                             problem: It returns an error if cell A1 consists of a single word. A slightly more complex formula that
                                             checks for the error using the IFERROR function solves that problem:
                                                  =IFERROR(LEFT(A1,FIND(“ “,A1)-1),A1)
                                        CAUTION        The preceding formula uses the IFERROR function, which is new to Excel 2007. If your work-
                                        CAUTION
                                                       book will be used with previous versions of Excel, use this formula:
                                               =IF(ISERR(FIND(“ “,A1)),A1,LEFT(A1,FIND(“ “,A1)-1))
                                             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 is locating the last space character. The formula that follows, however, solves
                                             this problem by returning the last word of a string (all text following 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 new IFERROR
                                             function to test for an error (that is, no spaces). If the first argument returns an error, then the formula
                                             returns the complete contents of cell A1.
                                                  =IFERROR(RIGHT(A1,LEN(A1)-FIND(“*”,SUBSTITUTE(A1,” “,”*”,LEN(A1)-
                                                  LEN(SUBSTITUTE(A1,” “,””))))),A1)

                                      218
   256   257   258   259   260   261   262   263   264   265   266