简体   繁体   English

Excel - 在字符串中查找多个值并返回第一个

[英]Excel - lookup multiple values in a string and return first one

I need a function to look up organizational abbreviations in a text and return the first one thas shows up.我需要一个函数来查找文本中的组织缩写并返回第一个出现的缩写。 I tried to solve that with a nested if clause but it has a logical error.我试图用嵌套的 if 子句解决这个问题,但它有一个逻辑错误。

=IF(ISNUMBER(SEARCH("BZC";I5)); "Finanz"; IF(ISNUMBER(SEARCH("AZC" /1";I5));"IT";""))

It looks up BZC and AZC as desired and return the organization name.它根据需要查找 BZC 和 AZC 并返回组织名称。 However, it does no return the first match in a string.但是,它不会返回字符串中的第一个匹配项。 Since BZC is the first lookup it will always be returned if it is in the string, eventhoug it might not be the first org abbreviation.由于 BZC 是第一个查找,如果它在字符串中,它将始终被返回,即使它可能不是第一个组织缩写。

What functionality of excel can be used to solve this issue? excel 的哪些功能可以用来解决这个问题? I basically need an array of variables that a function should do a look up and return the first one that is found.我基本上需要一个变量数组,函数应该查找并返回找到的第一个变量。

Edit:编辑:

I tried to implement the formula form Justyna MK.我试图实现 Justyna MK 的公式。 Besides the fact, that I still need to figure out the meaning of the formula (iferror, mid, small) It returns #N/A in my example.除此之外,我还需要弄清楚公式 (iferror, mid, small) 的含义,它在我的示例中返回 #N/A。 #不适用 Is there a certain reason for that?这有一定的原因吗?

I hope I understood your request correctly.希望我正确理解了您的要求。 Here's an Array formula for you to try (enter using Ctrl + Shift + Enter):这是一个数组公式供您尝试(使用 Ctrl + Shift + Enter 输入):

=CHOOSE(MATCH(MID(A1,SMALL(IFERROR(SEARCH({"BZC","AZD","xxx"},A1),""),1),3),{"BZC","AZD","xxx"},0),"Finanz","IT","Other")

You'd probably need to change , to ;您可能需要将,更改为; in order to match your regional settings.以匹配您的区域设置。

You can easily expand the list of search items by modifying the contents of curly brackets { } and also by expanding the MATCH results at the very end of the formula.您可以通过修改大括号{ }的内容以及扩展公式末尾的MATCH结果来轻松扩展搜索项列表。

Here's some sample result:这是一些示例结果:

在此处输入图像描述

Edit: here's an adjusted solution that ignores the length of your code (the previous solution was assuming that the code is always 3-characters long).编辑:这是一个忽略代码长度的调整解决方案(以前的解决方案假设代码总是 3 个字符长)。 This time it's not an array formula so you can enter it as it is.这次不是数组公式,直接输入即可。 Also, I suspect that you should not change , to ;另外,我怀疑您应该将,更改为; inside the curly brackets (it would modify the formula from column to row delimiter and thus it will stop working).在大括号内(它会将公式从列修改为行分隔符,因此它将停止工作)。 The remaining , can be transformed to ;剩下的,可以转化为; , if that makes sense. ,如果这是有道理的。

=CHOOSE(MATCH(TRIM(LEFT(SUBSTITUTE(RIGHT(A1,LEN(A1)-SUMPRODUCT(SMALL(IFERROR(SEARCH({"BZC","AZD","XYZX/1","NP-HSD"},A1),""),1))+1)," ",REPT(" ",255)),255)),{"BZC","AZD","XYZX/1","NP-HSD"},0),"Finanz","IT","Other1","Other2")

The result:结果:

在此处输入图像描述

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

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