简体   繁体   English

Excel:部分匹配范围 1 与范围 2 中的每个单元格字符串,然后在列中返回值

[英]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:假设我有一堆 URL:

| 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:我如何编写一个公式来下拉 URL 列表,使其看起来像:

| 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微软OFFICE

=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.这仅在字符串不包含列表中的多个部分字符串时才有效,但您可以使用部分匹配的行号对查找数组进行INDEX

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 .在这里,我们首先生成一个向量,使用FIND测试查找数组中所有值的位置。 All but one element in this vector are #VALUE!这个向量中除了一个元素之外的所有元素都是#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!请注意#VALUE!上的算术#VALUE! return will still yield #VALUE! return 仍然会产生#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;另请注意,上述答案中生成的行号向量( ...ROW($A$1:$A$5) )必须从第一行开始; 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.例如,如果您的查找数组从第 3 行开始,您需要执行类似...ROW($A$3:$A$7)-2才能获得所需的结果。

Finally, we replace any #VALUE!最后,我们替换任何#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.此行号用于INDEX以实现所需的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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