Excel Index with count function no working? - excel

I'm trying to use an index with count function to return the last number in a specific array.
I have numeric data in C6:C17 and I want that F6 returns the last value from that array (C6:C7).
I've used this formula: =INDEX(C6:C17;COUNT(C6:C17)) and it works perfectly.
But when I use this formula: =INDEX(C6:C17;COUNT(C6:C17)-4), it returns the value of the fourth previous row. If I only have values on C6, C7, C8, C9 and the rest is empty, it returns the value of C6. If I choose F7 instead of F6 to return the value, the same formula gives me the value of C7.
It shouldn't give me an error, since my -4 is now outside of the array. It only happens in the row before the row where the array starts.
If I put (-5) in the same formula and with the same values, it gives me an error.

That is because 0 is a viable option for the row return:
=INDEX(C6:C17;0)
Will return the entire range as an array and since that array is being put in the one cell and not many through an array formula it returns the first value only of the array, C6.
Negatives are not a viable option:
=INDEX(C6:C17;-1)
will error.
If you want to stop that from happening you need to test if 0. A quick way is to get the reciprocal of the reciprocal:
=INDEX(C6:C17;1/(1/(COUNT(C6:C17)-4)))
This will now throw an error instead of returning the value if the count is less than 5.
Or you can just test and return a negative:
=INDEX(C6:C17;IF(COUNT(C6:C17)>4;COUNT(C6:C17)-4;-1))

Related

Excel Find the first zero after a non zero within a range

I have a range, that contains data which follows a distribution *typically starts at 0, then goes up, then down, and finally back to zero.
I want to return the value of another column at the point that the data returns back to Zero (in my case you can see that Cell B6 is the point at which this event occurs, and I want my cell C2 to return the value "E" from cell A6...
Currently, the best I can get to is the following:
=INDEX(A2:B8,FIND(0,
TEXTJOIN(,,B2:B8),
MATCH(AGGREGATE(4,4,B2:B8),B2:B8,0)) -1,1)
The problem comes in with the fact that there can be any number of zeros before the data appears.
i.e. like this. = Where I would still expect the highlighted match.
1] 1st zero after a non-zero value, in C2 enter formula :
=INDEX(A:A,INDEX(MATCH(1,1/B:B),0)+1)
2] Nos. of zero after a non-zero value, in D2 enter formula :
=MATCH(9^9,B:B)-INDEX(MATCH(1,1/B:B),0)
For excel you can use INDEX/AGGREGATE:
=INDEX(A:A,AGGREGATE(15,6,ROW(B2:B8)/(B2:B8=0),2))
Edit:
If you want get first zero value after last non zero value use formula:
=INDEX(A:A,AGGREGATE(14,6,ROW(B2:B8)/(B2:B8<>0),1)+1)

Why does the SEARCH function works only in combination with SUMPRODUCT when searching for multiple strings?

