Extracting Specific Text from a Cell

ijhoeq

Board Regular
Joined
Jun 20, 2018
Messages
61
Hello, I'm having an issue getting my data sorted out here. I would like to find an 'identifier' from text in a cell and put the identifier in the next column. The identifier is always formatted as two letters-three numbers (XX-###; see the example data). If there is a formula for this that is preferred but using VBA is also fine with me.

Please let me know if you need more info! I appreciate any help!

TextIdentifier
Blah Blah - T2 Something (AB-123)AB-123
Temperature one -22 - TX (XY-000)XY-000
Sensor (thermo-1) 23 (IJ-876) - 15IJ-876
Thermocouple X1 1-1/2, 22-TX (AB-123; PN123x)AB-123
T2 (PNx123,AB-123,Hs1) Temp 4AB-123
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
You could try:

Excel Formula:
=TRANSPOSE(FILTERXML("<x><y>"&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2;";";" ");",";" ");"(";"");")";"");" ";"</y><y>")&"</y></x>";"//y[translate(.,'1234567890','')!=. and string-length()=6 and contains(., '-')]"))
 
Upvote 0
Or with VBA (which I prefer)

VBA Code:
Function jec(cell As String) As Variant
 With CreateObject("VBScript.RegExp")
   .Global = True
   .Pattern = "\D{2}-\d{3}"
   jec = .Execute(cell)(0)
 End With
End Function

Use as:

Excel Formula:
=jec(A2)
 
Upvote 0
Thanks for the reply!

However, for the first proposed solution, I get an error stating "There is a problem with this formula."

For the VBA solution, I get a #NAME? error.
 
Upvote 0
What version of Excel are you using?

I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
Microsoft Office Professional Plus 2019 (excel version 1808 if that helps)

Thanks for the suggestion I'll have to update that.
 
Upvote 0
Ok, the problem is that jec has posted the formula using semi-colons as separators, whilst you probably use commas.
 
Upvote 0
Thank you Fluff and JEC. I updated the formula for my version and it is working.

Excel Formula:
=TRANSPOSE(FILTERXML("<x><y>"&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,";"," "),","," "),"(",""),")","")," ","</y><y>")&"</y></x>","//y[translate(.,'1234567890','')!=. and string-length()=6 and contains(., '-')]"))
 
Upvote 0
You're welcome!
For the VBA solution, you need to paste the code behind a standard module first (right click, insert module).
 
Upvote 0
Any idea why that formula might not work for Microsoft Office Professional Plus 2013?
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,822
Members
449,096
Latest member
Erald

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