How to select non-cosecutive cells in Excel formula - excel

I want to do a T.TEST of the values in rows (2,3,8,9) vs. (4,5,6,7). I tried doing T.TEST((D1:D2,D8:D9),D4:D7,2,2) but Excel didn't like it. Is there a way to skip rows like that?
As a test I tried SUM((D1:D2,D8:D9)) and did get the right result.

SUM will work with non consecutive cell ranges also. But TTEST needs two matrices, either from consecutive cell ranges or from array literals like {1;2;3;4} or {1,2,3,4}.
So you could use INDIRECT in collaboration with N to create a matrice from a non consecutive cell range.
=TTEST(N(INDIRECT({"D2";"D3";"D8";"D9"})),D4:D7,2,2)
Since TTEST needs matrices there, it is in array context already. So this formula needs not be inputted as an array formula using [Ctrl]+[Shift]+[Enter].
Note the "D2", "D3", ... within INDIRECT are text strings and not cell references. So they will not be updated if the formula is copied.

Related

Comparing comma separated numbers in cells

I am trying to compare the numbers in the Reachability Set column with the numbers in the same row of the Antecedent Set column and return the common values in the corresponding cells of the Intersection Set column.
Screenshot:
In Excel 2016 (but NOT Excel 2013), you can use the following array-entered formula.
=TEXTJOIN(",",TRUE,IFERROR(1/(1/(ISNUMBER(FIND(","&TRIM(MID(SUBSTITUTE(B2,",",REPT(" ",99)),seq_99,99))&",",","&A2&","))))*TRIM(MID(SUBSTITUTE(B2,",",REPT(" ",99)),seq_99,99)),""))
seq_99 is a Named Formula
Refers to: =IF(ROW(INDEX($1:$65535,1,1):INDEX($1:$65535,255,1))=1,1,(ROW(INDEX($1:$65535,1,1):INDEX($1:$65535,255,1))-1)*99)
To enter an array formula, after entering the formula in the cell, confirm by holding down ctrl + shift while hitting enter. If you do it correctly, Excel will place braces {...} around the formula.
Although you would think a VBA solution is required, it is actually quite simple to do this with formulae, provided you use a lot of helper columns. These can, of course, be hidden.
All you need is a number of columns equivalent to the maximum of the numerals in the sets, after each of the original columns of your table. For the example supplied, this would be 17 columns.
Here is a screenshot of the new table with the helper columns unhidden:
The follow formulae are entered into the top left cell of each coloured region and filled/copy-pasted/ctrl-entered into the rest of the cells.
Red Cells (entered into B2):
=IF(ISERROR(FIND(","&B$1&",",","&$A2&",")),0,1)
Green Cells (entered into T2):
=IF(ISERROR(FIND(","&T$1&",",","&$S2&",")),0,1)
Blue Cells (entered into AL2):
=IF(B2*T2,AL$1&",","")&AM2
And finally, the result entered into cell AK:
=LEFT(AL2,LEN(AL2)-1)
The formulae work by ensuring that all the numbers in the sets have a preceding, and trailing, comma so that they can be uniquely searched for.
Then it is a simple matter of constructing a grid for the sets where a 1 means the number exists in the set a 0 means it doesn't. Multiplying these two grids together results in the "intersection set".
Then it is a simple matter of reconstituting the result strings.
Caveat:
This solution won't work correctly if there are any spaces in the "Set" data. To overcome this you need to use the SUBSTITUTE() function.

Excel SUMIFS Multiple Criteria Cell Value

Similar questions to this have been asked but not exactly like this. There is one that is very close, but that solution is not working for me.
I have a function which returns comma separated values into a cell. I would like to pass the elements in that cell as criteria to a SUMIFS function using an approach like this one.
My attempt is pictured below:
I believe that this is somehow tied to the way that the function is understanding what is in cell G8. It looks like it is adding some extra quotes. If I highlight G8 in the formula bar and press F9, I get:
There are extra quotes on each side of each criteria.
I am open to a custom VBA function solution, but I would prefer something which can be built as a worksheet function. The criteria are returned from a custom VBA function that pulls elements out of a list box and does some regex work to remove extra commas. The number of elements that can be selected is variable so I would like to avoid having to split the criteria into more than one cell. Thanks.
Seems that the raw comma-separated criteria is in G6, All you need is to split this criteria into an array and feed it to SUMIFS. Splitting is available in VBA, but not exposed to Excel. All we need is to write a little UDF that does the splitting of the CSV and use it in our formula:
Function splitCSV(str As String)
splitCSV = Split(str, ",")
End Function
Now the formula in F10 would be:
=SUM(SUMIFS(C3:C10, B3:B10, "blue", A3:A10, splitCSV(G6)))
EDIT
The above is an array formula (Ctrl+Shift+Enter). To have it a normal formula we can use SUMPRODUCT instead of SUM. This leads to more flexibility (normal formula vs array formula) as well as some "expected" performance improvement.
=SUMPRODUCT(SUMIFS(C3:C10, B3:B10, "blue", A3:A10, splitCSV(G6)))

