简体   繁体   中英

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

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#)

Can someone tell me how do I solve the problem.

As commented, you can include some OR logic within FIND . 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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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