18.11.2014 Views

Microsoft Office

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Creating Formulas That Look Up Values 15<br />

When a Blank Is Not a Zero<br />

The Excel lookup functions treat empty cells in the result range as zeros. The worksheet in the accompanying<br />

figure contains a two-column lookup table, and this formula looks up the name in cell B1 and returns<br />

the corresponding amount:<br />

=VLOOKUP(B1,D2:E8,2)<br />

Note that the Amount cell for Charlie is blank, but the formula returns a 0.<br />

If you need to distinguish zeros from blank cells, you must modify the lookup formula by adding an IF function<br />

to check whether the length of the returned value is 0. When the looked up value is blank, the length of<br />

the return value is 0. In all other cases, the length of the returned value is non-zero. The following formula displays<br />

an empty string (a blank) whenever the length of the looked-up value is zero and the actual value whenever<br />

the length is anything but zero:<br />

=IF(LEN(VLOOKUP(B1,D2:E8,2))=0,””,(VLOOKUP(B1,D2:E8,2)))<br />

Alternatively, you can specifically check for an empty string, as in the following formula:<br />

=IF(VLOOKUP(B1,D2:E8,2)=””,””,(VLOOKUP(B1,D2:E8,2)))<br />

The INDEX function returns a cell from a range. The syntax for the INDEX function is<br />

INDEX(array,row_num,column_num)<br />

The INDEX function’s arguments are as follows:<br />

n<br />

n<br />

n<br />

NOTE<br />

array: A range<br />

row_num: A row number within array<br />

col_num: A column number within array<br />

If array contains only one row or column, the corresponding row_num or column_num argument<br />

is optional.<br />

Figure 15.5 shows a worksheet with dates, day names, and amounts in columns D, E, and F. When you<br />

enter a date in cell B1, the following formula (in cell B2) searches the dates in column D and returns the<br />

corresponding amount from column F. The formula in cell B2 is<br />

=INDEX(F2:F21,MATCH(B1,D2:D21,0))<br />

279

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

Saved successfully!

Ooh no, something went wrong!