How to define a range, using an Excel formula - excel

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.

Related

Excel365 'Sequence' combined with 'If'

I've created a sequence of dates for a dynamic calender.
This works fine.
But now I want to implement a condition, using 'IF' statement, where the step of the sequence is changed based on the value in the cells in column D (starting in cell D8).
If the cell in column D contains "Y" then the step in the sequence for that specific cell must be 8 in stead of 7.
The idea is that the sequence generates a list of all Mondays of a specific year (defined in B6), but when the Monday is a holiday, the return value must be a Thuesday.
The problem is that the reference for the column (D8) doesn't change and stays on the first cell reference. It should change to D9 for the next sequence value, D10 for the 3rd sequence value, etc.
Dutch formula -
=REEKS(54;1;DATUM($B$6;1;1)-WEEKDAG(DATUM($B$6;1;1);2)+1;ALS(D8="Y";7;8))
English version formula -
=SEQUENCE(54,1,Date($B$6,1,1)-WEEKDAY(DATE($B$6,1,1),2)+1,IF(D8="Y",7,8))
Edit 04/01/2023
This is the first cell in the sequence
This is currently the second cell in the sequence, where reference to D8 needs to be D9
Now I understand your question (in my first answer, I thought you were copying your formula on another place).
There seems to be a difference between earlier Excel versions, where a formula could only have one single cell as a result. Now there are formulae (like =SEQUENCE(), whose answer spreads over different cells. All those cells contain one element of the formula result, which means that the formula itself does not change over the multi-cell result.
I'm not sure if I understand what you mean: I have copied your formula (the English one) in cell "E2" and this is what I get:
=SEQUENCE(54,1,DATE($B$6,1,1)-WEEKDAY(DATE($B$6,1,1),2)+1,IF(D8="Y",7,8))
In another cell ("G3"), this turns into (select cell "E2", press Ctrl+C, press cell "G3", press Ctrl+V):
=SEQUENCE(54,1,DATE($B$6,1,1)-WEEKDAY(DATE($B$6,1,1),2)+1,IF(F9="Y",7,8))
So, when I apply a formula to another cell (two columns further, one row further), the reference to "D8" turns into "F9" (two columns further, one row further).
The other reference ("$B$6) does not change. Obviously, because the dollarsigns prevent that value to be changed (this is exactly what absolute and relative cell references are about, as described here).
Unfortunately, I don't know what you mean when you say that your cell references don't change: the ones, who should, do, and the ones, who shouldn't, don't, which is correct behaviour.
Oh: when you enter your formula in an external tool (like Notepad or so), you paste your formula in a cell and you paste it again in another cell, Excel won't realise that the cell references need to be update, is this the problem you're having?

Excel - Highlight cell which is closer and smaller to a specific value

I would like to highlight a cell which is the closest the a specific value but still lower (below) or equal according to a formula.
For instance, if I have 14 in B4, I would like the cell 13 (or 14) highlighted but not 15 in the range.
The value I need to look for would be in B4 and the range that I must apply the highlight formula to would be L1:L371.
So far, the best result I was able to achieve was with (from what I was able to find using Google or Stackoverflow already existing content):
=SMALL($L1:$L317,COUNTIF($L1:$L371,"<="&$B$4))
Although, not only it selects the value I am looking for, it also selects all the values below.
May someone provide me some help so I can achieve the expected result please?
Thank you for your time and help, it is greatly appreciated.
Based on your description, it seems like you need to find the maximum value of a range (L1:L137) that is less than or equal to an input variable (B4) and highlight that value in the search range. Depending on what version of Excel you have, here's what you need to do:
For Excel 2019 or Excel 365
You can use the MAXIFS function. You would actually put that function in a conditional formatting rule, but more on that in just a second. As a test, put the following code in cell B5:
=MAXIFS($L$1:$L$137,$L$1:$L$137,"<="&$B$4)
The first argument is your "MAX_RANGE", or the range that contains the values you want to find the max of. It is also our "CRITERIA_RANGE", the second argument. The third argument is the criteria itself, which is that the search range must be less than or equal to the value in cell B4. Essentially, we are going through the list and creating a subset of numbers that are less than or equal to our search value, and discarding the rest. I assume you are aware of relative vs. absolute referencing, since you used the "$" anchor in your referenced code. If not, here's a description of the difference.
Now, the result of that formula should be the highest number in the list less than or equal to the search value, but that doesn't highlight it for us in our list. To do this we need conditional formatting. To do this, highlight your data range (L1:L137) and go to the conditional formatting drop down on the home tab of the menu ribbon. Choose "New Rule". On the dialog that pops up, choose, "Use a formula to determine which cells to format". In the formula input box, enter:
=L1=MAXIFS($L$1:$L$137,$L$1:$L$137,"<="&$B$4)
This will compare whether the value in the cell being evaluated (L1, for instance) is equal to the result of that formula we talked about above. Since L1 is only relatively referenced, this formula will work for every cell in the data range.
Now, before you hit "OK" on the dialog, select the Format button. This will allow you to adjust your highlighting and formatting as you desire. Click "OK" on the formatting dialog, then "OK" on the Conditional Formatting Rule dialog. This should now highlight any data cell that is equal specifically to the result of our formula, and not everything that's less than our value.
For Earlier Versions of Excel
The concept is the same in earlier versions of Excel, but unfortunately, the MAXIFS function is not present in these versions. Instead, we must use an array formula. Array formulas are a whole other can of worms, but ExcelJet is an excellent resource. In fact, they talk about this very issue, here.
Unfortunately, we can't put the array formula in the conditional formatting formula like we did above, so we'll need to put this formula on a cell in the worksheet, then the conditional formula should reference that new cell. So in cell B5, if you put:
=MAX(IF(L1:L137=B4,L1:L137)
And then, instead of pressing Enter, you must press Ctl + Shift + Enter
This keyboard combination will tell Excel that you are trying to enter an array formula. If you don't press these keys, then the formula will error. Once you have entered the array formula, if you put your cursor in cell B5, you will see the formula bar at the top has added curly braces ({ , }) around the formula to look like
{=MAX(IF(L1:L137=B4,L1:L137)}
This leads to the same result as above, but is just achieved slightly differently. Now, following the same process described above for conditional formatting, you will simply set the formula to:
=L1=$B$5
And that should be it! Hope this helps!

excel SUMIF not giving right answer

I have a table with data and I am trying to do the following on another sheet. I am trying to sum the price of items in a column if the date is a particular date. I filtered all the dates using UNIQUE on the second sheet and doing a SUMIF based on those values. The problem is, the results are all slightly less than the actual value obtained by manual summation/ highlighting the relevant cells and seeing the sum on the bottom. Why is this happening? The discrepancy cannot be attributed to any single cell it is supposed to sum.
btw anyone knows why the similar questions box seems to be bugging out? The original text stays in place as I scroll, like some kind of ghost effect.
edit: here is a picture of the problem
Here is what I think is the explanation.
From the documentation: The sum_range argument does not have to be the same size and shape as the range argument. The actual cells that are added are determined by using the upper leftmost cell in the sum_range argument as the beginning cell, and then including cells that correspond in size and shape to the range argument
Your formula: =SUMIF(F2:F31,"="&H2,G:G)
thus evaluates to =SUMIF(F2:F31,"="&H2,G1:G30)
Because of the offset in the ranges, the last instance of range = H2 gets dropped. And 34.86-33.67 = 1.19 which is the last cell that should be added.
In other words, g1 is evaluated against f2, and g13 is evaluated against f14. Since f14 does not contain 3/2/2020, the 1.19 does not get added by SUMIF
Sometimes this is useful. But here, as you note, it has an unwanted result.
It works in your first case because both range and sum_range start in the same row.
All you need to do to get it working in your second case is to ensure both ranges start at the same row.
eg: =SUMIF(F2:F31,"="&H2,G2) would work

Data generation in Excel

Good morning, I am new at using excel and I'll be very thankful if someone can help (and I'm sure that the answer is easy from your point of view).
https://img15.hostingpics.net/pics/785039data.png
The screen capture reprents a simplified way of explaining what I am looking for:
For each number in the first column (which can appear more than once), I want to generate a corresponding Id (See column Id to affect which is the type of data).
We are only looking at the beginning of "Number", which can be up to 8 number is size.
Sometimes there can be tricky case, as te example starting with 40 vs 402 which are not affected to the same Id.
As I am new on excel maybe this answer or question exist but I don't know how to search/name it.
Thank you for your help, have a great day.
Tricky formula. Not exactly the prettiest thing ever but this formula works: (Line breaks added for readability)
= IFERROR(INDEX(D$2:D$7,MATCH(MAX((((A2-(C$2:C$7*10^(CEILING(LOG10(A2+1),1)
-CEILING(LOG10(C$2:C$7+1),1))))=MOD(A2,10^(CEILING(LOG10(A2+1),1)
-CEILING(LOG10(C$2:C$7+1),1))))+0)*C$2:C$7),C$2:C$7,0)),"(no match)")
A few things to note:
You didn't include cell ranges so you will have to manually change the cell ranges in the formula above to whatever your particular cell ranges are.
This is an array formula, meaning after you enter this formula into a cell, you must press on the keyboard Ctrl+Shift+Enter rather than just Enter.
The formula looks for the "strongest" match. For example, if a number is 40200000, the formula would return "Grapefruit" because it is a stronger match than "Grape".
If no match is found, I just have the formula return "(no match)" but you can obviously change that to whatever you want.
I assume this won't be an issue, but the formula will not work if any negative numbers are used.
See below, screenshot that shows formula works for your data.
You can try this array formula (click Ctrl + Shift + Enter together) from cell B2 and drag it down:
=IFERROR(INDEX(D$2:D$7,MATCH(MAX((VALUE(LEFT(A2,LEN($C$2:$C$7)))=$C$2:$C$7)*C$2:C$7),C$2:C$7,0)),"")
This will try to match the longest number (with MAX) and return the value (with INDEX/MATCH).

One to many relationship to fetch values in an alternate rows

I am trying to fetch dates from sheet1 with common ID (there are multiple occurrences in sheet1) using an array formula in a cell (then dragging it to the right side to have all the dates belong to specific ID):
=IF(COLUMNS($E2:E2)<=$D2,INDEX(Sheet1!$B$2:$B$13,SMALL(IF(Sheet1!$A$2:$A$13=Sheet2!$A2,
ROW(Sheet1!$A$2:$A$13)-ROW(Sheet1!$A$2)+1),COLUMNS($E2:E2))),"")
But, whenever I try to insert one column (for counting purposes) in b/w the columns, this formula doesn't work. I can't figure out the issue, would really appreciate the help?
Thank you.
Assuming that your first formula is in E2, and that the cells in row 2 of any newly-inserted columns will always be blank, replace the two instances of:
COLUMNS($E2:E2)
in your formula in E2 with:
COUNT(1/LEN($D2:D2))
As a way of explanation, take the formula in F2, for which this part will be:
COUNT(1/LEN($D2:E2))
(The only part having changed naturally being the end range reference.)
And let's assume that 3 new columns are inserted to the left of column E, which means that E2, F2 and G2 will now be blank, H2 will contain the entry which was previously in E2, and, for the formula in I2, the above part will now be:
COUNT(1/LEN($D2:H2))
Clearly we wish this part to continue to give 2, since this cell should still represent the second of our returns.
Using LEN is a rigorous way to determine whether a cell is empty or not. Other functions are also available for this purpose, though may give incorrect results depending upon whether the blanks in that range are "genuine" blanks or the null string "" as a result of formulas in those cells.
If a cell is empty, it has a length of 0. Hence, in this example, the above resolves to:
COUNT(1/{1,0,0,0,2})
(Where I've made some random assumptions about the lengths of the strings in cells D2 and H2.)
There are many ways to determine how many non-zeroes there are in an array. SUMPRODUCT would be one; I chose another, the logic being that the above becomes, after reciprocation with unity:
COUNT({1,#DIV/0!,#DIV/0!,#DIV/0!,0.5})
and, since COUNT ignores any errors in the range passed to it, the above resolves to 2, as desired.
Regards

Resources