18.11.2014 Views

Microsoft Office

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Part II<br />

Working with Formulas and Functions<br />

For example, if cell A1 contains the text Blonde On Blonde and B1 contains the text Blonde, the formula<br />

returns 2.<br />

The comparison is case sensitive, so if B1 contains the text blonde, the formula returns 0. The following<br />

formula is a modified version that performs a case-insensitive comparison by converting the characters to<br />

uppercase:<br />

=(LEN(A1)-LEN(SUBSTITUTE(UPPER(A1),UPPER(B1),””)))/LEN(B1)<br />

Extracting a filename from a path specification<br />

The following formula returns the filename from a full path specification. For example, if cell A1 contains<br />

c:\windows\important\myfile.xlsx, the formula returns myfile.xlsx.<br />

=MID(A1,FIND(“*”,SUBSTITUTE(A1,”\”,”*”,LEN(A1)-<br />

LEN(SUBSTITUTE(A1,”\”,””))))+1,LEN(A1))<br />

This formula assumes that the system path separator is a backslash (\). It essentially returns all text that follows<br />

the last backslash character. If cell A1 doesn’t contain a backslash character, the formula returns an<br />

error.<br />

Extracting the first word of a string<br />

To extract the first word of a string, a formula must locate the position of the first space character and then<br />

use this information as an argument for the LEFT function. The following formula does just that:<br />

=LEFT(A1,FIND(“ “,A1)-1)<br />

This formula returns all of the text prior to the first space in cell A1. However, the formula has a slight<br />

problem: It returns an error if cell A1 consists of a single word. A slightly more complex formula that<br />

checks for the error using the IFERROR function solves that problem:<br />

CAUTION<br />

=IFERROR(LEFT(A1,FIND(“ “,A1)-1),A1)<br />

The preceding formula uses the IFERROR function, which is new to Excel 2007. If your workbook<br />

will be used with previous versions of Excel, use this formula:<br />

=IF(ISERR(FIND(“ “,A1)),A1,LEFT(A1,FIND(“ “,A1)-1))<br />

Extracting the last word of a string<br />

Extracting the last word of a string is more complicated because the FIND function only works from left to<br />

right. Therefore the problem is locating the last space character. The formula that follows, however, solves<br />

this problem by returning the last word of a string (all text following the last space character):<br />

=RIGHT(A1,LEN(A1)-FIND(“*”,SUBSTITUTE(A1,” “,”*”,LEN(A1)-<br />

LEN(SUBSTITUTE(A1,” “,””)))))<br />

This formula, however, has the same problem as the first formula in the preceding section: It fails if the<br />

string does not contain at least one space character. The following modified formula uses the new IFERROR<br />

function to test for an error (that is, no spaces). If the first argument returns an error, then the formula<br />

returns the complete contents of cell A1.<br />

=IFERROR(RIGHT(A1,LEN(A1)-FIND(“*”,SUBSTITUTE(A1,” “,”*”,LEN(A1)-<br />

LEN(SUBSTITUTE(A1,” “,””))))),A1)<br />

218

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!