I'm checking in Excel if a text in a cell in a column is within the text at another column.
Example text to search:
Column A
1. a
2. b
3. c
Text to search within:
Column B
1. aagg
2. hgjk
3. dhhd
4. bahj
5. adjd
This formula works:
=SUMPRODUCT(--ISNUMBER(SEARCH(A$1:A$3,B1)))
But this one works only for the first cell and for the rest I get #VALUE! error:
={(SEARCH(A$1:A$3,B1))}
Column B
aagg -> 1
hgjk -> #VALUE!
dhhd -> #VALUE!
bahj -> empty cell
adjd -> empty cell
Also the second formula is only giving results for the first three cells, as many as the number of cells in column A. The last two are empty.
What makes SEARCH work with SUMPRODUCT but cannot work alone as an array formula?
Let's assume we have the following data...
A1:A3
a
b
c
B1
oboe
In this case, SEARCH(A$1:A$3,B1) returns the following array of values...
#VALUE!
2
#VALUE!
If the formula is entered in a single cell, only the first value from this array gets transferred to this cell. So, in this case, #VALUE! will be display in the cell.
However, if you select three cells, then enter the formula, and then confirm with CTRL+SHIFT+ENTER, all three values from the array gets transferred to these cells.
Note, though, since A1:A3 is a vertical range of cells, you'll need to select a vertical range of cells in which to return these values. For example, you could select D1:D3, then while these three cells are selected enter the formula, and then confirm with CTRL+SHIFT+ENTER.
Now, for the interesting part. To return TRUE or FALSE, you'll need to first pass the array of values returned by SEARCH to ISNUMBER, and then you'll need to pass the array of values returned by ISNUMBER to the OR function, which in turn will return TRUE if at least one of the values returned by ISNUMBER is TRUE. Otherwise, it returns FALSE. So, you would use the following formula, which needs to be confirmed with CTRL+SHIFT+ENTER...
=OR(ISNUMBER(SEARCH(A$1:A$3,B1)))
Here's how the formula is evaluated...
=OR(ISNUMBER(SEARCH(A$1:A$3,B1)))
=OR(ISNUMBER(SEARCH({"a";"b";"c"},"oboe")))
=OR(ISNUMBER({#VALUE!;2;#VALUE!}))
=OR({FALSE;TRUE;FALSE})
...which returns TRUE. By the way, ISNUMBER is needed here. We can't simply pass the array of values returned by SEARCH to the OR function. If we did, the OR function would return #VALUE!, since the array of values contains an error value, in this case #VALUE!. That's why we use ISNUMBER, so that any error value gets converted to FALSE before passing it the OR function. So the OR function will always get an array of values made up of TRUE and/or FALSE.
Hope this helps!

Apply functions like MEDIAN to individual elements in an array formula - Excel

I am using the MEDIAN function to calculate the overlap of two date ranges.
The formula looks something like this
=MEDIAN(A18,C18+1,$C$8+1)-MEDIAN(A18,C18+1,$B$8)
A18 & C18 are date range 1
B8 & C8 are date range 2
I would like to turn this into an array formula similar to this to sum the results for multiple rows
{=SUM(MEDIAN(A18:A24,C18:C24+1,$C$8+1)-MEDIAN(A18:24,C18:24+1,$B$8))}
This only works if MEDIAN is applied separately to each element in the arrays A18:A24 & C18:C24.
Currently, Excel concatenates the arrays contained within each MEDIAN call and returns the overall median, leaving only one value for SUM to sum. And that is the 'wrong' result for what I need to do.
Is there a way of forcing excel to apply MEDIAN to each element in my arrays? Ie is there a way to force excel to return an array from the MEDIAN function.
If this is possible it allows me to avoid a 120,000 element array or a macro enabled workbook.
Cool MEDIAN formula. But you can't use MEDIAN as you desire with an array formula since MEDIAN always returns a single result (not an array).
I have a solution to your problem, but it's somewhat sluggish/respetitive:
(I have included comments to the right of the formula to explain what is happening)
= IF(C18:C24>C8, // (For each cell in C18:C24) If the value is greater than C8
C8, // Then C8 is the upper bound
C18:C24) // Else, the specific value from C18:C24 is the upper bound
- // (minus sign)
IF(A18:A24<B8, // (For each cell in A18:A24) If the value is less than B8
B8, // Then B8 is the lower bound
A18:A24) // Else, the specific value from A18:A24 is the lower bound
This returns an array of the overlaps of date ranges, except that it is possible that these results will return negative numbers (if an overlap doesn't occur), but in this case you want to return 0.
You can get around this with another IF statement.
= IF(formula < 0, 0, formula)
Where formula is the first formula I have above.
Side note: Normally in this situation, you'd be able to avoid typing out formula twice with something like this:
= MAX(formula,0)
Except this won't work when the formula itself returns an array since MAX only returns a single result. This is a similar problem you are experiencing with MEDIAN in the first place.
All in all without comments, this is the formula to sum up the values:
= SUM(IF((IF(C18:C24>C8,C8,C18:C24)-IF(A18:A24<B8,B8,A18:A24))<0,0,
IF(C18:C24>C8,C8,C18:C24)-IF(A18:A24<B8,B8,A18:A24)))
Of course, this is an array formula, must be entered with Ctrl+Shift+Enter instead of just Enter.

If statements in excel not consistent?

In my excel file, I am trying to determine if a set of numbers is greater than the value of another cell.
I have written out an IF statement that only works sometimes depending on the range of the set of numbers. I have been able to replicate the problem but can't understand what is going wrong. I have included an example of my problem.
"B4" is equal to 2
"'Data Sheet'!A1:A10" is a set of 10 numbers from 1 to 10 inclusive.
My first statement (seen below) is working fine:
=IF(B4>'Data Sheet'!A1:A10,"Pass", "Fail")
which returns Fail, as expected.
However, when I change the statement to:
=IF(B4>'Data Sheet'!A6:A10,"Pass", "Fail")
It returns "#VALUE!".
Why does the range of values in the IF statement make a difference to whether it works or not?
Your formula is an array formula, and it is probably due to serendipity that your first formula is returning what you expect as an answer.
When you enter it into a cell as a non-array formula, which is what you are doing, it will return the value from the column A range that is in the same row as the formula.
Had you entered the first formula in Row 1, it would have returned "Pass" since it would be comparing B4 with A1.
If it is not in a row corresponding to a row in the range, it will return #VALUE.
If you confirm the formula as an array formula, by holding down ctrl + shift while hitting enter, then the formula will return an array of Pass;Fail depending on the relationship of B4 to each of the values in A1:A10. (or A6:A10). The visible value in a single cell will be the result of the first comparison.
So you will need to properly evaluate the array, in order to return whatever it is you want from the formula.
I'm not sure of exactly what you want.
If you want to determine if B4 is greater than any of the numbers in A1:A10, then try (entered normally):
If(B4 > MAX(A1:A10),"Pass","Fail")
If you want something else as a result, you will need to be more specific.

Using INDEX(MATCH()) to return the n'th value of a cell

I have a simple worksheet with 2 columns
I want to get all the results(on column "H") (I can get only the first occurrence,I want to know if I can get the others) that contains the value from cell G1, is that possible without a macro ? any way of doing it would be appreciated...Any ideas?
You can use ROW() and SMALL() to get those instead of MATCH() since this always gets the first match.
=IFERROR(INDEX($C$4:$C$7,SMALL(IF($D$4:$D$7=$G$1,ROW($D$4:$D$7)-(ROW()-1)),ROWS($D$4:D4))),"Null")
So, if the array $D$4:$D$7=$G$1 returns true (i.e. the value equals that in G1), you will get the row numbers of these values, in this case, you will get 4 and 6. All the other will return False.
After some processing with -(ROW()-1), the 4 and 6 become 1 and 3. those two values are what will be fed to the INDEX.
SMALL() then picks the smallest, starting with the 1st (you get 1 from ROWS($D$4:D4)) and when you drag the formula down, the ROWS become ROWS($D$4:D5) which gives 2, and SMALL ends up taking the 2nd smallest value, which is 3.
EDIT: Forgot to mention. You have to array enter the above formula. To do this, type the combination keys of Ctrl+Shift+Enter after typing the formula (edit the formula again if necessary) instead of Enter alone.

Resources