Extracting & isolating multiple numerical values from a string

awebmanager

New Member
Joined
Sep 27, 2013
Messages
2
I have a list of 300 web URLs in a worksheet. Here are some typical examples:

/resources?keys=&tid_3=All&tid_5[]=50&tid_7=277

<colgroup><col width="487"></colgroup><tbody>
</tbody>

/resources?keys=lasagna&tid_3=35&tid_5[]=51&tid_7=277

<colgroup><col width="828"></colgroup><tbody>
</tbody>

/resources?keys=&tid_3=39&tid_5[]=50&tid_5[]=51&tid_5[]=54&tid_5[]=52&tid_5[]=53&tid_5[]=329&tid_7=277

<colgroup><col width="828"></colgroup><tbody>
</tbody>

The numbers after the = signs all refer to unique category terms which I have stored in a second worksheet which can be used as a lookup table. That worksheet looks like this:

Term ID
Term name
50
glass
277
england
39
fruit

<tbody>
</tbody>

I've got the lookup table part working OK, so that if in worksheet 1 I simply enter the number 50 in cell A1, Excel puts "glass" in cell A2, or if I had 277 in cell G1 it would put "england" in cell G2, and so on.

What I can't work out is a formula to automatically extract the numbers from the URLs and isolate those without all the other stuff. So for example in the URL /resources?keys=&tid_3=All&tid_5[]=50&tid_7=277 all I want are the numbers 50 and 277.

I don't mind if each of those numbers has to appear in a different column, although ideally I'd like the extracted data to show the multiple numbers in one cell e.g. "50, 277" so that the lookup function then returns "glass, england" in one cell adjacent to the URL cell.

This is probably all a long shot but I'd be grateful for any help.

Thanks :)
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hello and welcome to the Board

Is a VBA solution suitable for you?
 
Upvote 0
Try this UDF. Press Alt + F11, select Insert/Module and paste this code into an empty Module. Then go back to the sheet and select ExtrNum function from User Defined Functions.

Code:
Function ExtrNum(c As String) As String
    With CreateObject("VBScript.RegExp")
        c = c + "&"
        .Global = True
        .Pattern = "=(\d+)&|.+?"
        If .Test(c) Then ExtrNum = Application.Trim(.Replace(c, "$1 "))
    End With
End Function
 
Upvote 0
Hey Istvan, thanks so much for your help! That did the trick :pray:

Thanks also to Worf for the welcome. Great board! :)
 
Upvote 0
Try this UDF. Press Alt + F11, select Insert/Module and paste this code into an empty Module. Then go back to the sheet and select ExtrNum function from User Defined Functions.

Code:
Function ExtrNum(c As String) As String
    With CreateObject("VBScript.RegExp")
        c = c + "&"
        .Global = True
        .Pattern = "=(\d+)&|.+?"
        If .Test(c) Then ExtrNum = Application.Trim(.Replace(c, "$1 "))
    End With
End Function
For those who might be interested, here is a non-RegExp version of István's UDF...

Code:
Function ExtrNum(S As String) As String
  Dim X As Long, Nums() As String
  Nums = Split(S, "=")
  For X = 0 To UBound(Nums)
    If Nums(X) Like "#*" Then
      Nums(X) = Val(Nums(X))
    Else
      Nums(X) = ""
    End If
  Next
  ExtrNum = WorksheetFunction.Trim(Join(Nums))
End Function
 
Upvote 0

Forum statistics

Threads
1,215,835
Messages
6,127,167
Members
449,368
Latest member
JayHo

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