Combining Match and Offset - excel

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:

Related

How to make a drop down list with conditions? [duplicate]

I want the following in Excel:
Two dropdown lists in adjacent cells:
Dropdown list #1 | Dropdown list #1
Dropdown list 1:
One
Two
Three
If I select One in the first cell, the list in the second cell should contain these choices:
One:
1.1
1.2
1.3
If I select Two in the first cell, the list in the second cell should contain these choices:
Two:
2.1
2.2
2.3
And so on. There are a lot of tutorials around, but I'm having some hassle figuring out which of them addresses this exact question.
Update: An example. When selecting f.ex. Group 1 under the Group heading (col A), the entries listed under Group 1 to the right (col D) should appear under the Item heading (col B). And the same for the other Groups.
Update as promised:
When you're using a List for validation, you have to input a range as shown below.
The OFFSET function allows to to dynamically set a range based on its input criteria.
If you consider this:
=OFFSET(C1,0,0,1,1)
Argument 1 = Anchor cell
Argument 2 = Number of rows to move, you can use minus number here to move rows up and positive numbers to move down
Argument 3 = Number of columns to move. Negative is left, positive to the right.
Argument 4 = Height of the range (can't be negative and is optional, default is 1)
Argument 5 = Width of the range (can't be negative and is optional, default is 1)
In this instance, the range returned would be C1 as we have no row or column offset and height and width is set to 1
The MATCH function will return an index of where a value appears in a range of cells (range must be either 1 cell wide or 1 cell high)
Based on the above screen print =MATCH("Group2",D1:F1,0) will return 2, as "Group2" appears in the second cell in the D1:F1 range. ("Group1" would return 1, "Group3" would return 3, and "Group4" would return #N/A as it doesn't exist).
So based on that we can put the MATCH function in as our 2nd argument in the OFFSET function, and pick the column that matches the first argument in the MATCH function.
=OFFSET(C1,0,MATCH("Group2",D1:F1,0),1,1) will return back range E1 as we've shifted the columns by 2 from C1 because of the MATCH
=OFFSET(C1,1,MATCH("Group2",D1:F1,0),3,1) will now return back E2:E4 as we've increased the height of the range to 3 and the row offset to 1.
And finally we can change the "Group2" value in the MATCH function to a cell value that will mean the range will dynamically change.
Here I've used Cell A2 =OFFSET(C1,1,MATCH(A2,D1:F1,0),3,1) so whatever value is in cell A2 will be used to offset the range.
And the last thing to do is to put the dynamic range into the validation (I used B2)
This will dynamically set the validation range.
When I'm using OFFSET function with multiple arguments and I'm not sure that it's returning back the right range, I wrote a small helper User Defined Function that I just put in a VBA module.
Public Function GetAddress(rng As Range) As String
GetAddress = rng.Address
End Function
This allows me to put the offset formula in and it will return back the range address. So I can make sure it's right.
There may be a built in function for this, but I've never found it.
This solution avoids the use of the volatile OFFSET function.
For the first Data Validation located at A2 use this formula:
=$D$1:$F$1
For the second Data Validation located at B2 use this formula:
= INDEX( $D$2:$F$4, 0, MATCH( $A$2, $D$1:$F$1, 0 ) )
The second formula uses the value selected from the data Validation in A2 to MATCH the corresponding column in D1:F1 and applies the column number to the INDEX function to return the whole column in range D2:F4.
The function INDEX( reference, row number, column number ) returns the entire column or row of the referenceby entering 0 as the row number or column number respectively.

Formula to rearrange order

I specify a value to be looked up in a range and then another
=IFERROR(INDEX($A$3:$A$5,MATCH(1,($A$6:$A$6<=$B$2)*($A$3:$A$5>=$B$2),0)),"")
It returns a blank cell(A7, expected result 25) for some reason when A6:A6 equals to a value of B2 but it finds a greater than equal to when its less, in the second range parameter.
What I need is first search a range and if not found search a second range. Can it be modified to search backward?
B2=22
A3=7
A4=25
A5=45
A6=2
A7=25
What I need is first search a range and if not found search a second
range.
We need to perform two separate lookups.
=IFERROR(VLOOKUP("h",A1:A5,1, FALSE), VLOOKUP("h",B1:B5, 1, FALSE))
Following formula attempts to look up "h" in array A1:A5, if it's not found, it performs VLOOKUPon the second array. Obviously, you can replace "h" with whatever cell reference you need, but the principle of the formula stays the same.

MATCH reverse order

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).

Excel: Dropdown list dependant on other dropdown list

I want the following in Excel:
Two dropdown lists in adjacent cells:
Dropdown list #1 | Dropdown list #1
Dropdown list 1:
One
Two
Three
If I select One in the first cell, the list in the second cell should contain these choices:
One:
1.1
1.2
1.3
If I select Two in the first cell, the list in the second cell should contain these choices:
Two:
2.1
2.2
2.3
And so on. There are a lot of tutorials around, but I'm having some hassle figuring out which of them addresses this exact question.
Update: An example. When selecting f.ex. Group 1 under the Group heading (col A), the entries listed under Group 1 to the right (col D) should appear under the Item heading (col B). And the same for the other Groups.
Update as promised:
When you're using a List for validation, you have to input a range as shown below.
The OFFSET function allows to to dynamically set a range based on its input criteria.
If you consider this:
=OFFSET(C1,0,0,1,1)
Argument 1 = Anchor cell
Argument 2 = Number of rows to move, you can use minus number here to move rows up and positive numbers to move down
Argument 3 = Number of columns to move. Negative is left, positive to the right.
Argument 4 = Height of the range (can't be negative and is optional, default is 1)
Argument 5 = Width of the range (can't be negative and is optional, default is 1)
In this instance, the range returned would be C1 as we have no row or column offset and height and width is set to 1
The MATCH function will return an index of where a value appears in a range of cells (range must be either 1 cell wide or 1 cell high)
Based on the above screen print =MATCH("Group2",D1:F1,0) will return 2, as "Group2" appears in the second cell in the D1:F1 range. ("Group1" would return 1, "Group3" would return 3, and "Group4" would return #N/A as it doesn't exist).
So based on that we can put the MATCH function in as our 2nd argument in the OFFSET function, and pick the column that matches the first argument in the MATCH function.
=OFFSET(C1,0,MATCH("Group2",D1:F1,0),1,1) will return back range E1 as we've shifted the columns by 2 from C1 because of the MATCH
=OFFSET(C1,1,MATCH("Group2",D1:F1,0),3,1) will now return back E2:E4 as we've increased the height of the range to 3 and the row offset to 1.
And finally we can change the "Group2" value in the MATCH function to a cell value that will mean the range will dynamically change.
Here I've used Cell A2 =OFFSET(C1,1,MATCH(A2,D1:F1,0),3,1) so whatever value is in cell A2 will be used to offset the range.
And the last thing to do is to put the dynamic range into the validation (I used B2)
This will dynamically set the validation range.
When I'm using OFFSET function with multiple arguments and I'm not sure that it's returning back the right range, I wrote a small helper User Defined Function that I just put in a VBA module.
Public Function GetAddress(rng As Range) As String
GetAddress = rng.Address
End Function
This allows me to put the offset formula in and it will return back the range address. So I can make sure it's right.
There may be a built in function for this, but I've never found it.
This solution avoids the use of the volatile OFFSET function.
For the first Data Validation located at A2 use this formula:
=$D$1:$F$1
For the second Data Validation located at B2 use this formula:
= INDEX( $D$2:$F$4, 0, MATCH( $A$2, $D$1:$F$1, 0 ) )
The second formula uses the value selected from the data Validation in A2 to MATCH the corresponding column in D1:F1 and applies the column number to the INDEX function to return the whole column in range D2:F4.
The function INDEX( reference, row number, column number ) returns the entire column or row of the referenceby entering 0 as the row number or column number respectively.

Get column by finding value in the row

In Excel, I've looked into hlookup, vlookup, match and index but none of these functions do following:
I need to find a text value in A1:Z1 and get the column of it. For example, I found the value in F1, then I want a result F:F.
EDIT: This is the function I want it to be added to:
=COUNTIFS(Source!B:B;Result!C3;Source!AT:AT;Result!$D$2)
I need the Source!B:B and alternatively Source!AT:AT be a search function that looks for a specific value in my table row and column in different sheet (Source).
INDEX function can return a whole column, e.g. assuming you want to search for "x" in A1:Z1 try
=INDEX(A:Z;0;MATCH("x";A1:Z1;0))
Note: this doesn't return a text string like F:F, it returns a reference to the column in question which you would normally use within a function that expects a whole column reference, e.g. SUM, SUMIF, LOOKUP etc.
Edited: You can use the above in COUNTIFS function like this:
=COUNTIFS(INDEX(Source!A:Z;0;MATCH("x";Source!A1:Z1;0));Result!C3;Source!AT:AT;Result!$D$2)
That makes the first range dynamic (between columns A and Z - extend as required) based on where "x" is first found in Source!A1:Z1, e.g. if "x" is first found in J1 then COUNTIFS uses Source!J:J for the first range - you can do that for any of the ranges in COUNTIFS
You can use this (it will return E:E, which you could then use INDIRECT with to return a workable range). This uses "words" as the word to match and stop at the first match:
=SUBSTITUTE(
ADDRESS(1,MATCH("words",$A$1:$Z$1,0),4),"1",
":"&
SUBSTITUTE(ADDRESS(1,MATCH("words",$A$1:$Z$1,0),4),"1",""))
This works by using MATCH to find the position of the occurrence in your range (in this case, 5), and then using ADDRESS, with row_num = 1 and column_num = the result of the match. Using 4 for the abs_num argument ensures that the returned value will be without the $'s. You then substitute out the 1 in E1 with ":" and concatenate it with the same formula, giving you E:E as a string. You can then do look-ups based on that range (using INDIRECT), such as this:

Resources