问题
I am struggling with an array formula, that logically seems sound, however it doesn't appear to be working correctly. I have been working on a complex sheet, that is not to include VBA, which has made it formula heavy, and using arrays instead.
In the image below, the first part is the problem, for the data shown in columns A-F, I wish to get a sum of the values that match the values in I1:K1.
The formula I have used to begin with can be seen in the first image also, this evaluates, pressing F9, to give me the desired output 20,40 & 50. However when I add the SUM
around the formula, I only get the first result out.
I think this is an issue with me not seeing the wood for the trees on this one.
Thanks in advance.
回答1:
This array formula seems to work:
=SUM((IFERROR(MATCH(A1:F1,I1:K1,0),0)>0)*A2:F2)
回答2:
There are probably multiple better formulas to achieve the same thing.
But to talk about why this fails:
It is because of the OFFSET function returns a reference rather than a value. And so used in this array formula it returns an array of references {B2,D2,E2}
instead of an array of values {20,40,50}
which leads to the problem.
If you are using:
=SUMPRODUCT(OFFSET(A2,0,MATCH($I$1:$K$1,$A$1:$F$1,0)-1))
then using Evaluate Formula, you will get:
SUMPRODUCT({#VALUE,#VALUE,#VALUE})
in next to last step and 0 as the result. So the OFFSET
leads to error values because of it returns an array of references which will not be dereferenced automatically and so will become #VALUE
error each.
If you are using
=SUMPRODUCT(N(OFFSET(A2,0,MATCH($I$1:$K$1,$A$1:$F$1,0)-1)))
then it works and returns 110. So the N
dereferences the references of each OFFSET
and so the whole formula leads to an array of values {20,40,50}
in sum.
{=SUM(N(OFFSET(A2,0,MATCH($I$1:$K$1,$A$1:$F$1,0)-1)))}
works too.
These problems occur using funktions like OFFSET
and INDIRECT
, which returns references rather than values, in array formulas. And having a dereferencing function around the OFFSET
or INDIRECT
stops the problems.
It is the same with:
=SUMPRODUCT(INDIRECT("R2C"&MATCH(I1:K1,A1:F1,0),FALSE))
versus
=SUMPRODUCT(N(INDIRECT("R2C"&MATCH(I1:K1,A1:F1,0),FALSE)))
来源:https://stackoverflow.com/questions/47529513/array-formula-confusion