remove non integers from a string

mike2000

Registered User.
Local time
Today, 22:35
Joined
Mar 3, 2003
Messages
15
Could someone please help me put together some code to check a string for non integer characters, and delete them if present.

Thanks
 
How's this for you?

Code:
Public Function RemoveNumericValues(ByVal strText As String) As String

    On Error GoTo Err_RemoveNumericValues
    
    Dim intCounter As Integer
    Dim strTemp As String
    
    strText = Trim(strText) ' remove leading spaces (if any)
    
    For intCounter = 1 To Len(strText)
        If Not IsNumeric(Mid(strText, intCounter, 1)) Then
            strTemp = strTemp & Mid(strText, intCounter, 1)
        End If
    Next intCounter
    
    RemoveNumericValues = strTemp
    
Exit_RemoveNumericValues:
    Exit Function
    
Err_RemoveNumericValues:
    MsgBox Err.Description, vbExclamation, "Error #" & Err.Number
    Resume Exit_RemoveNumericValues

End Function
 
Thanks for the reply, i'll try it out and let you know how i get on.
 
help calling the function

I'm having a bit of difficulty as im new to functions. How do i call it? I need to apply the function to the string on the Before update of a text box.
Thanks for your patience.
 
Me.YourTextbox = RemoveNumericValues(Me.txtYourTextbox)
 
Another approach. Does essentially the same thing but allows the user to remove either numeric or alpha characters.
Code:
Function scrubomatic(ByVal thestuff As String, KeepNum As Boolean) As Variant
'*******************************************
'Name:      scrubomatic (Function)
'Purpose:   Remove all numeric or non-numeric
'           characters from a string
'Inputs:    from debug window:
'   (1) To remove non-numeric characters  ? scrubomatic("abc$123", True)
'   (2) To remove numeric characters    ? scrubomatic("abc$123 xyz456", False)
'Output:
'   (1) 123
'   (2) abc$
'*******************************************

Dim strHold As String, intLen As Integer, n As Integer
strHold = RTrim(thestuff)
intLen = Len(strHold)
strHold = ""
For n = 1 To intLen
   If KeepNum = False Then  'Remove numeric characters
      strHold = strHold & IIf(Not IsNumeric(Mid(thestuff, n, 1)), Mid(thestuff, n, 1), "")
   Else                     'retain only numeric characters
      strHold = strHold & IIf(IsNumeric(Mid(thestuff, n, 1)), Mid(thestuff, n, 1), "")
   End If
Next n
scrubomatic = strHold
End Function
 
Milo thanks for that it works great, i had to remove the Not from IsNumeric as it was leaving me with only the text and i actually wanted to remove this and keep the numbers.

Raskew thanks for your function havn't had time to play with it yet but ill try it out later.

Thanks for your help guys, great site.
 

Users who are viewing this thread

Back
Top Bottom