Find string between two spaces

griffj

Board Regular
Joined
May 29, 2015
Messages
56
Hi,

I am trying to find a sub-string between two spaces in a string.

Example String;
2PB IND3A 1MLGSTWIEL ANO

I would like to extract 1MLGSTWIEL

So far I have this formula...

=IF(ISERR(FIND("ML",A1)),"",RIGHT(LEFT(A1,FIND("ML",A1)+1),LEN(LEFT(A1,FIND("ML",A1)+1))-FIND("~", SUBSTITUTE(LEFT(A1,FIND("ML",A1)+1)," ","~",LEN(LEFT(A1,FIND("ML",A1)+1))-LEN(SUBSTITUTE(LEFT(A1,FIND("ML",[@Description])+1)," ",""))))))

which finds the 'ML' part of the string and then returns everything up to the previous space, but past the 'L' I can't seem to get it to work.

The positions will not always be the same.

Any suggestions would be great.
 
If you are looking for the part between the 2nd and 3rd space:
=TRIM(MID(SUBSTITUTE(A1," ",REPT(" ",100)),200,100))
 
Upvote 0
That works a treat...but, it is not always the 2nd and 3rd space. This is a variable that could be different on each line.
 
Upvote 0
Yes, I want to extract 1MLGSTWIEL from 2PB IND3A 1MLGSTWIEL, but the position might change, the string/sub-string length might change.

Then try this:

=TRIM(MID(SUBSTITUTE(A1," ", REPT(" ",200)),FIND("ML",SUBSTITUTE(A1," ", REPT(" ",200)))-200,400))
 
Upvote 0
Then try this:

=TRIM(MID(SUBSTITUTE(A1," ", REPT(" ",200)),FIND("ML",SUBSTITUTE(A1," ", REPT(" ",200)))-200,400))

That works, perfect, and is a little more refined than what I was just about to post as my answer!

=MID(A1,FIND(RIGHT(LEFT(A1,FIND("ML",A1)+1), LEN(LEFT(A1,FIND("ML",A1)+1))-FIND("~", SUBSTITUTE(LEFT(A1,FIND("ML",A1)+1)," ","~",LEN(LEFT(A1,FIND("ML",A1)+1))-LEN(SUBSTITUTE(LEFT(A1,FIND("ML",A1)+1)," ",""))))),A1),FIND(" ",A1,FIND("ML",A1))-FIND(RIGHT(LEFT(A1,FIND("ML",A1)+1), LEN(LEFT(A1,FIND("ML",A1)+1))-FIND("~", SUBSTITUTE(LEFT(A1,FIND("ML",A1)+1)," ","~",LEN(LEFT(A1,FIND("ML",A1)+1))-LEN(SUBSTITUTE(LEFT(A1,FIND("ML",A1)+1)," ",""))))),A1))

Simple when you know how! I will go an look at the REPT function.
 
Upvote 0
Then try this:

=TRIM(MID(SUBSTITUTE(A1," ", REPT(" ",200)),FIND("ML",SUBSTITUTE(A1," ", REPT(" ",200)))-200,400))

I see now that this formula does not work if the substring containing "ML" is in start position, so try this corrected version:

=TRIM(MID(SUBSTITUTE(" "&A1," ", REPT(" ",200)),FIND("ML",SUBSTITUTE(" "&A1," ", REPT(" ",200)))-200,400))
 
Last edited:
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top