How do I get the max N numbers in row in Excel? - excel

MAX will take a range and tell me the largest number. But what if I wanted to iterate over that range and find the largest two numbers in a row?
For example, if I have the range [0, 2, 5, 6, 9, 3, 8], MAX is 9, but MAX2 is 15 (6+9). MAX3 is 20 (5+6+9).
How would I write MAX2, MAX3, or MAXN in Excel?

Eg: sum 3 largest numbers in A1:A5
=SUM(LARGE(A1:A5,ROW(1:3)))
This is an array formula, so you need to use use Ctrl+Shift+Enter

=IF(AND(A4+A5>=A6+A5, A4+A5>=A3+A4),A4+A5,0)
you could use this formula to find the biggest two numbers in a row.
and this would equate to max2
place your example range in column a and place the formula in b4. drag this from b:1-B:N and the number in b will correspond to the sum of the two highest cells that are next to each other
so if you had n=n you could do
if(and(sum(a2:An)>=sum(a1:A(n-1), sum(a2:An)>=sum(An:A(n+n),sum(a2:an),0)

Thanks for this, the answer helped me solve an even more heinous problem:
=AVERAGE(LARGE(CHOOSE({1,2,3,4,5,6,7,8},(P3),(R3),(T3),(V3),(X3),(Z3),(AB3),(AD3)),ROW($1:$4)))
which let me average the largest of nonadjacent columns for a bunch of students.
you need to push ctrl shift enter to evaluate such an expression
(it's building a table on the fly) – that will wrap curly brackets
{} around it.
the ()s around a cell reference mean that if it's blank
(say a student didn't do a quiz) it gets translated as 0 not #fail
the dollar signs $ are necessary or you can't copy & paste this
into other rows and get the relative stuff to all work.
Not that you asked but I figured someone might...

Related

How can I redistribute a range of values without duplicates between 1 and 100?

I have a list of values between 1 and 100, essentially a sort of ranking that occassionally skips a few numbers (for example, the first ten values are 2, 6, 6, 10, 10, 10, 10, 11, 12, 13). They're ordered ascendingly, so every number will be either higher than or equal to the number above it. Now, I wish to remove all the duplicates from this list while remaining between 1 and 100. So, for example, for the values above, something like 2, 6, 7, 10, 11, 12, 13, 14, 15, 16 would work or 2, 6, 7, 8, 9, 10, 11, 12, 13, 14. However, the formulas I've tried so far will either go over 100, go under 1, or create circular references.
Given the nature of the list, there's very little chance of the amount of values exceeding 100, so if that possibility can be accounted for, it'd be a nice bonus, but it's not required.
Please create a named range for all your numbers and name it Source. As an alternative, replace the named range Source in my formulas below with the sheet address of the range where you have your numbers.
[C2] =INDEX(Source,MATCH(0,COUNTIF($C$1:$C1,Source),0))
This is an array formula and needs to be confirmed with Ctl + Shift + Enter. Observe that the COUNTIF range is defined one row above the row in which the formula resides. Its formulated with one absolute and one relative end. The end of the range will expand as you copy the formula down.
You can achieve a similar result using the LOOKUP() function. But since MATCH() finds the first instance, LOOKUP() returns the last. Therefore the formula below will return the same sequence in descending order. LOOKUP() is capable of looping on its own and the formula doesn't require array enabling. Just confirm it normally, with only Enter.
[E2] =LOOKUP(2,1/(COUNTIF($E$1:E1,Source)=0),Source)
Observe that the result range is defined with an absolute and relative end, starting above the row of the formula as explained above.
In B2, formula copied down :
=IF(A2="","",IF((COUNTIF(B$1:B1,A2)>0)+(A2=A1),B1+1,A2))
Edit #1
If data have repeated 100 as per OP's comment, then B2 formula become :
=IF((COUNTIF(B$1:B1,100)>0)+(A2=""),"",IF((COUNTIF(B$1:B1,A2)>0)+(A2=A1),B1+1,A2))
Thank you everyone for posting! Thanks to Bosco's original reply, I managed to fiddle around and find the answer myself. The biggest issue I was facing was getting a circular formula error, but by using Bosco's formula as a help column I was able to get it all worked out.
So, the solution holds 3 different columns: Original Data (column A), Help Column (column B) and Final Result (column C).
In the Help Column, I did as suggested, in cell B2 and copied down:
=IF(A2="","",IF((COUNTIF(B$1:B1,A2)>0)+(A2=A1),B1+1,A2))
This would occassionally create results over 100, so in the Final Column I placed the following array formula, which incidentally also accounts for lists longer than 100 entries, in C2 and copied down:
=IF(B2="","",IF(ROW(C2)>101,ROW(C2),IF(AND(B2>=100,B3=""),100,IF($B2:$B$1000>=C3,C3-1,B2))))
As an array formula, this needs to be entered using Ctrl+Shift+Enter.
Thank you very much to everyone who posted replies, I'm sorry for my lack of clarity!

Match Function Multiple Criteria with inequality (excel function)

I have two columns of data:
A has names in rows 1-5 (Amy, Greg, Matt, Sean, Greg).
B has numbers in rows 1-5 (90, 20, 30, 40, 50).
I want to write an excel function that will give me the number of the row with the name Greg in column A where the number in column B is greater than 25.
I have tried two functions that are not working:
=MATCH("Greg"&TRUE,A1:A6&B1:B6>25,0)
=Match("Greg"&>25,A1:A6&B1:B6,0)
I am pressing ctrl + shift + enter to do an array and it is still not working.
I am unable to sort the values (due to other constraints) so I cannot use the final argument in the match function to achieve the >25 argument.
Try a standard formula AGGREGATE instead of an array formula MATCH.
=aggregate(15, 7, row(a:a)/((a$1:a$6="greg")*(b$1:b$6>25)), 1)
To get possible second, third, etc. matching row numbers, change the 1 at the end to row(1:1) and fill down.
While I don't typically use this style of array formula, your original would have worked as a CSE formula if changed to this,
=MATCH("Greg"&TRUE, A1:A6&(B1:B6>25), 0)

SUM numbers, but when 0, add 8 instead

I'm trying to write a SUM(IF(zero, add 8), A1:G1) kind of excel function.
What it should do:
If the cell contains a number, add that number to the sum.
If the cell contains a number = 0, add 8 to the sum.
if the cell is empty, add nothing to the sum
I have tried using SUMIF, but it only "adds the numbers if they meet a criteria", and I rather need "add this when the criteria is met"
You could just make a longer formula:
=SUM(A1:G1)+COUNTIF(A1:G1, 0)*8
The first sum will only do your numbers ignore blanks and 0's, the second part counts the number of 0's and multiplies it by 8.
You can do this with the following formula:
=SUMIF(A1:G1,">0")+8*COUNTIFS(A1:G1,0,A1:G1,"<>")
should be self-explaining :)
Method 1
If you can spare another column, just use an IF statement to replace all 0's by 8's and blanks by 0's (not really necessary) in a new column
=IF(A1=0,8,A1) and take a SUM of this column.
Method 2
Just SUM over your original data, count the number of zeros in your data and multiply this number by 8.
=SUM(A1:A10)+8*COUNTIF(A1:A10,"=0")
Note: The data is in A1:A10 in this case.

Excel: look across array and return nth non-"n.a." value

in row 3, I have an array of data that contains numerical values and text in the form of "n.a.". I would be most appreciative if someone could help me construct a formula that looks across the array and returns the nth non-"n.a." value.
Row 3 beginning in column B: 5, 8, n.a., 7, 6 ,3, 9, 7, n.a., 12
Formula would return 7 if n was set to 3.
Formula would return 12 if n was set to 9.
Thanks in advance.
Treat the bold, italic 3 in the formula as your n value:
=INDEX(3:3,SMALL(INDEX(ISNUMBER(B3:K3)*COLUMN(B3:K3),),3+COUNTA(B3:K3)-COUNT(B3:K3)))
Note that given your example, setting n to 9 will result in a #NUM! error because there are only 8 numbers. Expand the B3:K3 to suit your range, but do not use a whole row reference (using whole row reference would greatly decrease performance time). If preferred, you could create a dynamic named range instead of manually setting the B3:K3 range.
Try
=INDEX(3:3,AGGREGATE(15,6,COLUMN(B3:K3)/ISNUMBER(B3:K3),3))
This formula comes almost directly from God, a.k.a. Scott Craner. The way it works is that any cells which don't contain a number will give a #DIV/0! error in the division. Any cells which do contain a number just give the column in B3:K3. Aggregate with 15,6 works like SMALL but ignores the error cells thus giving you the answer you want.

CountIF(s) for out of range criteria

I am trying to work out working formula for CountIF with criteria which is out of the range and I am not sure if "If and Count" would be any differen. Nonetheless, the combination I am trying brings "0" which is certainly not correct.
Can someone please take a look and help?
https://drive.google.com/file/d/0B0aOVjxZjuyrSjdXWG9acWlZMDA/view?usp=sharing
I am trying =COUNTIF(B12:B11511, $A$2=A12:A11511) and have no idea if this will work or not?
Countif(s) for B2 - B8
Thanks a lot.
As you are looking at two cirteria you want to use COUNTIFS:
=COUNTIFS($A$11:$A$11511,A2,$B$11:$B$11511,1)
Put in B2 and copy down. It will count any that match the country and are marked with 1 in column B.
To count the 0, just change the last criterion:
=COUNTIFS($A$11:$A$11511,A2,$B$11:$B$11511,0)
To count both together just sum the two. It can be done two ways:
Add them manually:
=COUNTIFS($A$11:$A$11511,A2,$B$11:$B$11511,1) + COUNTIFS($A$11:$A$11511,A2,$B$11:$B$11511,0)
Use SUM:
=SUM(COUNTIFS($A$11:$A$11511,A2,$B$11:$B$11511,{0,1})
But by your data, which only has 1s and 0s, this formula will return the same numbers.
=COUNTIF($A$11:$A$11511,A2)
Which counts the number of cells in A that match A2.
Use this in B2,
=sum(countifs($A$12:$A$11511, MID($A2, 8, 2), $B$12:$B$11511, {0, 1}))
That counts US with ones and zeroes. Fill down to row 8.

Resources