Google sheets arrayformula and ranges rules - google-sheets

I was looking a the formula for cumulative sum using arrayformula:
ARRAYFORMULA(SUMIF(ROW(A1:A10), "<=" & ROW(A1:A10), B1:B10))
I used arrayformulas before, but mostly for simple arithmetic, so I am trying to wrap my head around this.
The reason it is unclear for me is that I don't know what the rules are for ARRAYFORMULA to use the range, or to use the scalar value.
If I tabulate it it becomes something like this:
row 1: SUMIF(ROW(A1:A10), "<=" & ROW(A1), B1:B10))
row 2: SUMIF(ROW(A1:A10), "<=" & ROW(A2), B1:B10))
...
row n: SUMIF(ROW(A1:A10), "<=" & ROW(An), B1:B10))
But that doesn't actually work because ROW doesn't work like that if not used in an ARRAYFORMULA, according to the docs:
ROW([cell_reference])
if cell_reference is a range more than one cell wide and the formula is not used as an array formula, only the numeric value of the first row in cell_reference is returned.
Looking at the syntax for SUMIF it is:
SUMIF(range, criterion, [sumrange]).
Do I understand it correctly that the rules for ARRAYFORMULA are:
If a function parameter inside ARRAYFORMULA is expected to be a scalar (criterion in the examle) and a range is given it will expand in the cells below iterating this range
If a function parameter inside ARRAYFORMULA is expected to be a range (range and sumrange) and a range is given it will pass on the range to the formula
So, the question is:
What is exactly happening row by row with the formula pasted above and how does ROW behave in this? ROW is 'aware' that it is being used in an arrayformula and behaves differently?

It's not about the ROW. Its more about the & and the strings used. Used inside a arrayformula, it creates series(a array) of strings. So, "<="&ROW(A1:A10) becomes "<1","<2" and so on. Criterion argument of SUMIF expects a string and only a single string. Given the array, it creates a array of results for each criterion, the Range argument being the same for each criterion. Also, SUMIF already expects array for the range argument.

Related

Logicial formula : AND of multiples OR

I am trying to type a logical formula like that :
=AND(OR(A1=0;B1);OR(A2=0;B2);OR(A3=0;B3);...;OR(An=0;Bn))
But I own multiples rows and I am searching for a faster way to achieve this operation.
Any idea ?
You can use ARRAYFORMULA()
ARRAYFORMULA
Enables the display of values returned from an array formula into multiple rows and/or columns and the use of non-array functions with arrays.
Sample Formula:
=AND(ARRAYFORMULA(IF(A2:A8=0,1,0)+(B2:B8)))
what this formula does is performing OR operation using "+" for Column A and Column B. Once the OR operation was performed, ARRAYFORMULA will return an array of results which you can use in your AND operation.
Example:
In the first scenario, when you performed an OR operation within the ARRAYFORMULA (See Cell C2, formula was shown in Cell C1) It will return an array of OR operation results per row. Then if you check the formula used in Cell D2, we just get the AND operation of the array results from ARRAYFORMULA
NOTE:
You cannot use AND() or OR() functions inside the ARRAYFORMULA(), instead we use '*' for AND() and '+' for OR().
Reference: ArrayFormula and "AND" Formula in Google Sheets

Google Sheets VLOOKUP across Mismatched Ranges in Multiple Sheets

This formula should look up in the A column of multiple sheets and when the match is found (in this case there are not duplicates in any A column of the different sheets), it gives back the value found in the cell next on the right to the match.
=ArrayFormula(IF(LEN(B5),VLOOKUP(B5,{SHEET1!$A$3:$B,SHEET2!$A$15:$B},2,FALSE),""))
But it gives a mismatch error and even if I make the ranges the same length the value in B5 is searched only into the first sheet of the range, in this case SHEET1!$A$3:$B.
Is is possible to make the formula work with ranges of different length from multiple sheets?
always when you construct the virtual array with array brackets {} both sides needs to be of the same size.
={A1:A10, B1,B10}
or:
={A1:C10; D1:F10}
in your case, the array literal error comes from mismatched rows when you use "infinite" rows by not specifying the end row. eg your sheet1 has more or fewer rows then your sheet2
=INDEX(IF(LEN(B5), VLOOKUP(B5, {SHEET1!A3:B; SHEET2!A15:B}, 2, 0), ))

Google Sheets - Conditional Formatting on Closest Value

