公式或 VBA - 查找字符串及其后的特定数量的字符

Formulas or VBA - Find String and certain number of characters after it

我一直在用

=if(isnumber(search("MyString",A1,1)),"MyString","Not Found")

查看 "MyString" 是否存在于具有长描述的单元格中。通常 "MyString" 后跟一个数字(会变化),例如 "MyString#".

但是,"MyString" 和它的编号并不总是存在于描述的同一位置:

A2 = "MyString# is great today."
A3 = "Today is a great day for MyString# to be in Excel."

有什么方法可以搜索 MyString,然后打印它和它后面的数字吗? (我可能想多了或者忽略了一些简单的事情!)

谢谢!!

=IFERROR(MID(A1,SEARCH("MyString",A1),LEN("MyString")+1),"Not Found")

如果只想显示数字,请执行以下操作:

=IFERROR(MID(A1,SEARCH("MyString",A1)+LEN("MyString"),1),"Not Found")

注意:我假设"MyString"后面的数字总是1位。

如果MyString#后面的数据一直为空,那么无论是多少位数,都可以通过下面的公式得到。

=IFERROR(MID(A1,SEARCH("MyString",A1),SEARCH(" ",A1,SEARCH("MyString",A1))-SEARCH("MyString",A1)),"Not Found")

仅限数字:

=IFERROR(MID(A1,SEARCH("MyString",A1)+LEN("MyString"),SEARCH(" ",A1,SEARCH("MyString",A1))-SEARCH("MyString",A1)-LEN("MyString")),"Not Found")