Digita
07-25-2007, 12:17 AM
Hi everyone,
Just a curious question. I have a UDF to extract numbers (digits 0-9) from a string.
Function XtractNumbers(ByVal Str As String) As Long
Dim i As Long, k As Variant, j As Variant
For i = 0 To Len(Str)
k = Mid(Str, i + 1, 1)
If Not k >= 0 Or k <= 9 Then
j = j + k
End If
Next
XtractNumbers = j
End Function
The above UDF returns #Value! when the cell contains "asdfds 17/8 15/05/2007"
I tried another tactic by writing a sub to do the same thing.
Sub test()
Dim i As Integer, k As Variant, j As Variant
mystring = "asdfds 17/8 15/05/2007"
For i = 0 To Len(mystring)
k = Mid(mystring, i + 1, 1)
If Not k >= 0 Or k <= 9 Then
j = j + k
End If
Next
MsgBox j
End Sub
The sub provides the correct result "17815052007" in the msgbox. Why does the UDF not work?
Thanks & regards
KP
Just a curious question. I have a UDF to extract numbers (digits 0-9) from a string.
Function XtractNumbers(ByVal Str As String) As Long
Dim i As Long, k As Variant, j As Variant
For i = 0 To Len(Str)
k = Mid(Str, i + 1, 1)
If Not k >= 0 Or k <= 9 Then
j = j + k
End If
Next
XtractNumbers = j
End Function
The above UDF returns #Value! when the cell contains "asdfds 17/8 15/05/2007"
I tried another tactic by writing a sub to do the same thing.
Sub test()
Dim i As Integer, k As Variant, j As Variant
mystring = "asdfds 17/8 15/05/2007"
For i = 0 To Len(mystring)
k = Mid(mystring, i + 1, 1)
If Not k >= 0 Or k <= 9 Then
j = j + k
End If
Next
MsgBox j
End Sub
The sub provides the correct result "17815052007" in the msgbox. Why does the UDF not work?
Thanks & regards
KP