Excel / LibreOffice Calc Partial Match in Reverse

僤鯓⒐⒋嵵緔 提交于 2019-12-08 05:16:16

问题


I would like to use a lookup table to choose a tag for each row according to its description column. Descriptions contain keywords that map to tags. Therefore, I need to partially match against the keyword list as below:

  A                     B          C        D        E
1 Description           Tag                 Keyword  Tag
2 lorem KEYA ipsum                          KEYA     Tag A
3 dolor sit KEYC amet                       KEYB     Tag B
4 KEYB consectetur                          KEYC     Tag C
5 adipiscing elit KEYA                      KEYD     Tag D
6 sed do KEYB eiusmod                       

I would like to fill cells in column B with values from the lookup table in D2:E5. I could use something like

=VLOOKUP("."&$A2&".", $D$3:$E$6, 2, 0)

but it won't work because I try to match full text against partial texts.

=VLOOKUP($A2, "."&$D$3:$E$6&".", 2, 0)

does not work either. Apparently regular expressions are only supported in the search criterion. And I don't like to write something like

=IF(ISNUMBER(SEARCH("KEYA",$A2)), "Tag A", 
 IF(ISNUMBER(SEARCH("KEYB",$A2)), "Tag B", 
 IF(ISNUMBER(SEARCH("KEYC",$A2)), "Tag C", 
 IF(ISNUMBER(SEARCH("KEYD",$A2)), "Tag D", 
 ""))))

Do you have any suggestions?


回答1:


Try putting this array formula in B2 using Ctrl-Shift-Enter and copying it down:-

=INDEX(E$2:E$5,MAX((NOT(ISERROR(FIND(D$2:D$5,A2)))*ROW(D$2:$D$5))-1))

It uses FIND to try and match each of the keys in turn anywhere in A2. If one matches, it works out the corresponding row number in D$2:D$5. It takes the maximum matching row, and uses INDEX to find the corresponding element in E$2:E$5. If there is no matching key, it gives a #VALUE! error.

The matching with FIND is case-sensitive: if you didn't want it to be case-sensitive, you would need to use SEARCH instead of FIND.

If you could ever have a case with two or more matching keys, e.g.

lorem KEYA KEYB ipsum

you would get the last matching one in the list i.e. TAG B.




回答2:


Parse the KEYx text from column A to use as the lookup_value in your VLOOKUP.

    

The formula in B2 is =IFERROR(VLOOKUP(MID(A2,FIND("KEY",A2),4),$C$2:$D$5,2,FALSE),"").

Addendum: For OpenOffice/LibreOffice:

    

The formula for B2 would be =IF(ISERROR(VLOOKUP(MID(A2;FIND("KEY";A2);4);$C$2:$D$5;2;0));"";VLOOKUP(MID(A2;FIND("KEY";A2);4);$C$2:$D$5;2;0)). Fill down as necessary.



来源:https://stackoverflow.com/questions/26340878/excel-libreoffice-calc-partial-match-in-reverse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!