Question

Has anyone had experience of using the Excel OFFSET or INDIRECT spreadsheet functions inside a range formula?

I want to use a sum a column of values, but with a horizontal offset specific to each row, where the size of the offset is a different column. So e.g. if the data in range A1:C3 is:

[1  2  3;
 1  2  3;
 1  2  3]

And the offset given in D1:D3 is [0; 1; 2], I want to use some syntax like {=sum(offset(A1:A3, 0, D1:D3)} to give 6.

Another option would be to use the INDIRECT function, using syntax like {=SUM(INDIRECT("R"&ROW(A1:A3)&"C"&D1:D4,FALSE))}.

Both of these return the result as it if was just the first element of the matrix and vector, i.e. 1. Is this a restriction on these functions, or there is a way around this? Thank you.

Was it helpful?

Solution

(the answer is completely reedited after the discussion in the comments)

The OFFSET function is of no use here, as it operates with ranges and not with individual cells.

For the SUM function, everything is quite easy {=SUM(A1:C3*(COLUMN(A1:C3)=D1:D3))}. Or you might even avoid using the arrays with =SUMPRODUCT(A1:C3,(COLUMN(A1:C3)=D1:D3)). Note that column D now should refer to a specific column number and not to an offset from the first column, i.e. 1;2;3.

For other functions, it's more difficult a) because they count zeros (false values) from the matrices; b) because they usually don't work with non-contiguous arrays (and any attempt to exclude zero-values from the matrix runs into the #value error). However, the combination of FREQUENCY and INDEX does the trick:

  • finding average: {=(SUM(A1:C3*(COLUMN(A1:C3)=D1:D3)))/INDEX(FREQUENCY((A1:C3*(COLUMN(A1:C3)=D1:D3)),0),2)}
  • finding median: {=MEDIAN(LARGE((A1:C3*(COLUMN(A1:C3)=D1:D3)),ROW(INDIRECT("1:"&INDEX(FREQUENCY((A1:C3*(COLUMN(A1:C3)=D1:D3)),0),2)))))}

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top