Excel formula to get string between 2 characters

核能气质少年 提交于 2019-12-08 02:06:18

问题


I'm trying to dynamically extract the string between 2 characters in Excel using a formula (no text to columns). The sample data is:

US - Blue Widgets - Net
UK - Green - Grass
UAE - Red - Apples

* Note that the data doesn't have fixed length

I tried using a formula, but I think I'm missing something because that also returns the string after the last -.

Formula:

=TRIM(LEFT(SUBSTITUTE(MID(A2,FIND("|",SUBSTITUTE(A2,"-","|",1))+1,LEN(A2)),"_",REPT(" ",LEN(A2))),LEN(A2)))

what this returns is:

Blue Widgets - Net
Green - Grass
Red - Apples

here's what I'd like it to return:

Blue Widgets
Green
Red

回答1:


Try this:

=TRIM(MID(A2, 6, FIND("-",A2,6) - FIND("-",A2) - 2))

If hard coded 6 is not ok, you can replace it with FIND("-", A2) + 1.




回答2:


You can get help from the following link, its pretty useful,,

http://www.mrexcel.com/forum/excel-questions/444266-extract-string-between-two-characters.html




回答3:


I used this and worked.

=IF(ISERROR(SUBSTITUTE(TRIM(LEFT(SUBSTITUTE(MID(A3,FIND("|",SUBSTITUTE(A3,"-","|",1))+1,LEN(A3)),"-",REPT(" ",LEN(A3))),LEN(A3)))," ","")),"",SUBSTITUTE(TRIM(LEFT(SUBSTITUTE(MID(A3,FIND("|",SUBSTITUTE(A3,"-","|",1))+1,LEN(A3)),"-",REPT(" ",LEN(A3))),LEN(A3)))," ",""))

you will get:

Blue Widgets
Green
Red

is you want to get the last part, I mean the string after the second "-" use this

=IF(ISERROR(SUBSTITUTE(TRIM(LEFT(SUBSTITUTE(MID(A3,FIND("|",SUBSTITUTE(A3,"-","|",2))+1,LEN(A3)),"-",REPT(" ",LEN(A3))),LEN(A3)))," ","")),"",SUBSTITUTE(TRIM(LEFT(SUBSTITUTE(MID(A3,FIND("|",SUBSTITUTE(A3,"-","|",2))+1,LEN(A3)),"-",REPT(" ",LEN(A3))),LEN(A3)))," ",""))

you will get:

Net
Grass
Apples

Best regads.



来源:https://stackoverflow.com/questions/24871603/excel-formula-to-get-string-between-2-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!