Determing what cells covered by a single contiuous array formula - excel

Suppose in cells B1:B3 I have the following formula:
{=A1:A6}
Then cells B4:B6 I enter the same formula. The result will look like this:
A B
1 1
2 2
3 3
4 1
5 2
6 3
But casual inspection of the formulas would falsely suggest that B1:B6 are a single array formula, like this:
A B
1 {=A1:A6}
2 {=A1:A6}
3 {=A1:A6}
4 {=A1:A6}
5 {=A1:A6}
6 {=A1:A6}
In this contrived example, it's easy to look at the results and see that there are really two array formulas in B. Is there a reliable way to check this in situations where the data doesn't make it so obvious?
Ideally I would like to see a box around B1:B3 and one around B4:B6 showing that there are distinct blocks.

Answering my own question with the best I could find -- would still love to see a better answer:
You can select a cell in a multi-cell array formula and hit Ctrl+/. This will cause the whole multi-cell array formula to be selected.

Related

Finding uninterrupted sub-arrays in Excel - Kadane's algorithm variation?

Suppose you have an ordered, indexed list of positive values. These positive values are interrupted by 0 values. I want to determine if a consecutive sub-array exists which is not interrupted by 0 values and whose sum exceeds a certain threshold.
Simple example:
Index, Value
0 0
1 0
2 3
3 4
4 2
5 6
6 0
7 0
8 0
9 2
10 3
11 0
In the above example, the largest consecutive sub-array not interrupted by 0 is from index 2 to index 5 inclusive, and the sum of this sub-array is 15.
Thus, for the following thresholds 20, 10 and 4, the results should be FALSE, TRUE and TRUE respectively.
Note I don't necessarily have to find the largest sub-array, I only have to know if any uninterrupted sub-array sum exceeds the defined threshold.
I suspect this problem is a variation of Kadane's algorithm, but I can't quite figure out how to adjust it.
The added complication is that I have to perform this analysis in Excel or Google Sheets, and I cannot use scripts to do it - only inbuilt formulas.
I'm not sure if this can even be done, but I would be grateful for any input.
Start with
=B2
in c2
then put
=IF(B3=0,0,B3+C2)
in C3 and copy down.
EDIT 1
If you were looking for a Google sheets solution, try something like this:
=ArrayFormula(max(sumif(A2:A,"<="&A2:A,B2:B)-vlookup(A2:A,{if(B2:B=0,A2:A),sumif(A2:A,"<="&A2:A,B2:B)},2)))
Assumes that numbers in column B start with zero: would need to add Iferror if not. It's basically an array formula implementation of #Gary's student's method.
EDIT 2
Here is the Google Sheets formula translated back into Excel. It gives you an alternative if you don't want to use Offset:
=MAX(SUMIF(A2:A13,"<="&A2:A13,B2:B13)-INDEX(SUMIF(A2:A13,"<="&A2:A13,B2:B13),N(IF({1},MATCH(A2:A13,IF(B2:B13=0,A2:A13))))))
(entered as an array formula).
Comment
Maybe the real challenge is to find a formula that works both in Excel and Google sheets because:
Vlookup doesn't work the same way in Excel
The offset/subtotal combination doesn't work in Google sheets
The index/match combination with n(if{1}... doesn't work in Google sheets.
With data in columns A and B, insure column B end with a 0. Then in C2 enter:
=IF(AND(B3=0,B2<>0),SUM(B$1:$B2)-MAX($C$1:C1),"")
and copy downwards:
Column C lists the sums of consecutive non-zeros. In another cell enter something like:
=MAX(C:C)>19
where 19 is the criteria value.
You can avoid the "helper" column by using a VBA UDF.
EDIT#1:
Use this instead:
=IF(AND(B3=0,B2<>0),SUM(B$1:$B2)-SUM($C$1:C1),"")
Thanks to #Tom Sharpe and #Gary's Student for answering the question.
While I admittedly did not specify this in the question, I would prefer to achieve the solution without a helper column because I have to do this operation on 30+ successive columns. I just didn't think it was possible in Excel.
Full credit goes to user XOR LX on the Excelforum for coming up with this solution. It has blown my mind and took me the better part of an hour to wrap my head around, but it is certainly very creative. There is no way I could have come up with it myself. Re-posting it here for the benefit of everyone who is looking into this.
Copy and paste the table from my initial question into an empty Excel sheet such that the headers appear in (A1:B1) and the values appear in (A2:B13).
Then enter this formula as an array formula (ctrl+shift+enter), which gives the max of the sums of all the uninterrupted sub-arrays:
=MAX(SUBTOTAL(9,OFFSET(B2,A2:A14,,-FREQUENCY(IF(B2:B13,A2:A13),IF(B3:B14=0,A2:A13,0))-1)))
Note the deliberate offset to include one additional row below the end of the dataset.

Excel formula: how to get sum of calculated values?

I have following data in Excel:
A B C
1 Task Name Total Effort (days) %Finished
2 Task 1 5 30
3 Task 2 8 25
4 Task 3 23 18
5 Task 4 32 5
All I want to get is "=SUM(B2*C2 : B5*C5)". But Excel doesn't allow me to do this. How do I achieve my goal?
This is simple with SUMPRODUCT:
=SUMPRODUCT(B2:B5,C2:C5)
To elaborate, while #Olly's answer SUMPRODUCT(B2:B5, C2:C5) is indeed correct, however I got impression from your original post that you are a bit confused about the selectors in hand - I do believe you should study up on it before even copy-pasting any formula.
In excel formulas, if you are refering to a range of cells, you do it by declaring the range from it's starting point, to it's ending point with a colon.
So for example, the green range would be A1:A5, the blue range would be C3:E3 and the yellow range would be F5:G11
Next we have separators, these seperate individual ranges (or cells - even an individual cell is a range itself, a range with a size of 1 cell)
For example with the following range, we would select it as (A1:A2, A5, A7:A8) notice the comma (,) acting as a separator.
Now as to how =SUMPRODUCT actually works, it takes two ranges, pits them side by side. It then does multiply the first cell of the first range with the first cell of the second range and so on. In the end, it adds them all together.
That sounds a bit difficult in words. Here is an illustration
So yes, this actually means the individual values don't even have to be side by side.
Hopefully you understand now not only how your code should look like, but also why your original code didn't work! :)

Excel - Formula that Counts the First of each Duplicate

I have a spreadsheet that looks like follows:
On the lefthand side, column A, I have Bug numbers that corresponds to a specific bug in our Bugzilla.
I am trying to find a way to place, in a single cell, the amount of Bugs that have duplicates, each of them only counted once.
For example, say I have this:
Col A
1
1
2
3
3
3
4
4
5
I would like to have the formula return me 3 because I have 3 numbers that have duplicates, but each of them only counted once.
Use COUNTIF and SUMPRODUCT:
=SUMPRODUCT((COUNTIF($A$1:$A$9,$A$1:$A$9)>1)/COUNTIF($A$1:$A$9,$A$1:$A$9))
To deal with blanks we need to use SUM(IF()) in an array:
=SUM(IF(((COUNTIF($A$1:$A$9,$A$1:$A$9)>1)*($A$1:$A$9<>"")),1/COUNTIFS($A$1:$A$9,$A$1:$A$9,$A$1:$A$9,"<>")))
being an array formula it needs to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.

Highest Sum Value

I've been googling for a while being unable to find my answer.
I'm trying to get a single cell formula that returns with the Highest Sum of a listed item. So for example I have:
Value Name
4 James
5 James
1 Kari
50 (Blank)
7 Kari
3 James
Kari's has 8 days total, James has 12 total, and "Blank" is discarded. I'd like the formula to return just "12". I can get this to work using Max(), and Sumifs(), but that requires additional fields and formulas. Is there a clean way to do this in one in a single cell formula?
Use this as an array formula:
=MAX(SUMIFS($A$2:$A$7,$B$2:$B$7,$B$2:$B$7,$B$2:$B$7,"<>"))
Being an array formula it needs to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode. If done correctly then Excel will put {} around the formula.

Count the quantity of unique combinations in multiple columns in Excel

I need to have excel count the number of times number pairs occur in the same row regardless of their order. The following is what I'm look for. Column C will display the number of times A & B contain the same numbers, but not necessarily in the same columns (or order). Example below: 6 2 and 2 6 should be considered the same thing. Therefore the count in Column C should = 2 for both 6 2 and 2 6.
My Objective:
I tried the pivot table suggested at the following link and it successfully counted matching pairs, but for example 6 2 and 2 6 were not considered the same and the count was only 1 for each.
This simple pivot table solution almost works
Thank you! They all seem to work, but the easiest solution I found was here Quick to copy for large data
Use this array formula:
=SUM(COUNTIFS(A:A,A1:B1,B:B,TRANSPOSE(A1:B1)))
Being an array formula it must be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode. If done correctly then excel will put {} around the formula.
Use the solution from SuperUser you posted, but use:
=CONCATENATE(MAX(A2:B2),MIN(A2:B2))
I'm trying to think of a single - formula solution using COUNTIFS but can't find one at the moment so here's a two formula version.
In column D =IF(A2<B2,A2&", "&B2,B2&", "&A2)
In column C =COUNTIF(D:D, D2)
This creates a list in column D with the following logic;
If A < B then A goes first
If A > B then B goes first
If A = B then B goes first but it doesn't really matter
The list will be a set of strings which we then count.

Resources