Excel VBA/Formula to find a cell that includes search term? - excel

I was not sure how to really create the question...
But the problem I am having is this: I have a list (in rows) that relate to a regulatory document, and after trying to create some sort of for loop or elaborate VLookUp/Index formula, I'm requesting help.
For example:
Now I want to use the rows to find the corresponding section in the document. I've already extracted and formatted the compliance document so it is in excel format.
So what I really need is this: a formula or VBA script that can
1. take the compliance number (for example 1A-1 which exist in Cell A3) and go find a cell (in single column D) that has JUST 1A-1, not 1A-1.1, not 1A-1.1.2, etc. and return it to the adjacent cell to 1A-1, for example.
Many thanks ahead of time... I am so lost!! :/

VLOOKUP vs INDEX/MATCH
You can do the 'lookup' two ways (that I'm aware of):
Using VLOOKUP:
The B3 cell contains your formula
=IF(ISERROR(VLOOKUP(A3,C:D,2,FALSE)),"",VLOOKUP(A3,C:D,2,FALSE))
where 'FALSE' is indicating there has to be an exact match and the data doesn't have to be sorted.
Using INDEX with MATCH:
The F3 cell contains the Index/Match formula
=IF(ISERROR(MATCH(A3,C:C,0)),"",INDEX(D:D,MATCH(A3,C:C,0)))
where '0' is indicating there has to be an exact match and the data doesn't have to be sorted.
INDEX/MATCH preferable!?
The MATCH function finds the position (row number if whole column is used) of the found match. This way (there's another) of using the INDEX function uses exactly this found match to return a cell's value in that position (row) in ANY specified column range (column). So they are the ideal combination.
With the VLOOKUP function you have to additionally specify the column index (range_lookup) of a range which could get complicated when the columns aren't adjacent as in this case. Most importantly, the function doesn't work if the lookup data is to the right of the match data.
VLOOKUP NOT WORKING! INDEX/MATCH STILL WORKING!

try this formula
The formula in cells
B2: =INDEX(E:E,MATCH(A2,F:F,0))
C2: =INDEX(G:G,MATCH(A2,F:F,0))
MATCH(A2,F:F,0) is finding Cell A2 in column F (0 means it will find
exact match) and will return the first row number when it would find that
INDEX(E:E,MATCH(A2,F:F,0)) will return contents of column E where row number is returned by the Match formula

Related

Return multiple matches when range is dynamic

I have a column which is a dynamic named range:
A
A
B
C
A
How can I apply a MATCH or other formula which returns the row or cell address of all A values in the dynamic named range?
A dynamic named range is used so all row or address of A values can automatically appear as a spill rather than having to drag down the formula.
I have seen numerous formulas in this structure:
=INDEX($B$2:$B$8, SMALL(IF($A$11=$A$2:$A$8, ROW($A$2:$A$8)-ROW($A$2)+1), ROW(1:1)))
However, most of these seem to return the first instance (row or cell address of the first match) and requires the formula to be dragged down manually rather than a spill, which is more preferable.
Any help will be greatly appreciated.
Use FILTER:
=FILTER(ROW(A1:A5),A1:A5=C1)
This will return the Row number everywhere they match.
From there it is pretty easy to include that in INDEX:
=INDEX(B:B,FILTER(ROW(A1:A5),A1:A5=C1))
If you do the full column in the index, you do not need the ROW($A$2:$A$8)-ROW($A$2)+1 as returning the actual row instead of relative works just fine.
Try something like =SORT(ROW($A$1:$A$5)/($A$1:$A$5=$C$1)). This will get you the matches of a value in C1 with a spill formula, leaving behind #DIV/0 errors
See example below:
Other answers that you might be able to take ideas from are here: Match Exact Row Number of Nth Largest Value In Data With Duplicates
You could be thinking of:
=SMALL(IFERROR(ROW(A1:A5)*MATCH(A1:A5,C1,0),FALSE),ROW(A1:INDEX(A:A,COUNTIF(A1:A5,C1),)))
Where your data start in A1 and your compare value is in C1.
If you have Excel 365, this is easier:
=INDEX(SORT(ROW(A1:A5)*MATCH(A1:A5,C1,0)),SEQUENCE(COUNTIF(A1:A5,C1)))
or better yet:
=LET( dataRange, A1:A5,
compareTo, C1,
INDEX(SORT(ROW(dataRange)*MATCH(dataRange,compareTo,0)),SEQUENCE(COUNTIF(dataRange,compareTo))) )

Lookups with Multiple Non-Exact Criteria using INDEX-MATCH - Problem finding nearest values that best meet conedition

I am trying to make lookups with Multiple Non-Exact Criteria using INDEX-MATCH.
The formual looks like this:
=INDEX(C314:C318;MATCH(1;(D314:D318>=G313)*(E314:E318>=G314);0))
Criterias are: greater or equal to amount X.
Formula works fine, however when using a long list of values, it does not find the best matching value, it finds the first value that matches the criteria.
For example.
Condition 1 is: code "find code equal to 2055516"
Condition 2 is: numerical "find value equal or above 77"
Condition 3 is: alphabetical "find letter equal or greater than H"
In a large dataset where I´ve got many values, it finds only the next best value that matches this criteria. First value that meet that condition would be "80" and "R", however, following values in my dataset, way below, meet much better those criteria with "78" and "I". Problem here is clear I guess.
How can I adapt my formula to look for those much more fitting values that meet my condtions?
Dataset table looks like this:
The formula should return the name "A, B, C, D, E" of the best maching product.
I used a helper column called Helper to rank the letters in Condition 2 alphabetically first using the following formula (drag it down to apply to all rows):
=COUNTIF(Condition_2,"<="&Condition_2)
then use the following formula to find the best match (although it is an array formula it does not need to be confirmed by Ctrl+Shift+Enter):
=INDEX(Product1,MATCH(AGGREGATE(15,6,Helper/((Condition_1>=77)/(Condition_2>="H")),1),Helper,0))
Replace the named ranges in the above formula with the actual ranges in your worksheet.
Replace , with ; as the delimiter in all formulas to suit your system.
EDIT #2
Based on the new scenario, the problem can be solved by AGGREGATE function solely given that the look up value is a number (EAN)
The formula in Cell J2 of my above example is:
=AGGREGATE(15,6,EAN/((DIMENSION=F2)/(LOAD_INDEX>=G2)/(SPEED_INDEX>=H2)),1)
Please note the following are all named ranges which needs to be replaced with the actual range on your workbook:
DIMENSION being B2:B8
LOAD_INDEX being C2:C8
SPEED_INDEX being D2:D8
EAN being A2:A8
If you do not want to show the error #NUM! for no matching result, you can use IFERROR to return a blank cell as shown in Cell J3 of my example. The formula is:
=IFERROR(AGGREGATE(15,6,EAN/((DIMENSION=F3)/(LOAD_INDEX>=G3)/(SPEED_INDEX>=H3)),1),"")
EDIT #3
Please use the following array formula (need to confirm by pressing Ctrl+Shift+Enter) to find the closest match of LOAD INDEX and SPEED INDEX with the help of a Helper column.
{=INDEX(EAN,MATCH(AGGREGATE(15,6,Helper/((LOAD_INDEX/((DIMENSION=G2)/(LOAD_INDEX>=H2)/(SPEED_INDEX>=I2)))=AGGREGATE(15,6,LOAD_INDEX/((DIMENSION=G2)/(LOAD_INDEX>=H2)/(SPEED_INDEX>=I2)),1)),1),Helper/((LOAD_INDEX/((DIMENSION=G2)/(LOAD_INDEX>=H2)/(SPEED_INDEX>=I2)))=AGGREGATE(15,6,LOAD_INDEX/((DIMENSION=G2)/(LOAD_INDEX>=H2)/(SPEED_INDEX>=I2)),1)),0))}
The logic is to first find the closest matches to LOAD INDEX and then find the closest match to SPEED LIMIT from the range with the closest matches to LOAD INDEX.
Again if you do not want to show #NUM! error for no matching result, you can use IFERROR to return the desired result.
Let me know if there is any question. Cheers :)
This might be done through something much smarter, but the following worked for me:
Formula in G2, following the above sample data to return the row with the best match:
=INDEX(A2:A6,MATCH(SMALL(IF(B2:B6-E2>-1,B2:B6-E2+IF(CODE(C2:C6)-CODE(F2)>-1,CODE(C2:C6)-CODE(F2),""),""),1),IF(B2:B6-E2>-1,B2:B6-E2+IF(CODE(C2:C6)-CODE(F2)>-1,CODE(C2:C6)-CODE(F2),""),""),0))
Note Enter as array formula through Ctrl+Shift+Enter
When no criteria matches, it will return an error, you could catch through IFERROR().

How to compare one cell with a range and if TRUE - copycontent from the right cell?

I need to compare two columns I and L and copy matched result from M column. It is a list of 1000+ product codes (I,L) and EAN codes (M). So if cell I1 is found in range of L1:L1000 (lets say it found in L3 cell), then formula should copy the M3 cell.
Tried VLOOKUP and MATCH and some IF, but cannot figure it out how to make it work as it returns blank or REF! or N/A or errors-out completely. I'm desperate and don't know what i'm doing wrong...
=VLOOKUP(I1:I1164,L1:L1164,13,FALSE)
and with
=IF(ISNUMBER(SEARCH(I1,L1:L1000),M1," "")
Result should be in N column.
When using VLOOKUP, you need the lookup range to include both the range of values you're looking for (which MUST to be the first column) and the return values (whose column you specify as relative to the range. So in your case, you'll be looking up in L1:M1164 and using column 2 as return results (since column M is the second of L1:M1164).
Also, the value you're looking for will probably be just an item, relative to the current line. I'd thus try it that way (in N1):
=VLOOKUP(I1;$L$1:$M$1164;2;FALSE)
Wrapping it up in an IFERROR as suggested in SJR's answer might be a good idea too.
Try this
=iferror(index(m1:m1000,match(i1,l1:l1000,0)),"")
The match bit returns 3, the index bit then looks for the 3rd value in column M; the Iferror returns an empty string in the event of an error (i1 is not found).

Excel Formula not accepted but no reason for error

I have a if formula with a number of criterias it has to match.
When I have shortened the formula down it works from beyond - IF(LEFT(A6,1)="2"
but there are no reasons it should error at this point? Any help?
=IF(LEFT(A6,2)="10","Area 1",IF(LEFT(A6,2)="12","Area 2",IF(LEFT(A6,2)="13","Area 3",IF(LEFT(A6,2)="14","Area 4",IF(LEFT(A6,2)="15","Area 5",IF(LEFT(A6,2)="16","Area 6",IF(LEFT(A6,2)="17","Area 7",IF(LEFT(A6,1)="2","Bulk",IF(LEFT(A6,1)="4","Intl",IF(LEFT(A6,2)="7","CGCC","Ad-Hoc"))))))))))
You can try combining IF and VLOOKUP.
=IF(LEFT(A6,1)="4","Intl",IF(ISNA(VLOOKUP(LEFT(A6,2),{"7","CGCC";"10","Area 1";"12","Area 2";"13","Area 3";"14","Area 4";"15","Area 5";"16","Area 6";"17","Area 7"},2,FALSE)),"Ad-Hoc",VLOOKUP(LEFT(A6,2),{"7","CGCC";"10","Area 1";"12","Area 2";"13","Area 3";"14","Area 4";"15","Area 5";"16","Area 6";"17","Area 7"},2,FALSE)))
I embedded the array in the formula but you can prepare a table (assume G1:H7) like this:
and then use the VLOOKUP with the reference:
=IF(LEFT(A6,1)="4","Intl",IF(ISNA(VLOOKUP(LEFT(A6,2),G1:H7,2,FALSE)),"Ad-Hoc",VLOOKUP(LEFT(A6,2),G1:H7,2,FALSE)))
IFNA or IFERROR could also be used but they are not available in Excel 2003.
Your entire formula could be shortened to 2 VLOOKUP functions, by putting your data into a table, with your ID column on, say, column A of sheet2, and your results column on column B of sheet2. This would look as follows:
=IFERROR(VLOOKUP(LEFT(A6,2),'Sheet2'!A:B,2,0),IFERROR(VLOOKUP(LEFT(A6),'Sheet2'!A:B,2,0),"Ad Hoc"))
What this does is: first try to match the left 2 characters in A6 to one of your ID's in column A in sheet2. If that creates an error, it tries to match the left 1 character in A6 to one of your ID's in column A of sheet2. Either way, it returns the matching value in column B of sheet2. If no match is found, it returns "Ad Hoc".

Matching two columns in one sheet to two columns in another

I need to assign a status to a row based on a VLOOKUP query between two worksheets. The problem is that the identifier is not always unique. However, the identifier + a date value should be unique. I wanted to use:
=VLOOKUP(A3&H3,'OtherSheet'!D:E,1,FALSE)
with A3 being the identifier and H3 being the corresponding date. D in the other sheet is the identifier and E is the date column. However, I keep getting #N/A.
Does this mean that there are no matches with the "identifier+date" or is Excel looking for "identifier+date" in either column D or E? If the latter is true, how can I let Excel concatenate D and E when matching to the search pattern?
There's work around without using CTRL+Shift+Enter.
Use this formula that will match A3 in D column of othersheet and H3 with the date in column E of the othersheet.
=INDEX(OtherSheet!F:F,MATCH(1,INDEX((OtherSheet!D:D=A3)*(OtherSheet!E:E=H3),),0))
The formula will return data from F column of OtherSheet.
You can modify the range OtherSheet!F:F as appropriate.
That formula is looking to find A3 concatenated with H3 (identifier&date) in OtherSheet ColumnD that contains only identifiers, so will inevitably fail. Yes, Excel is looking for “identifier+date” in column D.
Excel will happily concatenate A3 with H3 ‘on the fly’ (within a formula) but will not so happily concatenate OtherSheet ColumnD and ColumnE values in the same way. The conventional solution, because usually simplest in a case like this, is to prepare for the VLOOKUP by adding a helper column that concatenates the D and E values while preserving these in the same row as the value sought.
Because VLOOKUP will only look to the right this is usually a column that is added to the left of the value being searched for, so say either in C or by insertion of a column immediately to the right of C. However, since you are only checking a single column the location is not critical. You might add this (in OtherSheet) as ColumnZ, with a formula such as:
=D2&E2
copied down to suit*. Again because you are only checking a single column it does not matter which row such a formula is placed in.
However, because only checking whether A3&H3 exists in OtherSheet a simple alternative may be to apply COUNTIFS:
=COUNTIFS(OtherSheet!D:D,A3,OtherSheet!E:E,H3)
Any result other than 0 from this should indicate that the combination being tested for exists in OtherSheet – without need for a helper column.
* Depending on the format of your identifiers it is possible that concatenation may introduce ambiguity. For example ID90 concatenated with 11/1/15 may not be distinguishable from ID901 concatenated with 1/1/15, so it may be advisable if taking this approach to introduce a delimiter, in both the VLOOKUP formula (say A3&"|"&H3 rather than just A3&H3) and therefore also in the helper column, say =D2&"|"&E2.
You likely would want to use Index/Match instead. Vlookup is tricky when it comes to searches for multiple things. Here's the way you would use Index/Match:
Without knowing how your spreadsheet is set up, here's how you could do it:
If I understand correctly, you want to use A3 to find the match in OtherSheet!D, and H3's match in OtherSheet!E. Index match is perfect for this. Instead of vLookup, use
=Index(OtherSheet!D:D&","&Text(OtherSheet!E:E,"mm-dd-yyyy"),Match(A3&H3,OtherSheet!D&OtherSheet!E,0)), and enter with CTRL+SHIFT+ENTER.
What the Index() will return is the concatenated Identifier and Date, separated with a comma. If, though, you have a table like this:
That index/match formula will return "Batman". The index to return is the named range G2:G5. You're looking for a match on A1 (the Identifier) and B1 (the Date), then you're searching for (in the order you just put) the Identifier to be in the range E2:E5, and the Date to be in F2:F5. When there's a match for both, it returns the name in G2:G5.
Here's a link to a site on using Index/Match, and another and its advantages over vlookup.

Resources