Extract Text Matching a Certain Sequence within a Cell

Aderonin

New Member
Joined
Feb 24, 2023
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I work for a company and we have type up orders for customers. The way the customers give the info can vary so I tend to use Excel for bigger lists. Sometimes, it's not nice and puts it all into a single cell. I don't know exactly where in the cell the item number or if the number is by itself is but the item number always is 5-6 digits long and starts and ends with a number. It will only have letters and numbers. Can you guys help with a quick script to get the info I need out of the cell?

Example: "PictureFrame5E645BlackBorder" and "TableTop 33ST59 StainlessSteel $59.00" and it should get just 5E645 and 33ST59 here.

Just looking to speed up the work. Thank you.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Welcome to the MrExcel board!

See if this user-defined function would work for you. Post back if you need instruction on how to implement it.

VBA Code:
Function ItemNo(s As String) As String
  With CreateObject("VBScript.RegExp")
    .Pattern = "(^|\D)(\d[0-9A-Za-z]{3,4}\d)(\D|$)"
    If .Test(s) Then ItemNo = .Execute(s)(0).SubMatches(1)
  End With
End Function

Aderonin.xlsm
AB
1TextItem Number
2PictureFrame5E645BlackBorder5E645
3TableTop 33ST59 StainlessSteel $59.0033ST59
4 
5Other 65T5 Red 
6Picture236TY67Frame5E645BlackBorder5E645
Sheet1
Cell Formulas
RangeFormula
B2:B6B2=ItemNo(A2)
 
Upvote 1
I was able to add it to a sheet to copy/paste into and it seems to have worked out well. I really appreciate it. It seems pretty simply if I knew VB at all. What would happen if there were multiple instances that fit the criteria? It's very, very unlikely but sometimes, manufacture part numbers could would be that way. Can this output all of them so I can then when I paste it back into the original, I can just choose the correct one? Maybe just in a format like Result1 Result2.
 
Upvote 0
Can this output all of them
Try this version

VBA Code:
Function ItemNo(s As String) As Variant
  Dim M As Object
  
  With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "(^|\D)(\d[0-9A-Za-z]{3,4}\d)(\D|$)"
    ItemNo = ""
    If .Test(s) Then
      For Each M In .Execute(s)
        ItemNo = ItemNo & " " & M.SubMatches(1)
      Next M
      ItemNo = Split(Mid(ItemNo, 2))
    End If
  End With
End Function

Aderonin.xlsm
ABCD
1TextItem Number
2PictureFrame5E645BlackBorder5E645
3TableTop 33ST59 StainlessSteel $59.0033ST59
4 
5Other 65T5 Red 
6Picture236TY67Frame5E645BlackBorder5E645
7Something12H25 and 895HG6 or 78654312H25895HG6786543
Sheet1
Cell Formulas
RangeFormula
B2:B6,B7:D7B2=ItemNo(A2)
 
Upvote 1
Fantastic. It looks like it threw the extra one of an example into the next column. That should work just fine. Thanks so much.
 
Upvote 0
It looks like it threw the extra one of an example into the next column.
If you would prefer them all in one column it is a minor change:

VBA Code:
Function ItemNo(s As String) As Variant
  Dim M As Object
  
  With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "(^|\D)(\d[0-9A-Za-z]{3,4}\d)(\D|$)"
    ItemNo = ""
    If .Test(s) Then
      For Each M In .Execute(s)
        ItemNo = ItemNo & ", " & M.SubMatches(1)
      Next M
      ItemNo = Mid(ItemNo, 3)
    End If
  End With
End Function

Aderonin.xlsm
AB
1TextItem Number
2PictureFrame5E645BlackBorder5E645
3TableTop 33ST59 StainlessSteel $59.0033ST59
4 
5Other 65T5 Red 
6Picture236TY67Frame5E645BlackBorder5E645
7Something12H25 and 895HG6 or 78654312H25, 895HG6, 786543
Sheet1
Cell Formulas
RangeFormula
B2:B7B2=ItemNo(A2)
 
Upvote 1
Solution

Forum statistics

Threads
1,215,069
Messages
6,122,956
Members
449,096
Latest member
Anshu121

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