How to find max value with INDEX-MATCH and MAX - excel

I am newbie to excel. I have this following data in excel sheet.
I want to have the value of G11 returned 27, as the value of its corresponding cell in column D, which is D11 matches to E8, and E9, which then correspond to the values of 14 and 27 in column J. Could someone tell me how to return a match value when I have two match situations and I need the maximum one. I am using the following formula.
=IF(D11=1,0,INDEX($H$4:$H$13,MATCH(D11,$E$4:$E$13,0),1))
I know if I use
=IF(D11=1,0,INDEX($H$4:$H$13,MATCH(D11,$E$4:$E$13,0)+1,1))
it returns me with the value 27 or greater of the both values but it will not work if I have three values and I want the maximum.

This will do it confirmed with ctrl+shift+enter:
{=MAX(IF(E4:E13=D11,J4:J13))}
Curly braces should not be entered manually, be sure to confirm with ctrl+shift+enter
This says:
Look for rows where the value in E matches D11
If you find one, return the value in column J
Give the maximum value of the result you find
This will cause errors if there are no matches. You also probably are designing your sheet in a not ideal way.

You can use the formula:
=IF(D11=1,0,MAX(IF(D11=$E$4:$E$13,$H$4:$H$13,0)))
entered as an array formula (i.e. you press Ctrl+Shift+Enter instead of Enter alone) for G11.
MATCH returns the first match, so that's why I'm using another IF instead.

Related

How to make VLOOKUP() match with the next greater value?

Please consider the table below:
If we write VLOOKUP(12,A1:B6,2,TRUE) we will get 2 as a result since the lookup value will match with 10 in the first column of the array and return the corresponding value in the second column.
What I am looking for is somewhat different. I want to find the value corresponding to the next greater value. For example, when my lookup value of 12, it will now match with 15
in the first column and then return 3 from the second column.
How do I do this preferably with VLOOKUP function?
If you have Excel O365 with the FILTER function, and if your lookup value is in D1, you can use:
=MIN(FILTER(B1:B5,A1:A5>=D1))
If the entries in column B are text, and not sorted numeric, you can use:
=INDEX(FILTER(B1:B5,A1:A5>=D1),1)
If you do not have O365 available then you can use the INDEX/MATCH functions entered as array formula:
=INDEX($B$1:$B$6,MATCH(TRUE,$A$1:$A$6>12,0))
Array formula after editing is confirmed by pressing ctrl + shift + enter

Vlookup equation

