How do I use an array formula over a whole column or varying range? - excel-formula

I have a spreadsheet that I'm importing data into. I need to find the value within a column that is closest to zero. The column contains both positive and negative values, and the value closest to zero will be used in another formula. I've found an answer using an array formula, but it will only work for a fixed range (e.g. K2:K10), and the number of records imported into my sheet will vary each time I use it.
Here's what I have so far:
=INDEX(K:K,MATCH(MIN(ABS(K:K)),ABS(K:K),0))
Is there a way to apply an array formula over an entire column and just include non-zero cells other than the column title? Or possibly just cells with numerical values? Or is it possible to control the range that it applies to?

We can dynamically find the last cell in the range by using another INDEX/MATCH formula that is not an array:
=INDEX(K:K,MATCH(1E+99,K:K))
This will find the last cell that has a number in column K.
So we now use this as the last cell in the range:
=INDEX($K$2:INDEX(K:K,MATCH(1E+99,K:K)),MATCH(MIN(ABS($K$2:INDEX(K:K,MATCH(1E+99,K:K)))),ABS($K$2:INDEX(K:K,MATCH(1E+99,K:K))),0))
And now the formula is dynamic.
This formula is still an array formula and must be confirmed with Ctrl-Shift-Enter when exiting edit mode. If done correctly then Excel will put{} around the formula.
If as you pointed out there is a chance of deleting row 2 then all the K2 references will also be deleted.
In place of K2 we can use INDEX(K:K,2) It will now always look at the second row and will not error when row 2 is erased. So use this instead:
=INDEX(INDEX(K:K,2):INDEX(K:K,MATCH(1E+99,K:K)),MATCH(MIN(ABS(INDEX(K:K,2):INDE‌​X(K:K,MATCH(1E+99,K:K)))),ABS(INDEX(K:K,2):INDEX(K:K,MATCH(1E+99,K:K))),0))
There is nothing wrong with the Offset() function in small amounts, but it is a volatile function. Which means that it will calculate EVERY TIME excel calculate whether the data to which it is dependent has changed or not.

For the benefit of anyone reading this post, I ran into another issue and found a way around it. Scott Craner's answer above worked well until I ran a macro that I had for that sheet, which would delete certain rows. If row 2 got deleted, the formula would give a #REF error, because it was trying to call $K$2.
My solution was to replace $K$2 with
OFFSET(K1,1,0)
Therefore, the complete formula would be:
=INDEX(OFFSET(K1,1,0):INDEX(K:K,MATCH(1E+99,K:K)),MATCH(MIN(ABS(OFFSET(K1,1,0):INDEX(K:K,MATCH(1E+99,K:K)))),ABS(OFFSET(K1,1,0):INDEX(K:K,MATCH(1E+99,K:K))),0))
And as Scott mentioned, remember to hit Ctrl-Shift-Enter to execute the array formula.

Related

Excel - How to count the number of distinct texts of a specific date inside a table?

I'm trying to count the number of distinct text from a specific date in a data table.
Data Sample with expect result :
I was able to figure out how to count the distinct element from a range I specify, because I can determine the first and last row containing the date.
=SUMPRODUCT(1/COUNTIF(B2:B15,B2:B15))
I have tried to modify my formula so that it determines the cell range by itself but without success.
I searched for an answer, using a combination of CELL and MAXIFS, example below, but Excel does not accept the formula.
=CELL("row",MAXIFS(A2:A15,A2:a15,D2))
I've looked at the INDEX formula, but I can't figure out how to do what I want to do. 😑
Any idea what I'm doing wrong, or what I should be doing instead?
Thanks, I appreciate it!
If you have Office 365 and the new Dynamic Arrays, this sort of formula has become ridiculously easy.
This formula in cell E3:
=COUNTA(UNIQUE(FILTER($B$2:$B$15,$A$2:$A$15=D3)))
Copy down.
You can also generate the unique list of dates with this formula in D3, which spills down automatically and does not need to be copied.
=UNIQUE(A2:A15)
It wasn't easy, but by separating each step of the problem, I was able to solve it.
Note that my solution only works because my dates are sorted.
Here's the final formula in the cell "One formula to rule them all":
=SUMPRODUCT(1/COUNTIF(INDIRECT(CONCATENATE(ADDRESS((MATCH(D3,$A$2:$A$15,0)+1),2),":",ADDRESS(MAX(($A$2:$A$15=D3)*ROW($A$2:$A$15)),2))),INDIRECT(CONCATENATE(ADDRESS((MATCH(D3,$A$2:$A$15,0)+1),2),":",ADDRESS(MAX(($A$2:$A$15=D3)*ROW($A$2:$A$15)),2)))))
Here are my explanations of my process:
Formula if I select the range :
=SUMPRODUCT(1/COUNTIF(B2:B15,B2:B15))
Formula to get the first iteration
=ADDRESS((MATCH(D3,$A$2:$A$15,0)+1),2)
Formula to get the last iteration
{=ADDRESS(MAX(($A$2:$A$15=D3)*ROW($A$2:$A$15)),2)}
Create range from two addresses
=INDIRECT(CONCATENATE(F3,":",G3))
Formula giving me the expect result
=SUMPRODUCT(1/COUNTIF(INDIRECT(CONCATENATE(F3,":",G3)),INDIRECT(CONCATENATE(F3,":",G3))))

