Return numbers

  • Thread starter Thread starter gr
  • Start date Start date
G

gr

Hi I'm get strings like the following:
str1 = "kefg12def"
str2 = "k4hil3"
str3= "j98998"
.....
So I have numbers inside a string. is there a function to
pull out the numbers??

From str1 get 12
From str2 = 43
From str3= 98998


Thank you!
 
You'll have to code a specific function to do that. First to extract the
numeric characters and then to convert them into numbers.

Two functions might be especially helpful,
isNumeric and Val

The Val function stops reading the string at the first character it can't
recognize as part of a number. Symbols and characters that are often
considered parts of numeric values, such as dollar signs and commas, are not
recognized. However, the function recognizes the radix prefixes &O (for
octal) and &H (for hexadecimal). Blanks, tabs, and linefeed characters are
stripped from the argument.

The following returns the value 1615198:

Val(" 1615 198th Street N.E.")
-- Kailash KalyaniMEA Developer Support CenterITWorx on behalf Microsoft
 
Hi I'm get strings like the following:
str1 = "kefg12def"
str2 = "k4hil3"
str3= "j98998"
....
So I have numbers inside a string. is there a function to
pull out the numbers??

From str1 get 12
From str2 = 43
From str3= 98998

Well, here's some untested air code:

Public Function ExtractNum(strIn As String) As String
Dim iPos As Integer
ExtractNum = ""
For iPos = 1 to Len(strIn)
If IsNumeric(Mid(strIn, iPos, 1)) Then
ExtractNum = ExtractNum & Mid(strIn, iPos, 1)
End If
Next iPos
End Function
 
Hello,
Well I code this function. Your air code also works and it
more simplified than mine =)

Function GetNum(strName As String) As Double
Dim strNo As String
Dim strTemp As String
Dim strCut As String
Dim inti As Integer
strCut = strName
inti = 1
For inti = 1 To Len(strName)
strTemp = Left$(strCut, 1)
strCut = Right$(strCut, Len(strName) - inti)
If strTemp = "0" Then
strNo = strNo & strTemp
ElseIf VBA.Val(strTemp) Then
strNo = strNo & VBA.Val(strTemp)
End If
Next inti
GetNum = CDbl(strNo)
End Function
 
Back
Top