Excel - Find the biggest gap in a set of numbers? - excel

I have a series of numbers
0,1,99,5,5,98,9
They are unsorted and will remain that way.
I cannot use macros.
I want the answer 89 from a formula or an array formula.
89 is the biggest gap (between 9 and 98) in this series when sorted.
I want a formula, no vba, and no sorting my column or row.
I need a formula that sorts the list and subtracts one cell relative to the sorted list and gives the largest difference of the list of differences it creates.
so the list becomes 0,1,5,5,9,98,99
subtracts the current from the previous (na,1,4,0,4,89,1)
and gives me the max 89.
My list is a column of 7 rows.

This formula must be array-entered. In the formula RNG refers to the range where you have entered your numbers, e.g. A1:A7
=MAX(LARGE(RNG,ROW(INDIRECT("1:"&-1+COUNT(RNG))))-
LARGE(RNG,ROW(INDIRECT("2:"&COUNT(RNG)))))
To array-enter a formula, after entering
the formula into the cell or formula bar, hold down
ctrl-shift while hitting enter. If you did this
correctly, Excel will place braces {...} around the formula.
You can see how the formula works by using the Evaluate Formula option on the Formula Auditing tab of the Formulas ribbon.
In brief, the formula works by creating two arrays, sorted in order of size. The "K" value of the LARGE function is an array created by the ROW(INDIRECT sequence. The first returns
{1;2;3;4;5;6}
and the second returns
{2;3;4;5;6;7}
The two arrays of values returned would then be:
{99;98;9;5;5;1}
{98;9;5;5;1;0}
Subtracting one from the other results an array of the differences, and we find the MAX.

MAX(A:A) - LARGE(A:A,2) gives the difference between the largest and second-largest value if your numbers are in column A. Don't put this formula in column A.

Place the values in A1 thru A7 in any order!
In B1 enter:
=RANK(A1,$A$1:$A$7,0)+COUNTIF($A$1:$A1,A1)-1
and copy down thru B7
In C1 enter:
=INDEX($A$1:$A$7,MATCH(ROW(),B$1:B$7,0))
and copy down thru C7
In D2 enter:
=C1-C2
and copy down thru C7
Finally in E1 enter:
=MAX(C:C)
Column B represents the order of the values in column A if they were sorted. Column C contains the values of column A in sorted order. Column D are the differences and E1 gives the desired answer. Here is an example:

Related

How to simplify the formula for sums with one variable in Excel

I need to sum the result of a formula, repeated several times, which has a single value that changes from 1 to 180.The final formula must be contained in a single cell.
Column A contains values ​​from 1 to 180 (to simplify in the example I have only put 13).
Cell B1 contains a value expressed as a percentage.
Column C (C1 to C13) contains the following formula (cell C1 in the example):
=+((1/(1+(A1*1/12*$B$1)))).
Cell C14 contains the sum of all results.
By defining X the variable value of column A, the formula is in practice the following:
Column A will be not present in my sheet, so therefore I cannot refer my formula to the contents of any cell.
My need is to have only two cells in my sheet: B1, with the rate value; and C1, with the sum of the products with X from 1 to 180. That is what is contained in cell C14.
Thanks for your help and for your patience with my bad English.
This can be done using SUMPRODUCT:
=SUMPRODUCT(1/(1+(A1:A13*1/12*$B$1)))
You can use the SEQUENCE() function for your problem to replace the column A in your example.
More specifically, you can write the formula:
=1/(1+SEQUENCE(180)/12*B1)
which will automatically create an array with a length of 180 cells returning your desired series of values.
If you don't want to 'print' out that series at all but only show the sum right away, simply enclose the formula with the SUM() function:
=SUM(1/(1+SEQUENCE(180)/12*B1))

Excel: How do I efficiently condition a cell so that I can identify a cell with observation counts less than a certain number?

Basically I wanted to only extract the top 2 rows for every Entry #, but if the Entry # does not have 2 rows it is not used.
I tried IF statements and eliminating blanks and whatnot, but if this is a large data I would not be able to handle it.
If you don't mind adding a few columns, here's one method. Arrange how you wish.
Column's B and F are your original data.
Column C, in cell C2, input your first value manually (1 in this case).
Then here's the formula for Cell C3:
=IF(ISBLANK(B3),C2,B3)
Cell D2 formula:
=IF(C2=C1,D1+1,1)
Cell E2 formula:
=IF(COUNTIF(C:C,C2)>1,"Yes","No")
Drag down your formulas.
Then you can filter on column E for YES, and filter on column D for 1 & 2.

Excel: Apply formula to each cell in column within another formula

I have a column of numbers. I want to ROUND the numbers in each cell of the column and then take the SUM of the cells.
Of course this can be done by creating another column with the rounded values and then taking its sum.
Say that I have 10 cells in column A. I round them to the nearest 0 using ROUND(A#,0) for A1 through A10. These new values go in B#. Then in B11 I have SUM(B1:B10).
Is there a way to apply the ROUND forumla to each cell in column A within the SUM function so that I do not need to create another column of values?
SUMPRODUCT 'Deals' in Arrays 2
=SUMPRODUCT(ROUND(A1:A10,0))
Just use an array formula - enter with Ctrl + Shift + Enter.
=SUM(ROUND(A1:A10,0))
For example (also shows the rounded numbers in Column B to show it works, or you can see the magic with Formulas > Evaluate Formula.)

Excel Formula to find the first higher value

In column D (Result), I would like to have the following formula.
For each Cell in column C, find in column B the first value higher than the value of the cell of column C (starting from the same row) and gives as output the difference between the values found in column A (Count).
Example:
the value in C2 is 40. the first cell of B that has a value higher than 40 is B6. So D2 takes A6.value - A2.value = 5 - 1 = 4.
Can it be done without the use of VBA?
It can easily be accomplished with an array formula (so you have to enter the formula with Ctrl+Shift+Enter ) :
{=MATCH(TRUE;IF(B2:$B$7>C2;TRUE;FALSE);0)-1}
Put this formula in cell D2, and just drag down. You only have to change the end of your data set (change $B$7 into the real last cell of the column with data)
The formula works as follows :
The IF statement results in an array with TRUE/FALSE values that meet your criteria : {FALSE;FALSE;FALSE;FALSE;TRUE;FALSE}
The MATCH (with the 0 switch) searches the array for the index of the first match, which is 5 in our case
And you have to subtract 1 to get the offset to the cell where the function is placed, so this gives you 4
So although you have to enter it as an array formula (you will get an N/A error without the ctrl+shift+enter), the result is just a single number.
Also, depending on your data set, you might want to add some ERROR handling in case no match has been found, e.g. just using the example data set in your question, the result in cell D5 will be N/A so you have to decide what value you want the result to be in such case.
And finally, I did not use the values in column A, as I assumed this is just a sequential ascending counter. If this is not the case, and you specifically want to find the difference between the corresponding values in that column, you can use the variant mentioned by Foxfire... in one of the other answers: =MIN(IF(B2:$B$6>C2;A2:$A$6))-A2
A slightly adjusted and shortened answer on Peter K.'s suggestion:
In D2:
=MATCH(TRUE,$B3:B$7>C2,0)
enter the formula with ctrl+shift+enter
Something like this should work for you. First transform your range to a table.
=IFERROR(AGGREGATE(15,6,--([#Second]<[First])*(ROW([#Second])<=ROW([First]))/--([#Second]<[First]*(ROW([#Second])<=ROW([First])))*[Count],1) - [#Count],"")
In this formula the comparison between [second] and [First] starts at the same row. That means that the value in D5 is 0 and not 1. (Like #Foxfire And Burns And Burnslike stated in the comments).
Ok, I did not post the answer waiting until OP answered why D5 is 1 instead of 0, but my formula is also an array formula. It would be:
=MIN(IF(B2:$B$6>C2;A2:$A$6))-A2
To type this formula in array mode, you need to type it as usual, but instead of pressing ENTER, you need to press CTRL+SHIFT+ENTER

How to use excel formula to highlight 3 closest values from range

A
1) 0.218967921
2) 0.02111355
3) 0.145493415
4) 0.151092791
5) 0.15407891
6) 0.178046392
7) 0.11408411
I need to Highlight number 0.145493415 ,0.151092791,0.15407891 (column 3,4,5) which is closest in the list.
Assuming you have unsorted data in A1:A7 without duplicates, and the "3 closest values" are those where the difference between smallest and largest (of the three) is the smallest difference of any three values in that range, then you can use this formula in conditional formatting
=ABS(RANK(A1,A$1:A$7)-MATCH(MIN(LARGE(A$1:A$7,ROW(INDIRECT("1:"&COUNT(A$1:A$7)-2)))-LARGE(A$1:A$7,ROW(INDIRECT("1:"&COUNT(A$1:A$7)-2))+2)),LARGE(A$1:A$7,ROW(INDIRECT("1:"&COUNT(A$1:A$7)-2)))-LARGE(A$1:A$7,ROW(INDIRECT("1:"&COUNT(A$1:A$7)-2))+2),0)-1)<=1
It's rather complex but it can equally be applied to any sized list of values, even containing blanks or text values in the range
If there's more than one set of 3 with the same "spreadover" then the formula only highlights the largest set of values.
See this example
Here for completeness is my answer to the much simpler problem of highlighting the nearest three adjacent numbers in the original data but it's only valid if data are all in ascending or descending order so needs further work:-
=OR(
AND(ROW()<=5,ABS(A3-A1)=MIN(ABS(A$3:A$7-A$1:A$5))),
AND(ROW()<=6,IFERROR(ABS(A2-OFFSET(A1,-1,0))=MIN(ABS(A$3:A$7-A$1:A$5)),FALSE)),
AND(ROW()<=7,IFERROR(ABS(A1-OFFSET(A1,-2,0))=MIN(ABS(A$3:A$7-A$1:A$5)),FALSE))
)
Limitations: Data must be in ascending or descending order. All cells in range A1:A7 have to contain numbers. Treats empty cells as zero.
The basic formula to highlight the first cell in a group of three nearest cells without assuming that the data are in order would be:-
=ABS(A2-A1)+ABS(A3-A2)=MIN(ABS(A$2:A$6-A$1:A$5)+ABS(A$3:A$7-A$2:A$6))
The formula to highlight three cells would then be:-
=OR(
AND(ROW()<=5,ABS(A2-A1)+ABS(A3-A2)=MIN(ABS(A$2:A$6-A$1:A$5)+ABS(A$3:A$7-A$2:A$6))),
AND(ROW()<=6,IFERROR(ABS(A1-OFFSET(A1,-1,0))+ABS(A2-A1)=MIN(ABS(A$2:A$6-A$1:A$5)+ABS(A$3:A$7-A$2:A$6)),FALSE)),
AND(ROW()<=7,IFERROR(ABS(OFFSET(A2,-2,0)-OFFSET(A1,-2,0))+ABS(A1-OFFSET(A2,-2,0))=MIN(ABS(A$2:A$6-A$1:A$5)+ABS(A$3:A$7-A$2:A$6)),FALSE))
)
Limitations: All cells in range A1:A7 have to contain numbers. Treats empty cells as zero.
In response to the question of whether it only works for exactly seven cells, here is a version of the formula that works for any block of numbers starting at A1 and followed by a block of empty cells within the range A1:A1000:-
=OR(
AND(ROW()<=COUNT(A$1:A$1000)-2,ABS(A2-A1)+ABS(A3-A2)=MIN(IF(ROW(A$3:A$1000)<=COUNT(A$1:A$1000),ABS(A$2:A$999-A$1:A$998)+ABS(A$3:A$1000-A$2:A$999)))),
AND(ROW()<=COUNT(A$1:A$1000)-1,IFERROR(ABS(A1-OFFSET(A1,-1,0))+ABS(A2-A1)=MIN(IF(ROW(A$3:A$1000)<=COUNT(A$1:A$1000),ABS(A$2:A$999-A$1:A$998)+ABS(A$3:A$1000-A$2:A$999))),FALSE)),
AND(ROW()<=COUNT(A$1:A$1000),IFERROR(ABS(OFFSET(A2,-2,0)-OFFSET(A1,-2,0))+ABS(A1-OFFSET(A2,-2,0))=MIN(IF(ROW(A$3:A$1000)<=COUNT(A$1:A$1000),ABS(A$2:A$999-A$1:A$998)+ABS(A$3:A$1000-A$2:A$999))),FALSE))
)
Limitations: The block of numbers must not contain any empty cells or text.
If there are two groups with the same least spread, both will be highlighted.
These formulae can be used in conditional formatting or as stand-alone array formulae.
You changed the list! First, you have to sort the list from smallest to largest. Then:
In cell B2 put =A2-A1
In cell B3 put =A3-A2
In cell B4 put =A4-A3
In cell B5 put =A5-A4
etc.
In cell b10, calculate the first closest pair: =min(B1:B8)
In cell B11, calculate the row number of the first closest pair:=Match(B10,B1:B8,0)
In cell C2, put =if(row(C2)=b$11, 9999, B2)
copy down to cell C9
In cell C10, calculate the SECOND closest pair: =min(C1:C8)
In cell C11, calculate the row number of the SECOND closest pair:=Match(C10,C1:C8,0)
copy column c to column d and e
column e will have 9999 in the lowest three values
Highlight A1:A10, sELECT conditional formatting, enter formula "$e1=9999"

Resources