How to find a number in a string?
When i get a string :
mystring = "jkj kj j jjjk 4.235kkik"
i want
mynumber = 4.235
The number doesn't always stand on the same place
in the string.
Thanks.
Printable View
How to find a number in a string?
When i get a string :
mystring = "jkj kj j jjjk 4.235kkik"
i want
mynumber = 4.235
The number doesn't always stand on the same place
in the string.
Thanks.
Try this hack, I'm sure someone will post a better response.
Code:
Private Sub Command28_Click()
Dim mystring As String
Dim mynumber As Long
Dim x As Integer
Dim mytemp As Variant
mystring = "jkj kj j jjjk 4.235kkik"
For x = 1 To Len(mystring)
If (IsNumeric(Mid(mystring, x, 1)) Or Mid(mystring, x, 1) = ".") Then
mytemp = mytemp & Mid(mystring, x, 1)
End If
Next x
'May need some conversion to Long here ???
mynumber = mytemp
End Sub
Try
Code:mynumber = val(mystring)
Your answer isn't so bad Steven, i was going to post the same answer ;)
Afraid not harry. The val function does work to en extent, but if the string does not start with a Number then you will get a value of 0.
The way it works is that it looks through the string and sees if it is a number. Upon hitting the first non-numerical character it returns what it has found.
On second thoughts try this
Code:Dim thestr As String, thenum As Double
thestr = "abcd de 39.76f alw"
For x = 1 To Len(thestr)
If IsNumeric(Mid(thestr, x, 1)) Then thenum = Val(Mid(thestr, x)): Exit For
Next
Darn you beat me to it Iain ;) I remembered that just after I posted *Doh*