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
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.
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
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