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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
38,150
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
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

LGFN1

Board Regular
Joined
Jun 25, 2014
Messages
63
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

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
21,595
Office Version
  1. 365
  2. 2019
  3. 2013
  4. 2007
Platform
  1. Windows
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

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
38,150
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
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

LGFN1

Board Regular
Joined
Jun 25, 2014
Messages
63
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

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
21,595
Office Version
  1. 365
  2. 2019
  3. 2013
  4. 2007
Platform
  1. Windows
Okay, then a few more examples would help....and just to confirm it is ALWAYS MFT85E98 to be extracted ??
 
Upvote 0

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
21,595
Office Version
  1. 365
  2. 2019
  3. 2013
  4. 2007
Platform
  1. Windows
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

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
38,150
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
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

LGFN1

Board Regular
Joined
Jun 25, 2014
Messages
63
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,191,043
Messages
5,984,321
Members
439,882
Latest member
gerdc

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
Top