Excel array formula with wildcard

I have an array formula that refers to a drop down cell ($AG$7) to determine which cells to evaluate. This works well, however, I need to include an additional item in the drop down which is "All".
When this is selected, I want the array formula to use "*" to return all instances from the array, but i can't get it to work.
This is the formula I'm currently using;
={SUM(IF((tblSkillsMatrix[Role]=[#Role])*(INDIRECT("tblSkillsMatrix["&V$2&"]")=$AG$7),1,0))}
I've tried using
={SUM(IF((tblSkillsMatrix[Role]=[#Role])*(INDIRECT("tblSkillsMatrix["&V$2&"]")="*"&$AG$7),1,0))}
and
={SUM(IF((tblSkillsMatrix[Role]=[#Role])*(INDIRECT("tblSkillsMatrix["&V$2&"]")="*"&$AG$7&"*"),1,0))}
But these don't work.
Does anybody have any ideas?
Thanks
A explicit = comparison cannot use wildcards. COUTIFS and SUMIFS can. As far as I see, you want to count only (conditional sum 1 and 0).
Problem is, COUTIFS and SUMIFS will not deal with INDIRECT ranges. But INDIRECT can and should (because of its volatile bahavior) often replaced by INDEX- MATCH.
So:
=COUNTIFS(tblSkillsMatrix[role],[#role],INDEX(tblSkillsMatrix,,MATCH($V$2,tblSkillsMatrix[#Headers],0)),"*"&$AG$7)
If $AG$7 is empty then it counts independent of the column named in $V$2.
Btw.: Within a table (ListObject) this needs not be entered as a array formula.
This is not 100% replacement of your formula since it not works if $V$2 is empty and so no table column title is given. Your formula will then look at all columns but this is not possible using COUNTIFS where each additional range must have the same number of columns as the criteria_range1 argument. So if $V$2 shall also can be empty, then this will not work.
If so then you could use
{=SUM((tblSkillsMatrix[role]=[#role])*(LEFT(INDIRECT("tblSkillsMatrix["&$V$2&"]"),LEN($AG$7))=$AG$7))}
Advantage: both $V$2 and $AG$7can be empty.
Disadvantage: Volatile behavior of INDIRECT and this formula then must be a array formula even in a ListObject-table. It must be confirmed using Ctrl+Shift+Enter.

Difference between COUNTIF(range,"*") and {SUM(ISTEXT(range)*1)}

I just learned about using wildcards in Excel functions like COUNTIF(), and I wondered whether there is any circumstance under which the following two formulas would give different results:
=COUNTIF(B1:B10,"*")
{=SUM(ISTEXT(B1:B10)*1)}
The objective is to count the number of text (non-numeric) values in the range.
In the following example, B3 is the formulta =MID("abc",3,1) and B9 is the formula =SUM(1,2), so I know it works for formulas that return text or numeric values. But is there any odd type of cell content that would cause COUNTIF() and the SUM array formula to give different counts?

Excel formula for column of last negative value in range (no array formulas)

I have an Excel formula to find the column number of the last cell with a negative value in a row.
The problem is that it's an array formula, and the tool I need to use to create the spreadsheet (Apache POI SXSSF) doesn't support array formulas.
The values in the row are not ordered in any way.
The array formula that works when I make the spreadsheet by hand is
{=MATCH(2,1/(B18:M18<0))}
Is there a way to achieve the same result with a normal formula?
Thanks for any tips!
=MATCH(2,1/MMULT(1,-(B18:M18<0)))
Explanation
If the original formula is entered normally, the range B18:M18 is evaluated using implicit intersection (eg if entered in C5 it evaluates as =MATCH(2,1/(C18<0)) which returns #N/A). Entering the formula with CTRL+SHIFT+ENTER means that the range is evaluated as an array and not a single cell range.
MMULT is one of only a few functions that can return an array from a range of values. To make use of it here we first need to convert the array of boolean values to numbers using -(B1:B18<0) (or with N in place of -). Next we pre-multiply by 1 (i.e. a 1x1 matrix) to return the same array as if it had been array-evaluated. This works for horizontal arrays, for vertical arrays we need to post-multiply by 1 which means switching the arguments around.
This method works quite generally. As another example consider finding the largest negative value in the range using the formula =MAX(IF(B1:B18<0,B1:B18)). Again replacing B1:B18<0 by MMULT(1,-(B1:B18<0)) removes the need for array-entry. Without trying to come up with a totally different formula, i don't know of any other way to do this.

Resources