简体   繁体   English

如何在Excel中使用vlookup在单元格中查找字符串

[英]How to use vlookup to find a string in a cell in excel

In excel I have a column A:A with a comment text (a lot of text). 在excel中,我有一栏A:A带有注释文本(很多文本)。 I want to make a column (col B) that put the category if a certain word is containing in the comment. 我想创建一列(列B),如果注释中包含某个单词,则会放入类别。

I have in my sheet2 a list of words (java,excel,...). 我的sheet2中有一个单词列表(java,excel,...)。 If java is containing in the comment text, I want to put in column B "java". 如果注释文本中包含Java,则我想在B列中添加“ java”。 If the comment text contain "excel" i want to have "excel as category. 如果注释文本包含“ excel”,我希望将“ excel”作为类别。

I thought to do that with vlookup, but I don't know how. 我本来想用vlookup做到这一点,但我不知道怎么做。

Assuming that Sheet2!$A$1:$A$50 contains the search list (without blanks) as per Palo's solution then try this formula in B1 copied down 假设Sheet2!$A$1:$A$50包含根据Palo解决方案的搜索列表(不带空格),然后尝试将B1中的公式向下复制下来

=LOOKUP(2^15,SEARCH(Sheet2!$A$1:$A$50,A1),Sheet2!$A$1:$A$50)

That returns the last match from Sheet2!$A$1:$A$50 so if you have any words in the list that are substrings of other words in the list, like Excel and Excellent then the longer word should be later in the list - so ideally Sheet2!$A$1:$A$50 would be sorted by length 这将返回Sheet2!$A$1:$A$50最后一个匹配项Sheet2!$A$1:$A$50因此,如果列表中有任何单词是列表中其他单词的子字符串,例如Excel和Excellent,则较长的单词应位于列表的后面-因此理想情况是Sheet2!$A$1:$A$50将按长度排序

The SEARCH function can take a range as input. SEARCH功能可以将范围作为输入。 So one solution would be this, with a helper column. 因此,一个解决方案就是使用辅助列。 Assuming you have 50 words in Sheet2, and that words in the comments are separated by space, and that you are OK to only return the first found word: 假设Sheet2中有50个单词,并且注释中的单词用空格分隔,并且可以只返回第一个找到的单词:

C1: C1:

=SEARCH(Sheet2!$A$1:$A$50,A1)

B1: B1:

=MID(A1,C1,FIND(" ",A1,C1)-C1)

The formula in C gives you the location of the first found word within the comment. C中的公式可为您提供注释中第一个找到的单词的位置。 You then use it to extract that word from your comment by finding the first space after the word's location. 然后,您可以通过查找单词位置后的第一个空格,使用它从注释中提取该单词。 There should exist a more elegant solution though! 不过应该有一个更优雅的解决方案!

Edit: When applied to comment "123 excellent", my formula will give "excellent" although your word list only contains "excel". 编辑:当应用于注释“ 123优秀”时,我的公式将给出“优秀”,尽管您的单词列表仅包含“优秀”。 See @barry's answer for a better solution that ingeniously uses a property of the LOOKUP function (If LOOKUP cannot find the lookup_value, it matches the largest value in lookup_vector that is less than or equal to lookup_value). 请参见@barry的答案,以获得更好的解决方案,该解决方案巧妙地使用了LOOKUP函数的属性(如果LOOKUP无法找到lookup_value,则它将匹配lookup_vector中小于或等于lookup_value的最大值)。

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

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