EXCEL count non-empty rows in non-contiguous range

I'm trying to count how many non-empty row I have in a range that is non-contiguous. I know it's easy to do if I create additional column that checks if a row is empty and then count that, but I'd like to do all that in a single cell.
Here's an example table:
So here my ranges would be "A1:C10" ; "E1:F10" ; "H1:H10" and I want to count the rows that are empty in these ranges. (So in this case the answer would be 2: rows "5" and "8") Note that there might be data in column "D" and "G" that I need to disregard in the evaluation. Also in the real data I have a lot of rows, but the number of ranges (columns)that is need to evaluate at a time are not the much so it is acceptable to enter the ranges in the formula by hand.
I've found this post and tried the formula in the last answer, but as far as I know I can't use non-contiguous range with COUNTBLANK, so I couldn't modify to work for me.
I tried this (entering as array formula) but it didn't work:
=SUM(IF(AND((COUNTA(OFFSET(E5,ROW(1:13)-1,0,1,4))=0),(COUNTA(OFFSET(I5,ROW(1:13)-1,0,1,1))=0), (COUNTA(OFFSET(C5,ROW(1:13)-1,0,1,))=0)),0,1))
Does anybody know a good formula or method for this?
Use MMULT as it is not volatile like OFFSET.
=SUM(--(MMULT((A2:H11<>"")*(COLUMN(A2:H11)<>4)*(COLUMN(A2:H11)<>7),TRANSPOSE(COLUMN(A2:H11)^0))=0))
Depending on one's version of Excel it may need to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.

How to define a range, using an Excel formula

