EXCEL: Array in a cell. How to get the array back? - arrays

Let's say I have in B2 the formula =ROW(A1:A3). This formula gives an array of {1;2;3}. Because a cell only holds one value, cell B2 displays a value of 1. Any place in the worksheet, the formula =B2 gives 1, not the array. Yet, Excel still remembers the array because the formula is still in B2.
Is there any way to get the array back so it, the whole array, not its individual elements, can be used for further manipulation? I'm looking for something like OPERATION(B2) = {1;2;3}. For example, SUMPRODUCT(OPERATION(B2)) = SUMPRODUCT(ROW(A1:A3)) = 6.

As a workaround, you can store your formula in Name Manager, e.g.:
Then you can use it as a reference in Excel formulas, like =INDEX(Rows,2,1):

I realize that this is not the answer to the OP's question as they do not have the latest Office 365. I put this here for those who come later and do have it.
With the latest Office 365 with Dynamic array formulas this problem is now gone.
Putting =ROW(1:3) or the equivalent dynamic array formula =SEQUENCE(3) Excel will spill the array down automatically without the need of Ctrl-Shift-Enter:
And now one can refer to the array by only refering to A1 by putting # after the reference:
=SUM(A1#)
Then no matter how the array in A1 changes the other formula does not need to be changed.
The caveat is that the formula in A1 needs the room to spill down. If there is a cell that has anything in it that resides in the spill down array it will return an error.

Just to try and clarify this, arrays in Excel before the September 2018 update for Office 365 don't self-expand like they do in Google sheets, so if you put an array into a single cell you just get the first element of the array (see column A for example).
If you want to enter the array directly into the sheet, you have to select the cells you want it to occupy (say B2:B4) and enter it as an array formula (see column B) using CtrlShiftEnter
You can still use the array in another formula as long as it's expecting an array - e.g. with sumproduct you get the correct total, 6 (see column C).
=SUMPRODUCT(ROW(A1:A3))
Unfortunately this doesn't always work without entering the whole formula as an array formula (see column D).
=SUM(ROW(A1:A3))
EDIT
#JvDV is correct, you don't always get the first element of an array expression entered into a single cell - see this reference for explanation of the implicit intersection mechanism.

I have two methods, which might qualify as solutions or workarounds, depending on what technology you're willing to accept.
Both rely on retrieving the formula that produces the array from B2 and then evaluating that formula each time you invoke your OPERATION(B2).
Method 1: VBA
Create a UDF:
Function Arr(R as Range)
Arr = Application.Evaluate(R.Formula)
End Function
which you then invoke in a cell, e.g. =SUM(Arr(B2)).
Method 2: Name Manager + EVALUATE()
Note that this is different from Justyna's answer. The idea is to allow you to specify the address of the cell holding the array-generating formula.
Create a name that holds =EVALUATE(FORMULATEXT(INDIRECT(INDEX($1:$1048576;ROW();COLUMN()+1)))). This way you can specify that you want the content of the cell B2 by writing the string B2 to the nearest cell to the right of the cell in which you're using your newly created name.
Unfortunately, EVALUATE() cannot be used within a cell.

Related

Excel function to filter an array where a step change has occurred

I am trying to filter an array to only include those values that are not within a set distance of a change in that array.
Here's my example:
The input array increases monotonically in steps. I want to zero out any of the input values from N rows above to N rows below any point where the value changes. Cell $B$2 holds this "window" value.
For example, cell A9 (value 11) is different from cell A8 (value 5), so I want to zero out values that come from any cells from A7 to A11 inclusive. That is from -N to +N relative the cell where the change occurs, where N=2.
I can achieve the required output with non-array functions, using intermediate calculation columns, but since I am processing a large number of columns it would save greatly on spreadsheet real-estate if I could construct a single array formula that operates on the range A4:A25, and uses cell B1 for the size of N. I am using the current Excel Beta, so I have access to LET and LAMBDA if needed.
I have tried various combinations, using SEQUENCE, but I run into the issue that LET (or indeed any array functions) seems to fail if they contain two SEQUENCE statements.
I am continuing to work on this, but if anyone wants to have a try, I would be most grateful!
UPDATE: This formula does the trick, but needs to be copied down:
=A4*IF(AVERAGE(OFFSET(A4,-($B$1+1),0,2*($B$1+1),1))<>A4,0,1)
So, while the suggestion of #JvdV was not exactly what I was after, a hat-tip to them for the idea of offsetting the whole column up and down, which led to this:
=LET(input,A4:A25,up,OFFSET(input,-1*(B1+1),0),down,OFFSET(input,B1,0),input*IF(IFERROR(up*down,0)>0,IF(up=down,1,0),1))

Excel SUM of SUMIF/SUMIFS with dynamic multiple criteria

I need to pass a multiple criteria list (a constant array) via cell reference rather than hard-typing it into my formula.
So, instead of this:
=SUM(SUMIFS(sum_range,criteria_range,{"red","blue"}))
But I would need to use this:
=SUM(SUMIFS(sum_range,criteria_range,$A1)) where $A1 is {"red","blue"}
I understand that one can use a range of cells to pass an array but I really need my condition to come from a single cell.
It seems that passing a constant array via cell reference only passes the first element to the formula (i.e. only "red" is used as a condition) and all the working examples I could find of this (here or here) are hard-typing the condition into the formula.
Any luck anybody ?
EDIT: I should add that my data set includes blank rows so it is not contiguous and in general, I'm looking for a not too convoluted solution that will work most of the time and with as little restrictions and caveats as possible.
Change the "Array" in A1 to a comma delineated list:
blue,purple
No quotes or {}
Change the SUM to SUMPRODUCT and use this as the criteria:
TRIM(MID(SUBSTITUTE(A1,",",REPT(" ",99)),(ROW(INDEX(AAA:AAA,1):INDEX(AAA:AAA,LEN(A1)-LEN(SUBSTITUTE(A1,",",""))+1))-1)*99+1,99))
The $20 should be placed at the max number of choices possible. I just used it here as a placeholder, it can be more without problem but not less or it will skip any more than that.
Based on the formula you provided.
=SUMPRODUCT(SUMIFS(W$12:W$448,$I$12:$I$448,$I474,$J$12:$J$448,$J474,$K$12:$K$448,TRIM(MID(SUBSTITUTE(A1,",",REPT(" ",99)),(ROW(INDEX(AAA:AAA,1):INDEX(AAA:AAA,LEN(A1)-LEN(SUBSTITUTE(A1,",",""))+1))-1)*99+1,99))))
With cell A1 containing {"red","blue"} I then setup a named range Condition to which I assigned =EVALUATE($A1) and now I can pass my condition like so:
=SUM(SUMIFS(W$12:W$448,$I$12:$I$448,$I474,$J$12:$J$448,$J474,$K$12:$K$448,Condition))

Array formula to return an ARRAY without duplicates, without VBA

I would like to know whether it's possible to return an array from a single cell formula, which is filtered to remove duplicates, and which is built purely on Excel formulas.
I'm aware of approaches to return a list of values where the duplicates are removed (see this question), where the list is spread over multiple cells. However I specifically want to return an array intermediate.
E.g. For the list in A1:A5, I can get an array of values {0.1,0.2,0.2,0.7,0.3}, from which I want a second array {0.1,0.2,0.7,0.3}, as an intermediate in an array formula. Current approaches use single-end anchored ranges (like C$1:C1) to iterate through the items in the array geometrically (by dragging down column C). I would like to leave the array un-iterated, within the formula. I can then manipulate this as I would any other array.
All this should take place in a single cell if possible.
NB
Both MacroMarc's and Barry Houdini's answers are perfectly valid, and I ran a speed check on each - there was negligible difference (any difference was smaller than the variation between test runs). Both scored ~ 1.0±0.2 ms
I have used a defined name for the Range (A1:A5) and called it myList. You can do the same, or substitute in the Address $A$1:$A$5 if you wish:
{=INDEX(myList, N(IF({1}, MODE.MULT(IF(MATCH(myList, myList, 0) = ROW(myList), ROW(myList)*{1,1})))), 1)}
EDIT: Above wasn't robust to handle if the column list is further down the sheet, and a shorter minrow routine courtesy of OP:
{=INDEX(myList, N(IF({1}, MODE.MULT(IF(MATCH(myList, myList, 0)=ROW(myList)-MIN(ROW(myList))+1, (ROW(myList)-MIN(ROW(myList))+1)*{1,1})))), 1)}
This should be ok for you. Needless to say, these are array formulas..
This formula will return a sorted array without duplicates, e.g. for your example
{0.1;0.2;0.3;0.7}
=SMALL(IF(MATCH(A1:A5,A1:A5,0)=ROW(A1:A5)-ROW(A1)+1,A1:A5),ROW(INDIRECT("1:"&SUM(0+(0<(FREQUENCY(A1:A5,A1:A5)))))))
confirmed with CTRL+SHIFT+ENTER
......or this version will keep the order
=INDEX(A1:A5,N(IF({1},SMALL(IF(MATCH(A1:A5,A1:A5,0)=ROW(A1:A5)-ROW(A1)+1,ROW(A1:A5)-ROW(A1)+1),ROW(INDIRECT("1:"&SUM(0+(0<(FREQUENCY(A1:A5,A1:A5))))))))))
Use this array formula to sum the unique, it requires the use of TEXTJOIN() which is only available with Office 365 Excel:
=SUM( IF(ISERROR(FIND(TRIM(MID(TEXTJOIN(REPT(" ",999),TRUE,A:A),(ROW(INDIRECT("1:" & COUNTA(A:A)))-1)*999+1,999)),MID(TEXTJOIN(REPT(" ",999),TRUE,A:A),1,(ROW(INDIRECT("1:" & COUNTA(A:A)))-1)*999))),--TRIM(MID(TEXTJOIN(REPT(" ",999),TRUE,A:A),(ROW(INDIRECT("1:" & COUNTA(A:A)))-1)*999+1,999))))
Being an array formula it needs to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.
You can replace SUM with many different formula.
If one does not have Office 365 Excel then vba and or helper columns are the only method available.
Edit:
To remove the False from the array and return the 3rd non duplicate in the array we can wrap it in another TEXTJOIN:
=--TRIM(MID(TEXTJOIN(REPT(" ",999),TRUE,IF(ISERROR(FIND(TRIM(MID(TEXTJOIN(REPT(" ",999),TRUE,A:A),(ROW(INDIRECT("1:" & COUNTA(A:A)))-1)*999+1,999)),MID(TEXTJOIN(REPT(" ",999),TRUE,A:A),1,(ROW(INDIRECT("1:" & COUNTA(A:A)))-1)*999))),--TRIM(MID(TEXTJOIN(REPT(" ",999),TRUE,A:A),(ROW(INDIRECT("1:" & COUNTA(A:A)))-1)*999+1,999)),"")),(3-1)*999,999))
The 3 in the (3-1)*999 can be replaced by anything that returns the Index number desired.
Still an array formula that needs Ctrl-Shift-Enter.
If you want to return the relative position in then use this array:
=AGGREGATE(15,6,ROW(INDIRECT("1:" & COUNTA(A:A)))/(--TRIM(MID(TEXTJOIN(REPT(" ",999),TRUE,IF(ISERROR(FIND(TRIM(MID(TEXTJOIN(REPT(" ",999),TRUE,A:A),(ROW(INDIRECT("1:" & COUNTA(A:A)))-1)*999+1,999)),MID(TEXTJOIN(REPT(" ",999),TRUE,A:A),1,(ROW(INDIRECT("1:" & COUNTA(A:A)))-1)*999))),--TRIM(MID(TEXTJOIN(REPT(" ",999),TRUE,A:A),(ROW(INDIRECT("1:" & COUNTA(A:A)))-1)*999+1,999)),"")),(ROW(INDIRECT("1:" & COUNTA(A:A)))-1)*999,999))=B1),1)
I gave you the long formula for creating the array but for the sum:
=SUMPRODUCT(A1:A5/COUNTIF(A1:A5,A1:A5))
would be sufficient.
And for the Average:
=SUMPRODUCT(A1:A5/COUNTIF(A1:A5,A1:A5))/SUMPRODUCT(1/COUNTIF(A1:A5,A1:A5))
There are many work arounds that are shorter and better if you know what you want.

Excel - Passing a cell containing an array formula as input to a function in another cell

I was wondering if in excel there was a way to do this:
In cell A1 write an array function (e.g.INDEX(some_array,r_n,0) where some_array is a square NxM array and r_n is the row number, commited using Ctrl+Space+Enter)
In cell B2 calculate the i-th percentile of array in cell A1. I would
like to write something like PERCENTILE.INC(A1,i/100), which, alas, does not work, returning the first element of the array in A1.
Currrently I'm passing the formula in A1 to percentile, but it's a bit cumbersome.
Thanks in advance
Currently there seems to exist a functionality for this, in case someone stumbles upon the same problem as I did.
Making use of the spill range functionality, you just need to append the '#' char to the address of the first cell of the spill range you want to use as part of some function, e.g. 'A1#'.

Excel - pass an array into a function

On one sheet in my file I have a number of arrays defined as named ranges. One another sheet I'd like to use a drop down, or something similar to select the name of one of the named range and have the data/contents of that named range populate a range on the second sheet. Is this possible WITHOUT VBA? Is there an array formula that will do this?
One, not entirely elegant, method that I've thought of is using the index function and copy this within a range of cells equivalent in size to the size of the largest named range. Something like:
=INDEX(range_1,ROW(),COLUMN())
This requires me to be able to pass the name of a named range into a function though. Is this possible?
Answers to either or both of these questions would be greatly appreciated.
Without that, the only other way I can think to do this is using a brute force, offset look up, which I'd prefer not to do.
Thanks for your help.
Indirect might do what you want it to.
In Sheet1 I created 3 named ranges:
Then in Sheet2, I
1) Put these names in Column A
2) Used a data validation list linked to column A to place a name in a cell (C2)
3) Used the array formula {=INDIRECT(C2)} (accepted with Ctrl+Shift+Enter) in the cells that I wanted to hold the array (C4:E5)
When C2 is changed via the drop-down, the array is automatically changed:

Resources