I have a column of data and a target value and am wanting to apply a rule that will highlight the closest value to that target value from the column of data possible. I have tried a few different forumla and nothing has worked so far. This is what I am currently working with:
Target Number is in I3
Data is in I4:I24
=ABS($I$3-I4)=MIN(ABS($I$3-$I4:$I24))
This is all being done through Google Sheets (not sure if this is relivant but thought it couldn't hurt being included)
First of all, let's change $I4:$I24 to $I$4:$I$24, so that this array is the same for all cells that the formula is applied to.
The - operator only works for numbers, not arrays. So when you subtract an array from a number, it returns just the value minus the first number in the array. The same goes for the ABS function - it can't take an array.
To instead have the - operator and ABS function apply to each element of the array individually, you need to use the ARRAYFORMULA function. To do this, wrap the ABS($I$3-$I$4:$I$24) in ARRAYFORMULA - that is, ARRAYFORMULA(ABS($I$3-$I$4:$I$24)). Now, ABS and - apply to each element of the array individually, and return an array of all the results. This can then be passed into MIN.
Now we get
=ABS($I$3-I4)=MIN(ARRAYFORMULA(ABS($I$3-$I$4:$I$24)))
Try
=ArrayFormula(ABS($I$3-I4)=MIN(ABS($I$3-$I$4:$I$24)))

Google sheets, SUMIF function gives: "Argument must be a range" error if I use ADDRESS function as second parameter of range

I am writing a formula in Google Sheets. Here's the formula;
=IF(COUNTIFS(C$3:ADDRESS(ROW(INDEX(A2:A,COUNT(A2:A))),3), C3, E$3:ADDRESS(ROW(INDEX(A2:A,COUNT(A2:A))),5), E3, F$3:ADDRESS(ROW(INDEX(A2:A,COUNT(A2:A))),6), "<>0") > 1, "MULTIPLE POSITIVE LBS THAT'S GREATER THAN ZERO", MINUS(F3, SUMIFS(G$3:G$480, C$3:C$480, C3, E$3:E$480, E3)))
Now the formula works like this but it only sums the values between row 3 and 480. What I want to do is get all the values between row 3 and the last non-empty field. In order to achieve that I used this formula;
ADDRESS(ROW(INDEX(A2:A,COUNT(A2:A)))
I was able to make this formula work for countif function but when I use the same formula in sumif function, it gives me Argument must be a range error.
This is the second version (the one giving an error)
=IF(COUNTIFS(C$3:ADDRESS(ROW(INDEX(A2:A,COUNT(A2:A))),3), C3, E$3:ADDRESS(ROW(INDEX(A2:A,COUNT(A2:A))),5), E3, F$3:ADDRESS(ROW(INDEX(A2:A,COUNT(A2:A))),6), "<>0") > 1, "MULTIPLE POSITIVE LBS THAT'S GREATER THAN ZERO", MINUS(F3, SUMIFS(G$3:ADDRESS(ROW(INDEX(A2:A,COUNT(A2:A))),7), C$3:ADDRESS(ROW(INDEX(A2:A,COUNT(A2:A))),3), C3, E$3:ADDRESS(ROW(INDEX(A2:A,COUNT(A2:A))),5), E3)))
I also tried to make ADDRESS function work independently, in a completely different cell and it gives the correct address.
This is the first time I'm writing sheets or any kind of excel formula so I couldn't find the source of the problem. What am I doing wrong?
Replacing ADDRESS(ROW(INDEX(A2:A,COUNT(A2:A))) with C$3:INDEX(C2:C,COUNT(A2:A)) did the trick. Apparently ADDRESS returns a string rather than a reference.
Address function returns a string. Which is not accepted as an input for sumif function So you have to convert the string returned by the address function to a reference. To do this, use the indirect function.
Like so: INDIRECT(address(2,row()-1,4,TRUE,"MAIN"))
Now you can use this as an input to many other functions like sumif, column, row, etc.

Preprocessing each cell in range in Google Spreadsheet Formula

I'm trying to sum a range of values, but the values in my range comes from a formula resulting in string values (string user input, number extracted with left()). This makes =sum(A:A) not work.
What I would like to do is something along the lines of =sum(value(A:A)), but I need value() to apply to each cell before they are all summed up.
I would like a general solution applicable to any preprocessing formula.
Try this one:
=sum(ARRAYFORMULA(--A:A))
BTW, you could also double minus each number before summing: = --left(B1) will give the number. If A:A contains text, then use =sum(IFERROR(ARRAYFORMULA(--A:A)))

Resources