Hi ,
It has nothing to do with COUNTIF being nested within INDEX.
When the COUNTIF function is used in its standard syntax , the first parameter is an array or range , and the second parameter is a scalar , a single value.
For example , suppose you have defined the range A3:A11 as a named range called List. Suppose this range consists of the following numbers :
1 , 8 , 7 , 9 , 9 , 3 , 9 , 4 , 7
Now , suppose we have the value 9 entered in cell D1.
We can put in the following formula :
=COUNTIF(List , D1)
and it will display the result as 3.
But , suppose we interchange the order of the two parameters , and put in the formula :
=COUNTIF(D1 , List)
You will see the value 1 displayed in the cell where this formula has been entered , but when you press F9 in the cell , you will see :
{0;0;0;1;1;0;1;0;0}
What this is doing is it is comparing D1 with every element of List , and outputting 0 if the two are not the same , and 1 if they are.
Next , suppose we enter 3 in cell D2 , and enter the formula :
=COUNTIF(D1:D2 , List)
If you press F9 in the cell , you will now see :
{0;0;0;1;1;1;1;0;0}
where the 1s signify either a 3 or a 9.
The 0s signify where the values are neither 3 nor 9.
How do we use this array ?
Let us see what happens if we enter this formula as follows :
=INDEX(List , MATCH(0 , COUNTIF(E$1:E1 , List) , 0))
This is an array formula , to be entered using CTRL SHIFT ENTER.
It is to be entered in cell E2.
So what happens in E2 ?
The COUNTIF function finds none of the numbers in the list in cell E1 , which means it will return an array of zeros :
{0;0;0;0;0;0;0;0;0}
The MATCH function looks for a zero , and finds it in the very first element of the array ; taking the first element of the named range List , the INDEX function returns the first value from the named range List , which is 1.
Thus E2 will have the value 1.
What happens if this formula is copied down to E3 ?
The COUNTIF function is now :
COUNTIF(E$1:E2 , List)
which means that the returned value of 1 is now included in the first parameter. Thus , what the above formula will return is the array :
{1;0;0;0;0;0;0;0;0}
Thus , the MATCH function will now return 2 , since the zero is now in the second place.
The INDEX function will therefore return the second element of the named range List.
Thus , as we copy this formula down , each of the elements of the named range List will be returned , and since the COUNTIF function will count all occurrences , when ever a duplicate is encountered , it will skip that element and move to the next element.
What we will have finally is a list of all the distinct values in the named range List.
Now , to the main question : why do we have the construct :
=INDEX(List,MATCH(0,INDEX(COUNTIF(F$7:F8,List),),0))
The highlighted INDEX function eliminates the need to enter the overall formula as an array formula , which means with this usage , you do not need to use CTRL SHIFT ENTER. The formula is a non-array formula , and just ENTER will do.
Narayan