If you have a date in col1, a name in col2 of sheet1, can you use a vlookup to search for the name in sheet2 and return the only first date greater if that name in sheet2 has multiple dates?
You can nest your vlookup inside an if(countif to make it lookup only if your condition is met. Whatever you are looking up needs to be in a column to the left of the date. You will also need to have your list of dates sorted descending.
Here is the formula in the example below: IF(COUNTIF($A$2:$A$6,D2)>1,VLOOKUP(D2,$A$2:$B$6,2,0),"")
Formula explanation:
Count the number of time the value of D2 appears in A2:A6. If it appears more than 1 time, return the value of adjacent cell to the right, otherwise, return blank.
REVISION
A basic vlookup with an approximate match will work for this. When the match type is set to approximate, it returns the next higher result.
=VLOOKUP(A1,E1:F6,2,1)
REVISION #2
In order to omit values that are equal to or lesser than the value in column B, you'll need to use an index/match array. Enter the formula below and then while your cursor is in the cell, press control+shift+enter. Excel will then add curly braces around the formula and calculate it as an array and return the answer.
=IFNA(INDEX($F$1:$F$6,MATCH(1, ($E$1:$E$6=A1)*($F$1:$F$6>B1),0)),"")
Without the IFNA wrapped around the Index/Match formula, if there are no matches with a greater date, Excel will return a #N/A error. However, it will also return a #N/A error if you don't get the control+shift+enter done correctly.
I recommend you first enter the formula below, which doesn't include the IFNA wrapper, and confirm that some matches are returned. If your item doesn't have any matches, then #N/A will be returned, but you'll know the formula is working properly. Then change the formula to the one above that does include the IFNA wrapper, so that blanks are returned in place of #N/A.
=INDEX($F$1:$F$6,MATCH(1, ($E$1:$E$6=A5)*($F$1:$F$6>B5),0))
After the first is done, you can double-click the lower-right corner of the cell to populate the rest of the column.

Check if text exist in rest of Column, and if so, use info from that row it is in

I'm after a way to a cell to check another cell that I'm inputting text into, and for it to see if that text value is the same anywhere else in the column, and if so, it grabs the number value which is in the same column as itself but in the row of the text that checked for.
So if you use picture, you can see I've currently got E7 selected. I'm wanting it to check the "GOLF COURSE" column for any other row that contains the same text it has in it's own row. For this it's "Course1". I'd like it to check through the rest of column B if there are any matches for "Course1" which there is in B3. If it matches I'm wanting it to then use the value that's in same column as it (E) but the same row as the matched text in column B. In this case I would want it to copy the value that is in E3.
If there wasn't a match (as it's a new course lets say) then I need to be able to just click on the cell and input the numbers needed, which I would be able to do anyway but just throwing it in for sake of info.
I've tried all sorts of googling and thinking how I could possibly do it but it's too difficult for my amateur knowledge of Excel.
I believe you are looking for INDEX/MATCH:
=IF(COUNTIF($B:$B,$B7)>1,INDEX(E:E,MATCH($B7,$B:$B,0)),"New")
I added a COUNTIF check to ensure that the same course exists more than once in column B, without it, you would be getting a circular reference formula (which would also happen with the above formula if the same course appears more than once, but you use this formula on the first occurrence of that course, so make sure do not use it the first time you fill out the PAR scores for a particular course).
Merged Cells Messing With INDEX/MATCH
The Formula
Copy the following formula into cell E7:
=IF(AND($B7<>"",$D7="Par"),IF(ISERROR(MATCH($B7,$B$3:$B5,0)),"Par",INDEX(E$3:E5,MATCH($B7,$B$3:$B5,0))),IF(AND($B6<>"",$D7="Strokes"),IF(ISERROR(MATCH($B6,$B$3:$B4,0)),"Strokes",INDEX(E$3:E4,MATCH($B6,$B$3:$B4,0)+1)),""))
Image
How
We are creating the formula in cell E7.
MATCH
Syntax: MATCH(lookup_value,lookup_array,match_type)
We will use MATCH to find the position of COURSE1 in the above
cells of column B.
The 1st argument is lookup_value which will be the cell in the same
row of our row (7) in column B: B7 where we will lock only the
column (we will not be searching in other columns): $B7.
The 2nd argument lookup_array will be a range. The first cell will be
cell B3 whose row and column we'll lock, because we will always
start the search from this cell in every other cell to the left or
below: $B$3. The last cell will be B5 where we will lock only the
column: $B5.
And finally we will use 0 as the parameter of the 3rd argument
match_type to find an exact match.
Now were ready to write our MATCH formula:
=MATCH($B7,$B$3:$B5,0)
Which will return 1 i.e. an exact (0) match of B7 was found
in the 1st cell of range B3:B5.
We don't want 1 (E3), but the value in the cell (5).
INDEX
The INDEX function has 2 syntaxes where we will use the 2nd:
Syntax 2: INDEX(reference,row_num,column_num,area_num)
Since were using a one-column range we can safely omit the arguments row_num and column_num, which leaves us with:
Modified Syntax: INDEX(reference,area_num)
The INDEX function used this way will return the area_num-th value
of reference i.e. in our case if area_num is 1 it will return the
1st value in our column range, if it is 2, then the 2nd, etc.
The 1st argument reference will be the same sized range of our
MATCH range in column E: $E$3:$E5 where we will remove the
column locks because we also want to return results for other
columns: E$3:E5.
The 2nd argument area_num will be our MATCH formula.
Our current formula looks like this:
=INDEX(E$3:E5,MATCH($B7,$B$3:$B5,0))
which will return the value of cell E3: 5.
Final Adjustments: IF, AND and ISERROR
That would have been almost (Error Checking) all if the cells in column B weren't merged. Therefore we have to use IF to determine if the row in which we're writing the formula contains either Par or Strokes and adjust our so far created formula for both conditions:
=IF($D7="Par",INDEX(E$3:E5,MATCH($B7,$B$3:$B5,0))
=IF($D7="Strokes",INDEX(E$3:E4,MATCH($B6,$B$3:$B4,0)+1)
=IF($D7="Par",INDEX(E$3:E5,MATCH($B7,$B$3:$B5,0)),$D7="Strokes",INDEX(E$3:E4,MATCH($B6,$B$3:$B4,0)+1))
and (3rd condition) check in column B if there is a value in the row where we're creating the formula for a row containing Par, or the row above for a row containing Strokes, using AND:
=IF(AND($B7<>"",$D7="Par"),INDEX(E$3:E5,MATCH($B7,$B$3:$B5,0)),IF(AND($B6<>"",$D7="Strokes"),INDEX(E$3:E4,MATCH($B6,$B$3:$B4,0)+1),""))
Finally we have to add some error checking, because if the match was not found the formula will produce and #N/A error:
=IF(AND($B7<>"",$D7="Par"),IF(ISERROR(MATCH($B7,$B$3:$B5,0)),"Par",INDEX(E$3:E5,MATCH($B7,$B$3:$B5,0))),IF(AND($B6<>"",$D7="Strokes"),IF(ISERROR(MATCH($B6,$B$3:$B4,0)),"Strokes",INDEX(E$3:E4,MATCH($B6,$B$3:$B4,0)+1)),""))
Now we are ready to copy the formula to the right and below as far as we need.

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

In Excel, how do I get the header corresponding to the max value from a subset of a range?

I'm pushing beyond my Excel knowledge here. I'm trying to do a poll like thing in Excel. My problem lies on showing the selected result. Here's what I have so far:
I need to select the header corresponding to the cell with the highest value in the range B2:G2 (type 1). However, if there's a tie, I need to select the header corresponding to the highest value in the range B3:G3 amongst the cells with highest values in the range B2:G2.
In my sample, column "bb" and "cc" both share highest value on type 1 (5). So, in order to determine the winner, I need to compare the highest value for type 2 between them. Since "bb" is 0 and "cc" is 1, I expect "cc" as final result.
Components for formula are below:
J2: Displays the count of cells on line 2 with the highest value in the range. So, 2. I did that with COUNTIF comparing with MAX.
K2: Displays the first header it finds with the highest value on line 2. I managed with the following formula:
=INDEX($B$1:$G$1;0;MATCH(MAX($B$2:$G$2);$B$2:$G$2;0))
To be honest, I don't fully understand that formula. Did it with help of tutorials from the internet.
I2: Displays "TIE" when there's a tie on range B2:G2. Otherwise display the winning header (K2).
J3: Displays the number of cells with the maximum value on range B3:G3 but only considering winning cells from line 2. I did that with COUNTIFS.
=COUNTIFS(B3:G3;LARGE(B3:G3;1);B2:G2;MAX(B2:G2))
Edit: Just found out by entering number "4" on B3 that this formula above is also not working...
I3: Should follow the same pattern as the cell above. Displays "TIE" when there's still a TIE. Otherwise would display winning header (to be presented on K3).
K3: I don't know what to put here. Probably because I don't quite understand that formula with INDEX, MATCH and so on, I can't figure out a way to check the highest value between the two "winning" columns from the line above and get the header.
Could somebody help me with this?
First, let's establish if there is a tie. As you have discovered, you can do this by counting how many times the highest number appears in the range.
=COUNTIF($B2:$G2;MAX($B2:$G2))
If that count is more than 1, then there is a tie.
=IF(COUNTIF($B2:$G2;MAX($B2:$G2))>1;"TIE";"no tie")
In case of a tie you want to involve the values in row 3 as a tie breaker. We could add them to the values in row 2 using this array formula. You must confirm the array formula with Ctrl+Shift+Enter, not just Enter, otherwise it won't work.
=INDEX($B$1:$G$1,MATCH(MAX(((IF(B2:G2=MAX(B2:G2),MAX(B2:G2),0))+B3:G3)),INDEX((B2:G2+B3:G3),0)))
You only want to factor in row 3 if there is a tie, though, so you can re-use the IF statement from above and replace the "tie" in the formula above with the array formula and remember to press Ctrl+Shift+Enter!!
=IF(COUNTIF($B$2:$G$2,MAX($B$2:$G$2))>1,INDEX($B$1:$G$1,MATCH(MAX(((IF(B2:G2=MAX(B2:G2),MAX(B2:G2),0))+B3:G3)),INDEX((B2:G2+B3:G3),0))),"no tie")
You already have the formula to look up the value if there is no tie.
My system uses the comma as the list separator. I have manually replaced these with semicolons in the formulas I posted, but please bear with me if I may have missed one.
Now you can copy these formulas down to row 3. If there is a tie in the data in row 3, you will need data in row 4 to break the tie.
To understand the Index/Match combo, start with your first formula and read it from the inside out. The Max() finds the largest number. The Match() returns the position, i.e. column number, of the largest number in the range B2 to G2, i.e. 2 (the second column in the range). Index looks at B1 to G1 and returns the column value from the position that the Match returned, i.e. the 2nd column, which is the text bb.
Using row 3 as the tie breaker, the formula works pretty much the same, only that rows 2 and 3 are added together when the value in row 2 is the Max value and then that number is used to find the Max and the Match.
Here is an approach with sumproducts. I dont really inderstand what results you want in I3, J3, and K3. will try to workout.
I2:
=IF(SUMPRODUCT(--(B2:G2=MAX(B2:G2)))>1,"TIE","")
J2:
=SUMPRODUCT(--(B2:G2=MAX(B2:G2)))
K2:
=IF(B7>1,OFFSET(B1,0,SUMPRODUCT(--(B3:G3=MAX(B3:G3))*--(B2:G2=MAX(B2:G2))*{0,1,2,3,4,5})),OFFSET(B1,0,SUMPRODUCT(--(B2:G2=MAX(B2:G2))*{0,1,2,3,4,5})))
the {0,1,2,3,4,5} refers to the number of headers, if there are more, this array needs to changed

Resources