简体   繁体   中英

Excel: Partial matching Range 1 against each cell string individually in Range 2, then returning value in column

If you can help me I love you.

Let's say I have a bunch of URLs:

| Range 1                |
|------------------------|
| www.orange.com         |
| www.orange.example.com |
| www.example.red.com    |
| www.example.com/blue   |

And I also have a table like this:

| Range 2 | Range 3 |
|---------|---------|
| orange  |       1 |
| red     |       2 |
| blue    |       3 |
| green   |       4 |
| pink    |       5 |

How could I write a formula to pull down alongside the URL list so that it looks like:

| Range 1                | Results |
|------------------------|---------|
| www.orange.com         |       1 |
| www.orange.example.com |       1 |
| www.example.red.com    |       2 |
| www.example.com/blue   |       3 |

Essentially doing partial matches every time and then returning a result to the right.

Driving me nuts, you're my only hope!

Google Sheets.

=ArrayFormula(LOOKUP(1,0/COUNTIF(A1,"*"&C$1:C$5&"*"),D$1:D$5))

在此处输入图片说明

MSOFFICE

=LOOKUP(1,0/FIND(C$1:C$5,A1),D$1:D$5)

This will only work if the string does not contain multiple partial strings in your list, but you can just INDEX the lookup array using the row number of a partial match.

For example:

=IFERROR(
INDEX($B$1:$B$5, CONCAT(IFERROR(
FIND($A$1:$A$5,D5)+ROW($A$1:$A$5)-FIND($A$1:$A$5,D5),
""))),"")

在此处输入图片说明

Here we first generate a vector testing the location of all values in the lookup array against the string using FIND . All but one element in this vector are #VALUE! . The remaining element is the location of the first character in the string being tested that is the same as the value in the lookup array.

Next we convert this value to the row number in the lookup array by adding another equal sized vector of sequential row numbers, and then subtracting the original quantity. Note that arithmetic on a #VALUE! return will still yield #VALUE! . Also note that the vector of row numbers as generated in the answer above (the ...ROW($A$1:$A$5) ) must begin in the first row; if your lookup array starts in row 3 for example, you would need to do something like ...ROW($A$3:$A$7)-2 to get the result you desire.

Finally, we replace any #VALUE! return with an empty string and concatenate the entire vector. The result is the row number of the lookup array which contains a partial string in the full string. This row number is used in an INDEX to achieve the desired results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM