Extracting number between characters

stupideye

New Member
Joined
Aug 6, 2019
Messages
14
Hello,

I'm looking to extract product numbers to do some analysis. I get them in a format that is this:
/product/12345/product-description

The part I need is the "12345" (the product number). Sometimes the products are displayed like:
/product/12345?product-description
or
/product/12345

The product number is not always 5 characters either.

I've gotten close but I can't figure this one out! Any assistance? Thanks in advance.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Does this UDF do what you're looking for?

Code:
Function EXTRACTNUM(s As String) As String
With CreateObject("VBScript.RegExp")
    .Pattern = "/(\d+)\w?"
    EXTRACTNUM = .Execute(s)(0).submatches(0)
End With
End Function
 
Upvote 0
Welcome to the Board!


Book1
AB
1/product/12345/product-description12345
2/product/12345?product-description12345
3/product/1234512345
4abcd1apr/xyz43556
5abc/12345/xyz/9999999999
6/product/1234567?description1234567
Sheet3
Cell Formulas
RangeFormula
B1=AGGREGATE(14,6,MID(A1,ROW(INDIRECT("1:"&LEN(A1))),{1,2,3,4,5,6,7,8,9})+0,1)


Here's one option. It has some limitations (see row 4). I'll see if I can devise something better, or you can try lrobbo's UDF. Let us know how these work.
 
Upvote 0
Welcome to the Board!

AB
1/product/12345/product-description12345
2/product/12345?product-description12345
3/product/1234512345
4abcd1apr/xyz43556
5abc/12345/xyz/9999999999
6/product/1234567?description1234567

<colgroup><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet3

Worksheet Formulas
CellFormula
B1=AGGREGATE(14,6,MID(A1,ROW(INDIRECT("1:"&LEN(A1))),{1,2,3,4,5,6,7,8,9})+0,1)

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>



Here's one option. It has some limitations (see row 4). I'll see if I can devise something better, or you can try lrobbo's UDF. Let us know how these work.

Does this UDF do what you're looking for?

Code:
Function EXTRACTNUM(s As String) As String
With CreateObject("VBScript.RegExp")
    .Pattern = "/(\d+)\w?"
    EXTRACTNUM = .Execute(s)(0).submatches(0)
End With
End Function
A huge thank you to both of you for responding so quickly to my question. Like you mentioned Eric, the formula didn't quite work every time. The UDF ended up working perfectly though. Thank you again, this saved a huge amount of time.
 
Upvote 0

Forum statistics

Threads
1,215,261
Messages
6,123,931
Members
449,134
Latest member
NickWBA

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