简体   繁体   English

在 VBA 中的字符之前获取特定字符串

[英]Getting specific string before a character in VBA

I wanted the leftmost string from a specific string.我想要特定字符串中最左边的字符串。

Ex.前任。 sds_djfh_jdj I want sds for this I used the sds_djfh_jdj我想要 sds 我用

LEFT('string',FIND("_",'string')-1)

but in my case another character may appear before the _ character但在我的情况下,另一个字符可能会出现在_字符之前

Ex.前任。 sds#_djfh_jdj (I want just sds but the formula that I use will give me sds#) sds#_djfh_jdj (我只想要 sds,但我使用的公式会给我 sds#)

Can someone tell me how do I solve the problem.有人可以告诉我如何解决这个问题。

As commented, you can include some OR logic within FIND .如评论所述,您可以在FIND中包含一些OR逻辑。 For all your cases it appears that the following would do just fine:对于您的所有情况,以下内容似乎就可以了:

=LEFT(A1,MIN(IFERROR(FIND({"#";"_"},A1)-1,LEN(A1))))

Note: It's an array formula and need to be confirmed through Ctrl Shift Enter注:为数组公式,需通过Ctrl Shift Enter确认

在此处输入图像描述

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

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