使用VBA或Excel公式在Excel单元格中search部分string

我有一个Excel工作表,具有列模板和模板名称,我必须在模板列中使用部分string填充模板名称列:

Template Template Name This is String String line contains data This is Int Int line contain data int: in the String data string: in the int data Int: is empty or no string 

我尝试了下面的公式,但是给出了不同的结果:

  =IF(ISNUMBER(SEARCH("string",A2)),"string",IF(ISNUMBER(SEARCH("int",A2)),"int",IF(ISNUMBER(SEARCH("int:",A2)),"int",IF(ISNUMBER(SEARCH("string:",A2)),"string")))) 

B列公式给出的输出:

  Template Template Name This is String String String line contains data string This is Int int Int line contain data int int: in the String data string #it must be 'int' string: in the int data int #it must be 'string' Int: is empty or no string string #it must be 'int' 

请让我知道需要更改以获得所需的输出。

重新排列search顺序:如果您首先search较长的string(使用':'),然后search没有使用':'的string,结果应该是正确的。

 =IF(ISNUMBER(SEARCH("string:",A2)),"string",IF(ISNUMBER(SEARCH("int:",A2)),"int",IF(ISNUMBER(SEARCH("int",A2)),"int",IF(ISNUMBER(SEARCH("string",A2)),"string")))) 

编辑 :使代码适合在电子表格单元格中直接input(我以前曾经想过一个VBAmacros)

如果你想要在“string”和“整数”之间的第一个出现,然后使用

 =IF(IFERROR(SEARCH("int",RC1),10000)>IFERROR(SEARCH("string",RC1),10000),"String", "Int")" 

如果你处理string中既不是“string”又不是“int”的情况,那么只要检查其中一个string