Extract text

LGFN1

Board Regular
Joined
Jun 25, 2014
Messages
63
I'm trying to extract a string from a cell.

The said cell may be in either one of the following patterns (always, of course, with different values):
Code:
GH MFT85E98/12
GH-MFT85E98-12
GH/MFT85E98/12
GH MFT85E98 12
GH MFT85E98
GH-MFT85E98
and so on.

In other words, it's unpredictable how the value is patterned. What I'm trying to extract is all text between the first non-alphanumeric character and the last non-alphanumeric character (if there is one). So in all the above cases my result should be MFT85E98.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I'm trying to extract a string from a cell.

The said cell may be in either one of the following patterns (always, of course, with different values):
Code:
GH MFT85E98/12
GH-MFT85E98-12
GH/MFT85E98/12
GH MFT85E98 12
GH MFT85E98
GH-MFT85E98
and so on.

In other words, it's unpredictable how the value is patterned. What I'm trying to extract is all text between the first non-alphanumeric character and the last non-alphanumeric character (if there is one). So in all the above cases my result should be MFT85E98.
Are spaces, forward slashes and dashes the only non-alphanumeric characters that could be in the text?
 
Upvote 0
Things can change with time, but as of now I think this is all I got. Either way, I understand that once I get the hang of the formula, I can manipulate it to match my needs. Correct?
 
Upvote 0
Would this not do as required



Excel 2010
AB
1GH MFT85E98/12MFT85E98
2GH-MFT85E98-12MFT85E98
3GH/MFT85E98/12MFT85E98
4GH MFT85E98 12MFT85E98
5GH MFT85E98MFT85E98
6GH-MFT85E98MFT85E98
Sheet1
Cell Formulas
RangeFormula
B1=MID(A1,4,8)
 
Upvote 0
Michael raises a good point... is your data as regular as you show, namely, two characters followed by a non-alphanumeric character followed by the eight characters you want (what follows that would be immaterial)?
 
Upvote 0
Oops, my mistake, I should've been more clear. Sorry! It's not always two characters in the beginning, it's not always eight characters in middle, and it's not always two characters at the end. All this varies greatly from one value to another.
 
Upvote 0
Okay, then a few more examples would help....and just to confirm it is ALWAYS MFT85E98 to be extracted ??
 
Upvote 0
I'm out on the road, so won't have Excel for a while, but if you need to extract 3 letters followed by 2 digits, 1 letter 2 digits...try this
Rick will probably provide a non Regex solution that will be quicker but it will be up to you

Code:
Function PN(s As String) As String
With CreateObject("VBScript.RegExp")
  .Pattern = "([A-z]{3}[0-9]{2}[A-z][0-9]{2})"
  If .Test(s) Then PN = .Execute(s)(0).SubMatches(0)
End With
End Function
 
Upvote 0
Oops, my mistake, I should've been more clear. Sorry! It's not always two characters in the beginning, it's not always eight characters in middle, and it's not always two characters at the end. All this varies greatly from one value to another.
Okay, give this formula a try...

=TRIM(MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"/"," "),"-"," ")," ",REPT(" ",99)),100,99))
 
Upvote 0
Thanks, there's just one problem, this extracts from the first non-alphanumeric character to the next one. I need from the first to the last. Is it possible?
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,447
Members
448,966
Latest member
DannyC96

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