View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Harlan Grove[_2_] Harlan Grove[_2_] is offline
external usenet poster
 
Posts: 1,231
Default Returning an array of unique values?

"Blue Max" wrote...
....
. . . Nevertheless, here is an example if it will help explain your
'Yes' answers to our three questions:

The sample formula
{=IF(FREQUENCY(MATCH(A1:A8,A1:A8,0),MATCH(A1:A8,A 1:A8,0))0,
INDEX(A1:A8,MATCH(A1:A8,A1:A8,0)),"")}
is entered as an array formula in the range A10:A17. The source range
A1:A8 in the formula is simply a test column of 8 cells with the
entries Banana, Apple, Apple, Banana, Orange, Pineapple, Orange, and
Nut. The formula returns a listing of unique fruits to the
destination range A10:A17 of Banana, Apple, "", "", Orange,
Pineapple, "", Nut.


You could make this more efficient by using the array formula

=IF(MATCH(A1:A8,A1:A8,0)=ROW(A1:A8)-MIN(ROW(A1:A8))+1,A1:A8,"")

As you can see, the formula successfully extracts the unique values
of the source range and keeps them properly updated in the
destination range as values are modified in the source range. The
problem here is that the resulting destination list is interspersed
with blank cells. We want to eliminate the blank cells from the
destination range and list the unique values sequentially at the top
of the range. At some point we my also wish to have the formula sort
the resulting unique values before placing them in the destination
range of cells.


The destination range COULD return as many items as the source range
if all items in the source range were distinct. So you'll need to make
due with formulas that evaluate to "" or #N/A for duplicate items in
the source list, but those values could appear below the distinct
values.

The most efficient way to load the distinct values into another range
would be with formulas like these (which also sort).

C1 [array formula]:
=INDEX($A$1:$A$8,MATCH(0,COUNTIF($A$1:$A$8,"<"&$A$ 1:$A$8),0))

C2 [array formula]:
=IF(COUNTIF($A$1:$A$8,""&C1),INDEX($A$1:$A$8,MATC H(COUNTIF($A$1:$A$8,
"<="&C1),COUNTIF($A$1:$A$8,"<"&$A$1:$A$8),0)), "")

Fill C2 down into C3:C8. Given your data in A1:A8, this fills C1:C8
with {"Apple";"Banana";"Nut";"Orange";"Pineapple";"";"" ;""}.

This technique is often useful in accounting where the user needs to
identify all the unique General Ledger Accounts in a very long list of
detail transactions which are repeatedly associated with the same set
of account numbers. . . .

....

In that case, it'd be more more efficient to use an advanced filter,
copying only the unique items in the source range to the destination
range followed by sorting the filtered, unique results. Or you could
use a pivot table to summarize fields by general ledger number if your
general ledger data were in a table.