Hello tparvaiz,
You can extract the number using regular expressions in a user-defined function. Put a formula in a worksheet cell like:
=GetNumber(A2,1) 'returns the first number (122) found in cell A2. If none exists, returns an empty string.
=GetNumber(A2,2) 'returns the second number (50) found in cell A2
To install a function in a regular module sheet:
1) ALT + F11 to open the VBA Editor
2) Use the Insert...Module menu item to create a blank module sheet
3) Paste the suggested code in this module sheet
4) ALT + F11 to return to the spreadsheet
Optional: to add descriptive text (appears at bottom of Function Wizard):
5) ALT + F8 to open the macro window
6) Type in the name of your function in the "Macro name" field at the top
7) Press the "Options" button
8) Enter some descriptive text telling what the function does in the "Description" field
9) Click the "OK" button
If the above procedure doesn't work, then you need to change your macro security setting. To do so, open the Tools...Macro...Security menu item. Choose Medium, then click OK.
Regards,
Brad
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
|
Function GetNumber(sText As String, iOccurrence As Integer) As Variant
Dim RgExp As Object, oMatches As Object
Dim v As Variant
Set RgExp = CreateObject("VBScript.RegExp")
GetNumber = ""
With RgExp
.Pattern = "\d+"
.Global = True
Set oMatches = .Execute(sText)
If oMatches.Count >= iOccurrence Then
v = oMatches(iOccurrence - 1)
If Not IsError(v) Then
GetNumber = Val(v)
End If
End If
End With
Set RgExp = Nothing
End Function
|