Explanation of SUMPRODUCT
"Shawn O'Donnell" wrote in message
...
"Bob Phillips" wrote:
In the example that you show, which should be (at least)
=SUMPRODUCT(1/(COUNTIF(A1:A10,A1:A10)))
In this version of the formula, I don't think you need SUMPRODUCT. SUM
will
do, since you're dealing with a single array and you aren't doing any
dot-producting. I don't even think you need to enter it as an array
formula.
Yes, we know this, but in this case SUM is an array formula SUMPRODUCT
isn't.
But neither the SUMPRODUCT or SUM version will work if one of the cells is
blank.
For the purposes of exploring this SUMPRODUCT construct a little further,
let's say we take Bill's example and erase the contents of the cell that
has
"Jon" in it. The data in A1:A10 is then Bob,John,Bob,Bob,John,
,Bob,Bill,Bill,Max
Here's a parsing of another popular version of this formula, one that
ignores blank cells:
=SUM((A1:A10<"")/COUNTIF(A1:A10,A1:A10&""))
Yes we know all this too, and it works just as well with SUMPRODUCT, which
is till not an array formula.As I recall, this technique was first suggested
with SP not SUM .
Piece by piece, here's what that says:
COUNTIF(A1:A10,A1:A10), as Bob explained, produces a 10-element array
where
each element is the number of times the corresponding cell value appears
in
the range A1:A10.
The extra &"" at the end is a string concatenation. You're adding a
zero-length string onto whatever was in each cell. That converts empty
cells, which Excel interprets as the number 0, to empty strings. Why do
you
want to do that? Because if you represent empty cells as 0, then you'll
end
up with zero in the denominator of the 1/COUNTIF() expression. And you
know
what happens when you do that. So the COUNTIF(A1:A10.A1:A10&"") part of
the
expression evaluates to: {4;2;4;4;2;1;4;2;2;1}. If we didn't have the
&"",
the array would have been {4;2;4;4;2;0;4;2;2;1}. Notice the zero.
You are not adding a "" onto whatever was ikn each cell, buut simply on to
the criteria values.
Why is that zero there? Because COUNTIF promotes an empty cell to 0 if
the
cell is in the 'criteria' argument, but not if the empty cell is in the
'range' argument position. Try this: put the number 0 in B1 and B2. Put
COUNTIF(B1,B2) in cell B3. Then try deleting the zeros in B1 and B2 one
at a
time. Watch how it affects B3. (Let me know if you find the
documentation
on this.)
I assume tyhat you mean =COUNTIF(B1:B2,B2)
snip
I find that I can get away without using an array formula if I use
SUMPRODUCT to add the array, but not if I use SUM. If I use SUM, I have
to
enter the formula as an array formula. I suppose SUMPRODUCT knows how to
handle an array divided by an array piecewise, while SUM doesn't.
It is not SUM that is the problem here but COUNTIF. SUM can work on arrays.
SUMPRODUCT can work on arrays. COUNTIF expects to work on a range array, but
a criteria value. To work on an array where a single value, be that a
hard-coded value or a celle reference, you have to use an array formula.
|