Thread: Extract digits
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Claus Busch Claus Busch is offline
external usenet poster
 
Posts: 3,872
Default Extract digits

Hi Robert,

Am Wed, 20 Nov 2019 10:48:20 -0800 (PST) schrieb RG III:

I have a set of strings that contain digits at the right end, such as:

s1 = "abcd1234"
s2 = "g10"
s3 = "yyy19123"

The data will always contain alpha chars on the left, and digits on the right.


try:

For i = 1 To Len(s2)
If Asc(Mid(s2, i, 1)) 47 And Asc(Mid(s2, i, 1)) < 58 Then
myNumber = CInt(Mid(s2, i))
Exit Sub
End If
Next

or:

Sub Test2()
Dim re As Object, Match As Object
Dim ptrn As String

s1 = "abcd1234"
s2 = "g10"
s3 = "yyy19123"

ptrn = "[\d]{1,}"
Set re = CreateObject("vbscript.Regexp")
re.pattern = ptrn
re.IgnoreCase = False
re.Global = True
Set Match = re.Execute(s3)
MsgBox Match(0).Value
End Sub


Regards
Claus B.
--
Windows10
Office 2016