If you have a text string with numbers within (let say you are interested only in those numbers, it's just an example), and you want to pick up those numbers from the text, you need a function to do it.
Option Explicit
'source code: https://code-for-vb.blogspot.com/2020/02/extracting-from-string-vba.html
'please to mention
Function NumbersFromText() As String
Dim i As Integer
Const a = "ancduirg123456ggt789uyq"
Dim b As String
Dim c As String
Dim d As String
Dim e As String
b = a
c = a
'split constant a in characters and find the ASCII code for each:
For i = 1 To Len(a)
'just for a better understanding:
Debug.Print Mid(a, i, 1), Asc(Mid(a, i, 1))
'the Debug window will contain this text:
' a 97
' n 110
' c 99
' d 100
' u 117
' i 105
' r 114
' g 103
' 1 49
' 2 50
' 3 51
' 4 52
' 5 53
' 6 54
' g 103
' g 103
' t 116
' 7 55
' 8 56
' 9 57
' u 117
' y 121
' q 113
'replace every non-digit character with zero / with zero length string:
If Asc(Mid(a, i, 1)) < 49 Or Asc(Mid(a, i, 1)) > 57 Then
b = Replace(b, Mid(a, i, 1), "")
c = Replace(c, Mid(a, i, 1), 0)
End If
Next i
'now, b is 123456789
c = Trim(Str(Val(c)))
'now, c is 123456000789000
'reverse the string c in d
For i = Len(c) To 1 Step -1
d = d & Mid(c, i, 1)
Next i
d = Trim(Str(Val(d)))
'now, d is 987000654321
'reverse the string d in e
For i = Len(d) To 1 Step -1
e = e & Mid(d, i, 1)
Next i
'c could be reused for saving memory:
c = Replace(e, "0", "_")
Debug.Print "Substract only numbers from string '" & a & "' : " & b
Debug.Print "Keep only the numbers interval from string '" & a & "' : " & e & " or " & c
'this is what Immediate window will show:
'Substract only numbers from string 'ancduirg123456ggt789uyq' : 123456789
'Keep only the numbers interval from string 'ancduirg123456ggt789uyq' : 123456000789 or 123456___789
'remain to choose:
NumbersFromText = b
'or
NumbersFromText = c
'or
NumbersFromText = e
End Function
Function NumbersFromText(a As String) As String
...
...
...
End Function
Niciun comentariu:
Trimiteți un comentariu