SMALL function to find X smallest number greater than Y

K

kittronald

How can you find the X smallest number in a range that is greater than
Y ?

For example, return the second smallest number in the range A1:A4 that
is greater than 0.

A
1 0
2 0
3 10
4 20

The result should be 20.

Any ideas ?


- Ronald K.
 
K

kittronald

*** CORRECTION ***

I didn't word this problem correctly, so forget the A1:A4 range.

If you were to create a formula that resulted in =SMALL({0,0,10,20},
2), how could you get the result to be 20 ?

Is there a way to replace the zero values with text and then use the
SMALL function ?


- Ronald K.
 
J

joeu2004

How can you find the X smallest number in a range that
is greater than Y ?
For example, return the second smallest number in the
range A1:A4 that is greater than 0.

*** CORRECTION ***
I didn't word this problem correctly, so forget the
A1:A4 range.
If you were to create a formula that resulted in
=SMALL({0,0,10,20},2), how could you get the result
to be 20 ?

I see nothing wrong with using the A1:A4 range. Your correction is
simply that you want the __2nd__ smallest instead of the smallest.
That is indeed an important correction because my first reaction was:
20 is the __largest__, not the (1st) smallest. But it is indeed the
__2nd__ smallest if we ignore zeros.

To answer your original more general question (Xth smallest greater
than Y), suppose B1 is the value X, and B2 is the value Y. Then enter
the following __array_formula__ [*]:

=SMALL(IF(A1:A100>B2,A1:A100),B1)

[*] Enter an array formula by pressing ctrl+shift+Enter instead of
Enter. Excel will display an array formula surrounded by curly braces
in the Formula Bar, i.e. {=formula}. You cannot type the curly braces
yourself. If you make a mistake, select the cell, press F2 and edit,
then press ctrl+shift+Enter.
 
D

Dave Peterson

=COUNTIF(A1:A4,"<="&0))
will give you the count of values less than or equal to 0.

So if you want to ignore those values, you could just look for the
(2 + that number) smallest:

=SMALL(A1:A4,2+COUNTIF(A1:A4,"<="&0))

I like this syntax:
=COUNTIF(A1:A4,"<="&0))
but you could use:
=COUNTIF(A1:A4,"<=0"))

But if you put that value in X99, the first is easier to modify:
=COUNTIF(A1:A4,"<="&x99))
 
K

kittronald

Joe and Dave,

Thanks for those quick replies.

Unfortunately, the curly braces within the parentheses of my second
post weren't very noticeable.

In my haste to post, I incorrectly described the problem.

Here's an accurate, albeit more involved example.

A grocery store is going through its inventory and is interested in
tracking only certain types of items.

For example, each type of fruit is assigned a code. When a
different vendor is used to obtain that type of fruit, the previous
vendor's code is retired by appending a letter or number to that code
(i.e., OA changes to OA1). The new vendor is assigned the original
code (i.e., OA). Over time, retired codes appear many times in the
inventory.

The goal is to look for duplicates and return a value of
"Duplicate" where that code will be ignored in other formulas.

A B
1 Code Name
2 A Apple
3 P Pear
4 OA Orange
5 OA1 Orange

Two named ranges exist:

Codes = $A$2:$A$5

Names = $B$2:$B$5

In C2:C5, the following formula is entered:

=IF(COUNTIF(Names,$B2)=1,"",IF(SUMPRODUCT(SMALL(INDEX(Names=
$B2,)*INDEX(LEFT(Codes,LEN($A2))=$A2,)*INDEX(LEN(Codes),),
2))>0,"","Duplicate"))

In cells C2 and C3, the formula correctly returns "".

However, in cell C4, the formula returns "Duplicate", because out
of {0,0,1,2}, 0 is the second smallest number.

What I'm trying to do is get the formula to exclude the 0's and
return the smallest number in that set.



- Ronald K.
 
K

kittronald

*** CORRECTION ***

The formula should read:

=IF(COUNTIF(Names,$B2)=1,"",IF(SUMPRODUCT(SMALL(INDEX(Names=
$B2,)*INDEX(LEFT(Codes,LEN($A2))=$A2,)*INDEX(LEN(Codes),),
1))>0,"","Duplicate"))


- Ronald K.
 
K

kittronald

Dave,

How can you get COUNTIF to use an array constant ?

For example, =COUNTIF(Names=$B2,"=0")



- Ronald K.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top