Excel: Finding the nth Value That Meets a Condition

ID: Q101167


The information in this article applies to:
  • Microsoft Excel for Windows, versions 4.0, 4.0a
  • Microsoft Excel for the Macintosh, version 4.0
  • Microsoft Excel 97 for Windows


SUMMARY

In Microsoft Excel 4.0, to find the nth value in a range of cells that meets a condition, use the SMALL() or LARGE() function to evaluate the array of row numbers that meet the condition.


MORE INFORMATION

Example

To find the nth nonblank value in a range, enter the following formula as an array:

   =INDEX(range,SMALL(IF(ISBLANK(range),"",ROW(range)),n)-ROW(range)+1) 
NOTE: To enter the formula as an array, type the formula in a cell and press CTRL+SHIFT+ENTER in Microsoft Excel for Windows or COMMAND+ENTER in Microsoft Excel for the Macintosh.

In above example, the range argument refers to the cells you are searching and the n argument is a number indicating the occurrence you are looking for. For example, if range refers to cells A1:A10 and n is 2, the formula returns the second nonblank value from cells A1:A10.

Following is a description of how the formula works:

In the following section of the formula,

   SMALL(IF(ISBLANK(range),"",ROW(range)),n) 
the IF() formula returns an array of row numbers where the condition is met. In this example, an array of row numbers for all the nonblank cells is returned by the IF() function. The SMALL() function looks at these row numbers and returns the nth smallest row number.

In the -ROW(range)+1 section of the formula, the starting row number in the range is subtracted from the row number returned by SMALL() and then 1 is added. This calculates a relative "position" of the value in the range so that the value can be returned with the INDEX() function.

If the nth value is a blank, and the remaining cells in the range are blank, the #NUM! error value will be returned to the cell. If you want to find the nth value from the bottom up (instead of from the top down), use the LARGE() function instead of the SMALL() function.


REFERENCES

"Function Reference", version 4.0, pages 236-238, 250-251, 406

Additional query words: 4.00a howto returning

Keywords :
Version : MACINTOSH:4.0; WINDOWS:4.0,4.0a,97
Platform : MACINTOSH WINDOWS
Issue type :


Last Reviewed: December 23, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.