In order to do some calculations on averages and differences of values in columns, I've defined a name, based on a range, but it seems to be completely going berserk:
I have a cell (D13), defined as Header_First _Answer, which contains the title of the column, and I have a value (currently being 69), which contains the number of entries, called Total_Count.
I've defined the entries of that column as another name: "All_First_Answered_Dates", defined as =OFFSET(Header_First_Answer;1;0):OFFSET(Header_First_Answer;Total_Count;0) (start by the first entry under Header_First_Answer, take up to 69 entries, and define a range out of this).
In cell G5, I'm using that name in order to do some calculations (calculating averages), but this seems not to work (there is a #Value error).
After second comment from Rory: G5 formula and first formula evaluation result:
Formula:
=AVERAGE(IF(ISBLANK(All_First_Answered_Dates);TODAY();All_First_Answered_Dates) - All_Start_Dates)
First evaluation result:
=AVERAGE(IF(ISBLANK(#Value!);TODAY();All_First_Answered_Dates) - All_Start_Dates)
Hence, my conclusion:
After some checking I've found out that this is due to the name "All_First_Answered_Dates", which seems to be interpreted one time too many (or how do I explain this):
In different cells, I've entered the formula =OFFSET(Header_First_Answer;1;0):OFFSET(Header_First_Answer;Total_Count;0) (which is exactly the meaning of "All_First_Answered_Dates"), and every time, using the Evaluate Formula feature, I see that the last but one result is correct: $D$14:$D$82. However, after that, another evaluation is done, turning this value into 43283 (in case the formula is entered in "J14"), 43300 (in case the formula is entered in "J15"), ..., and in case I enter this formula in a cell with row number lower than 14, I have the error value #Value (which explains the wrong result in cell G5).
If I simply put the formula =$D$14:$D$82 in any of the mentioned cells, then the content of some cells in column D are shown (which are dates, not values like 43283 or 43300).
It appears that declaring a range as =x:y, where x and y are formula results, is not working.
Does anybody know how I can define a range as a formula, which I can then use in order to define in a name?
I can imagine my explanation being quite complicated without an image, hence the attached screenshot. In there:
In cell J13, there is the formula =OFFSET(Header_First_Answer;1;0):OFFSET(Header_First_Answer;Total_Count;0).
In cell J14, there is the same formula.
In cell K14, there is the formula =$D$14:$D$82.
For completion purposes, hereby a screenshot of the name manager, containing both mentioned names (the ones, selected in the name manager):
Edit after first comment:
The idea behind the range is the following:
1. Take the first row under Header_First_Answer, do not take any other column : OFFSET(Header_First_Answer;1;0)
2. Take the Total_Count's row under Header_First_Answer, do not take any other column : OFFSET(Header_First_Answer;Total_Count;0)
3. Define a range, based on those two cells, by putting a semicolon between them.
I was not aware of the height and width features of the Offset() worksheet function. I've implemented them, which makes the formulas much easier.
Unfortunately the problem still persists.
Thanks in advance
Dominique
I've just found the answer of what was going wrong:
The formula was meant to be an array formula. Something went wrong and while trying to debug, I accidently re-formatted the formula into a normal formula (I must have pressed "ENTER" instead of "Ctrl" + "Shift" + "ENTER") at some point.
I have re-applied array formula (using "Ctrl" + "Shift" + "ENTER"), getting a formula like:
{=AVERAGE(IF(ISBLANK(All_First_Answered_Dates);TODAY();All_First_Answered_Dates) - All_Start_Dates)}
(mind the braces {, })
Now everything is working fine.

Lock sumif to specific cell range

I have a spreadsheet that contains data in columns A:D. Each block of data A1:D11, A12:D22, etc. represents a single days worth of data. It has configuration items that I do a sumif function on to pull for the grand total since the report began(=SUMIF(A:A,L2,B:B). I have now been asked to sum only the last weeks worth of data. My current formula is (=SUMIF(A1:A55,L13,B:B)-very simple. However, I need it to lock so that we only checks data between A1:A55. If I add in a new days block of data it changes the reference to (=SUMIF(A12:A66,L13,B:B). If I use the $ refernce it does the same thing...how do I lock the formula so that it will always look between A1:A55?
Regards
When you do some deletions or insertions, excel will shift the reference used in the formula automatically. e.g. If the original formula in B1 is =SUM(A1:A20), after inserting cell A5 with shifting cells down , the formula will be =SUM(A1:A21).
To avoid this situation, you can use the formula INDIRECT, it will return the reference decided by the argument with string type. Since it is a string, excel will not change its value, therefore the range will always be the same no matter you inserted or deleted something.
So give it a try =)
=SUMIF(INDIRECT("A1:A55"),L13,B:B)
As an alternative suggestion, I would create another column in your table that calculates the weeknumber from the date using weeknum() and then use sumif to lookup a specific week.
That way you don't need to worry about row inserts at all.
By using the Indirect() method (which is a good solution and may be be all you need to solve your problem), you still have the problem of data being inserted into the range that your sumif() covers that might not be part of the week that you're monitoring (for example, if a row containing data from a different week were to be entered into A55).

trying to expand vlookup to other cells without automatically changing values

I am setting up a vlookup to pull product prices from another sheet within the workbook. The code works for the cell but when i try to expand or copy and past the code into the next row it automatically changes the data table_array value.
=VLOOKUP(B5,Prices!1:65536,3)
Within the code i want the first value, B5 to scale with the row it is in, however the second value needs to remain the same. How do i go about doing this? Also is there a way that i can get the cell to remain blank instead of displaying N/A if there isnt a valid part number?
Thanks for your help!
=VLOOKUP(B5,Prices!$1:$65536,3)
The $ lock the range.
For example.
$A1 will lock the column to A when the formulas is copied other
locations.
A$1 will lock the row
$A$1 will lock both the column and the row.
I can't comment because I do not have enough rep but this will fix user3716271 's formula:
=IF(ISERROR(VLOOKUP(B5,Prices!$1:$65536,3)),"", VLOOKUP(B5,Prices!$1:$65536,3))
The following formula should solve both problems as well, a little more compact and would use one less VLOOKUP():
=IFERROR(VLOOKUP(B5,Prices!$1:$65536,3), "")
As guitarthrower had said, the $ before the number is used to lock the range.
For the second part, an IF formula will work fine:
=IF(ISERROR(VLOOKUP(B5,Prices!1:65536,3)),"",VLOOKUP(B5,Prices!1:65536,3)),"")
And if I understand correctly the first part have you tried set an absolute value? Something like:
=IF(ISERROR(VLOOKUP(B$5,Prices!1:65536,3)),"",VLOOKUP(B5,Prices!1:65536,3)),"")

Resources