Extract numbers from alphanumeric strings

Maurizio

Well-known Member
Joined
Oct 15, 2002
Messages
691
Office Version
  1. 2007
Platform
  1. Windows
Hi all,

how can I extract (by VBA) this numbers:

149575
191368
191369

with prefix "DUD" and suffix "/nn" from alphanumeric strings with so DB:

DUD 149575/18
DUD 191368/17 Ton 39.00, DUD 191369/18 Ton 9.04


Tia. Maurizio
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
You didn't specify whether the number to be extracted is always 6 digits like your 3 examples.

Would this user-defined function be any use? This one allows for any number of digits between "DUD " and "/nn"
To implement ..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot below and copy across and down.


Code:
Function GetNums(s As String, num As Long) As Variant
  Static RX As Object, m As Object
  
  If RX Is Nothing Then
    Set RX = CreateObject("VBScript.RegExp")
    RX.Global = True
  End If
  RX.Pattern = "(DUD )(\d+)(?=\/\d\d)"
  GetNums = ""
  If num <= RX.Execute(s).Count Then GetNums = Val(RX.Execute(s)(num - 1).Submatches(1))
End Function

Excel Workbook
ABCD
1DUD 149575/18149575
2DUD 191368/17 Ton 39.00, DUD 191369/18 Ton 9.04191368191369
Sheet1



If the number of digits is always 6, the above will still work but an alternative would be
Code:
Function Get_Nums(s As String, num As Long) As Variant
  Dim i As Long, j As Long
  
  Get_Nums = ""
  For i = 1 To Len(s) - 12
    If Mid(s, i, 13) Like "DUD ######/##" Then
      j = j + 1
      If j = num Then
        Get_Nums = Val(Mid(s, i + 4, 6))
        Exit For
      End If
    End If
  Next i
End Function
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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