In an excel sheet, I have from A1 to A6:
1, 2, 4, 6, 8, 9
I would like, using MATCH function, to retrieve the smallest interval that contains 5. Here, 4 and 6.
I can easily use the MATCH and INDEX function to find 4, but I can't find a way to find the 6.
How can I reverse the order of the Array in the MATCH function?
You still can use the composition of INDEX and MATCH by using #ExcelHero add one trick but you need to make sure the matched offset doesn't overflow your index. In many use cases, you could also protect your match against an underflow. Of course, we wouldn't need all this if MATCH didn't request a reverse (descending) order for the -1 (Greater than) match type argument or if Excel provided a formula for reversing an array.
My suggestion is to use the following formula for the MATCH
part:
=IF(N19 < INDEX(lookup_range, 1), 1, MIN(ROWS(lookup_range), 1 + MATCH(N19, lookup_range, 1)))
N19 is the cell holding the value you look up, lookup_range is the name of your lookup range, the condition refers to the first cell in the named range.
So all in all you can just do (adapt the formulas if you don't like named ranges):
# For the lower limit
=INDEX(lookup_range, IF(N19 < INDEX(lookup_range, 1), 1, MATCH(N19, lookup_range, 1)))
# For the higher limit
=INDEX(lookup_range, IF(N19 < INDEX(lookup_range, 1), 1, MIN(ROWS(lookup_range), 1 + MATCH(N19, lookup_range, 1))))
NOTA: You can also change the first argument of INDEX in these two formulas if you're interested in any other output range.
You could also try these two formulas:
=LOOKUP(1,0/FREQUENCY(-B1,-A1:A6),+A1:A6)
=LOOKUP(1,0/FREQUENCY(B1,A1:A6),+A1:A6)
Notes:
the list A1:A6 does not need to be sorted
if B1 is equal to one of the values in A1:A6 then both formulas return B1.
if B1 lies outside the range of values in A1:A6then one of the formulas returns #N/A
Use XMATCH, as explained on this site:
https://exceljet.net/formula/xmatch-reverse-search
XMATCH allows you to set the search direction, as follows:
=XMATCH(B1,A1:A6,0,-1)
Where B1 is the cell to match, A1:A6 is the array you are searching through, 0 indicates "exact match", and -1 selects searching in the reverse direction (starting with cell A6 and ending with A1).
Related
I have a three-column table in Excel, called Table1, like this:
Given two values (one for each input variable), one which must be exactly equal to any of the numbers in the first column (2, 4, 6, or 8) and which must be typed in cell F2, and another one which can be any number between the least (1) and greatest (25) numbers in the second column and which must be typed in cell F3, I want to find the corresponding value in the third column. If the value typed for the second variable is not present in the second column of the table, then the output value of the next row is chosen.
For example, suppose the lookup values are 4 (for the first column) and 10 (for the second column), then the output should be E, since both 4 and 10 are present in the first and second columns, respectively, and the row with the output E corresponds to those values for the inputs.
Another example. Suppose the lookup values are 8 (for the first column) and 17 (for the second column), then the output should be K; it is not J because the latter corresponds to a value of 15 for the second column, which is strictly less than 17; so the output is K because it corresponds to the value that is immediately after (or greater than) 17, being 20.
My attempt
To limit the available values the user can choose, I could create data-validated cells. For choosing the values in the first column, the data validation would by of type list and be equal to 2, 4, 6, 8; such cell would be F2. Like this:
For choosing the values in the second column, the data validation would be of the type whole number, with minimum value of 1 and maximum value of 25. Like this:
Now the formulas for the lookup. After googling, I found out that performing a look-up task with two input criteria is known as a two-way lookup. Using the INDEX and MATCH functions, I managed to perform the two-way lookup, unfortunately the formula only allows exact matches, so it works fine when the first and second input values are 4 and 10, but not when they're 8 and 17. The formula is the following, and it is in cell F4:
{=INDEX(Table1[Output], MATCH($F$2 & "|" & $F$3, Table1[1st input variable] & "|" & Table1[2nd input variable], 0))}
(The presence of curly braces means that we must enter the formula with Ctrl + Shift + Enter instead of just Enter.)
Here's a screenshot for the first successful example:
Here's a screenshot for the second failed example:
I tried changing the third parameter of the MATCH function from 0 to 1, but it returns J (which corresponds to 15 in the second column, but 17 < 15) instead of K (which corresponds to 20, since 17 > 20 and 20 is the closest value to 17 that is immediately after it.)
How can I achieve what I want?
if you have Excel 365 then you can use the new Filter-function:
=INDEX(FILTER(Table1[output],(Table1[1st Input variable]=first)*(Table1[2nd input variable]>=second),"no result"),1)
I named F3 "first" and F4 "second".
FILTER returns all output values where
column A = value from F3
column B >= the value from F4.
INDEX selects the first row of the FILTER-result
Not the best way, but you can round the second input to what you need. In your example, all your values are multiples of 5. Just create an exception for number 1 with an IF.
Here's what I tried:
={INDEX($C$1:$C$12;MATCH(F7&IF(ROUNDUP(G7/5;0)=1;1;ROUNDUP(G7/5;0)*5);$A$1:$A$12&$B$1:$B$12;0))}
Notice it's an array formula.
I am trying to create a formula that checks Column A for unique values, then takes that value and checks column B for unique values and orders them sequentially in Column C. Column C is the where the formula goes. It's not in the actual data set.
This is what I want my data set to look like.
For example, I want to find unique Entry Number "123-A. I then want to look within that entry number and find the unique codes in Column B and order them sequentially. The first two are the same, so they both are sequence 1. Then the third row as a new code, "Y09", so it will get sequence 2. Once the next unique entry number is identified, I want to reset the sequential count. Thanks!
So, the first thing we want to do is check if a number has already been allocated. Since there are 2 columns to check, we need to use INDEX MATCH with an Array Condition instead of just a VLOOKUP:
INDEX($C$1:$C1, MATCH(1, ($A$1:$A1=$A2)*($B$1:$B1=$B2), 0))
These formula are intended for cell C2 - notice how we left the second cell in each Range reference without the $ to lock it in place. This means it will always stop at the row above the formula
If this works, we're done. If it doesn't we get an error - so, we can use IFERROR:
=IFERROR(INDEX($C$1:$C1, MATCH(1, ($A$1:$A1=$A2)*($B$1:$B1=$B2), 0)), ???)
On to replacing those question marks!
Since we don't have a match, we need to find the largest match for the Entry Number, and add 1 for it. If you have Office365 or Office2019, we can just use the MAXIFS function. Otherwise, we will have to use SUMPRODUCT and MAX to get the same result: (If the Entry Number does not exist, this will return 0)
MAXIFS($C$1:$C1, $A$1:$A1, $A2)
SUMPRODUCT(MAX($C$1:$C1 * ($A$1:$A1=$A2)))
Then, Add 1:
=IFERROR(INDEX($C$1:$C1, MATCH(1, ($A$1:$A1=$A2)*($B$1:$B1=$B2), 0)), MAXIFS($C$1:$C1, $A$1:$A1, $A2) + 1)
=IFERROR(INDEX($C$1:$C1, MATCH(1, ($A$1:$A1=$A2)*($B$1:$B1=$B2), 0)), SUMPRODUCT(MAX($C$1:$C1 * ($A$1:$A1=$A2))) + 1)
It appears that your data is already organized/sorted by the column A and B values. If this is the case, we can construct a formula implementing the following rules:
if the adjacent A and B values match the values above them, copy down the C value from above
if the adjacent A value matches the value above it, but the B value does not, then increment the C value from above.
If the A value does not the value above it, then set the C value to 1
In C2 enter 1. In C3 enter:
=IF(AND(A3=A2,B3=B2),C2,IF(A3=A2,C2+1,1))
and copy downward:
if your data is not organized in the same way your picture indicates, then ignore this solution.
I have a table that looks like this and I want to use something like VLOOKUP to find the 2nd column value that's non-empty.
An example would be:
VLOOKUP("Kiwi", Range A:B, 2, 0) should return Green
VLOOKUP("Apple", Range A:B, 2, 0) should return Red
VLOOKUP("Pineapple", Range A:B, 2, 0) should return Yellow
Note that the first occurence of Pineapple is empty, but it still should return Yellow. and I don't have to use VLOOKUP and I am open to other methods too.
I would appreiate help.
Thank you.
You can use an INDEX/MATCH array formula:
The formula is:
{=INDEX(B1:B7;MATCH(1;(A1:A7=E1)*(B1:B7<>"");0))}
Be aware that this is an array formula, you need to enter it with CTRL + SHIFT + ENTER.
This formula will check if column A is "Pineapple" and column B is not a blank. You will get an (imaginary) array that looks like this:
The values in both arrays are multiplied and only for the last two rows, the result will be 1. MATCH looks up the 1 in this array (only the first occurrence!) and delivers the row number for "Yellow" to the INDEX function. INDEX then picks up the value in row 6 - which is "Yellow".
I hope that helps you.
MATCH will give you offset, so you can use OFFSET function.
IFNA is there to display empty instead of #N/A
=IFNA(OFFSET(B1;MATCH(1;(A1:A7=E1)*(B1:B7<>"");0)-1;0);"")
I have an excel file like the following:
and I would like to replace the value of votes and avgsocre of those rows with Print = 1 with the rows I have in another file, which looks like the following:
The index number in the second file is exactly off by 1 and since there are also \N's with values from rows with Print = 0, so I cannot use replace then vlookup.
Would appreciate any help on this.
It looks like the variables that I'll call Design and Author uniquely identify your observations, so it would probably make more sense to look up Votes and AvgScore based on those values rather than trying to use the row-1 value in col A of the second file.
In your main file, make a column Votes_new with the formula (in this example, for row 2707):
{=INDEX(SecondFile!$A$1:$H$11, MATCH(1, (SecondFile!$B$1:$B$11=$A2707)*(SecondFile!$C$1:$C$11=$B2707)*(SecondFile!$G$1:$G$11=1), 0), 4)}
Use the same formula with 5 instead of 4 in the last argument in a column for AvgScore_new.
This formula matches on three criteria: Design (equal to the value in the current row), Author (equal to the value in the current row), and Print (equal to 1). It gets the value from the 4th column of the data in the second file, which is Votes (or the 5th column, which is AvgScore).
Note that you have to enter as an array formula using Ctrl+Shift+Enter.
Also note that this will return a #N/A error if there is no match (either because the Design-Author combination is not present or because Print is not equal to 1) so you may want to enclose this in an IFERROR() formula.
Alternatively, if you really want to look up by row reference, you can use =VLOOKUP(ROW()-1, ...) to look up the current row (e.g., 2707) minus 1 (2706) from the second file, and then find the value of print: =VLOOKUP(ROW()-1, SecondFile!$A1$H11, 7, FALSE). You can use this in an IF() function to look up a different column if the value is one: e.g., =IF(VLOOKUP(ROW()-1, SecondFile!$A1$H11, 7, FALSE)=1, VLOOKUP(ROW()-1, SecondFile!$A1$H11, 4, FALSE), "Value not found")
I want to use match function to find a value (say, value in A3) from a named range, and then use offset to give the value right of the value searched, from the named range. The named range is called "CATEGORY"
I've written something like this:
=OFFSET(MATCH(CATEGORY, A3, 0)), 1, 1, 1, 1)
Its not working, how do I make it work?
MATCH returns a number; OFFSET needs a range as reference.
Also, MATCH requires a value as the first argument and a range as the second.
The following works
=OFFSET(category, MATCH(A3, category,0)-1, 1)
assuming that you are looking for the value in A3 in the column CATEGORY
Note that MATCH returns 1 for the first cell - so you need to subtract one to stay on the same line; similarly, you need a column offset of +1 to get "cell to the right". The 0 third argument for MATCH means "exact match".
Example of this formula at work: