×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Function to strip all other than numbers from a field

Function to strip all other than numbers from a field

Function to strip all other than numbers from a field

(OP)
Hello I have a field InvoiceNumber

The invoice data sometimes has prefix or postfix - ie. eet3546 or ibb876373 or 2355456llk

I would like a function which removes everything and just leaves the numbers with no spaces or other characters.

Could someone help with this? I really can't seem to make progress with this.

Thanks Mark - Keep safe :)

RE: Function to strip all other than numbers from a field

I am sure there is a better way, but ...

CODE

Option Explicit

Private Sub Command1_Click()
Dim str1 As String
Dim str2 As String
Dim str3 As String

str1 = "eet3546"
str2 = "ibb876373"
str3 = "2355456llk"

Debug.Print "The value of " & str1 & " is " & MyValue(str1)
Debug.Print "The value of " & str2 & " is " & MyValue(str2)
Debug.Print "The value of " & str3 & " is " & MyValue(str3)

End Sub

Private Function MyValue(ByRef str As String) As Long
Dim x As Integer
Dim strOut As String

For x = 1 To Len(str)
    If IsNumeric(Mid(str, x, 1)) Then
        strOut = strOut & Mid(str, x, 1)
    End If
Next x

MyValue = CLng(strOut)

End Function 

You get:
The value of eet3546 is 3546
The value of ibb876373 is 876373
The value of 2355456llk is 2355456


---- Andy

There is a great need for a sarcasm font.

RE: Function to strip all other than numbers from a field

(OP)
Thats great - thank you very much

Regards Mark

RE: Function to strip all other than numbers from a field

(OP)
I have tested it - and it works well.

I notice it is fine if there is a 10 or less digit number, but it crashes if 11 digits or more.

Is there a way to fix that?

Thank you Mark

RE: Function to strip all other than numbers from a field

That is because of the limit of Long.

You could modify the Function to:

CODE

Private Function MyValue(ByRef str As String) As Double
Dim x As Integer
Dim strOut As String

For x = 1 To Len(str)
    If IsNumeric(Mid(str, x, 1)) Then
        strOut = strOut & Mid(str, x, 1)
    End If
Next x

MyValue = CDbl(strOut)

End Function 

But you may get back the huge number in scientific notation smile

You can simply do:

CODE

Private Function MyValue(ByRef str As String) As String
Dim x As Integer
Dim strOut As String

For x = 1 To Len(str)
    If IsNumeric(Mid(str, x, 1)) Then
        strOut = strOut & Mid(str, x, 1)
    End If
Next x

MyValue = strOut

End Function 

and you get the String with just the digits (numbers)... wavey3


---- Andy

There is a great need for a sarcasm font.

RE: Function to strip all other than numbers from a field

(OP)
Fantastic - thank you :)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close