Steve Bogdanoff
New Member
- Joined
- May 11, 2010
- Messages
- 12
There have been numerous posts about how to count the number of unique values existing in a specific column of a filtered table. Responses often point to array formulas that use a combination of the FREQUENCY, SUBTOTAL and MATCH functions, for example:
{=SUM(IF(FREQUENCY(IF(SUBTOTAL(3,OFFSET($A10,ROW($A$10:$A$1000)-ROW($A10),,1)),IF($A$10:$A$1000<>"",MATCH("~"&$A$10:$A$1000$A$10:$A$1000&"",0))),ROW($A$10:$A$1000)-ROW($A10)+1),1))}
While those formulas produce accurate results in that simple situation, they do not address this slightly more advanced scenario.
Assume a filtered table with no limit on the number of active filter selections. Once filtered, the table will produce a specific number of “visible rows.” Within those visible rows there will be a specific number of unique values in a targeted column. So, for example, assume a table with a data range of A10:C1000 where column A contains region names, column B contains product names, and column C contains sales revenues. If the table is filtered on column C for sales revenues greater than $1,000, only those rows with a value in column C that is greater than $1,000 will be visible. From there we can use the standard array formula above to determine how many unique region names are currently visible in column A.
Now assume that cell A1 contains a specific product name, e.g., “shirts.” Here is the question: what array formula could be placed in cell A2 that would count the number of unique region names in the filtered table that ALSO have “shirts” in column B? The array formula in question would thus have to have the capacity to count unique values in column A but also evaluate them with respect to the additional criteria, in this case a column C value equal to “shirts" which is noted in a cell external to the table, A1.
An easy solution would be to simply activate a filter on column ‘B’ for “shirts.” However, that only produces a result for that one type of product. The task is to create a report that shows the number of unique regions in the filtered table for each of the possible product names: “shirts,” “pants,” “socks,” “coats,” etc. That would require an array formula for each type of product name.
With all this in mind, an important question is whether a better solution would be a VBA user-defined function?
{=SUM(IF(FREQUENCY(IF(SUBTOTAL(3,OFFSET($A10,ROW($A$10:$A$1000)-ROW($A10),,1)),IF($A$10:$A$1000<>"",MATCH("~"&$A$10:$A$1000$A$10:$A$1000&"",0))),ROW($A$10:$A$1000)-ROW($A10)+1),1))}
While those formulas produce accurate results in that simple situation, they do not address this slightly more advanced scenario.
Assume a filtered table with no limit on the number of active filter selections. Once filtered, the table will produce a specific number of “visible rows.” Within those visible rows there will be a specific number of unique values in a targeted column. So, for example, assume a table with a data range of A10:C1000 where column A contains region names, column B contains product names, and column C contains sales revenues. If the table is filtered on column C for sales revenues greater than $1,000, only those rows with a value in column C that is greater than $1,000 will be visible. From there we can use the standard array formula above to determine how many unique region names are currently visible in column A.
Now assume that cell A1 contains a specific product name, e.g., “shirts.” Here is the question: what array formula could be placed in cell A2 that would count the number of unique region names in the filtered table that ALSO have “shirts” in column B? The array formula in question would thus have to have the capacity to count unique values in column A but also evaluate them with respect to the additional criteria, in this case a column C value equal to “shirts" which is noted in a cell external to the table, A1.
An easy solution would be to simply activate a filter on column ‘B’ for “shirts.” However, that only produces a result for that one type of product. The task is to create a report that shows the number of unique regions in the filtered table for each of the possible product names: “shirts,” “pants,” “socks,” “coats,” etc. That would require an array formula for each type of product name.
With all this in mind, an important question is whether a better solution would be a VBA user-defined function?