Remove Numeric Characters

stehamp

New member
Local time
Today, 17:37
Joined
Jan 2, 2007
Messages
4
I have found the following module code which removes alpha characters from an alphanumeric string

Function RemoveAlphas (ByVal AlphaNum as Variant)

Dim Clean As String
Dim Pos, A_Char$

Pos = 1
If IsNull(AlphaNum) Then Exit Function

For Pos = 1 To Len(AlphaNum)
A_Char$ = Mid(AlphaNum, Pos, 1)
If A_Char$ >= "0" And A_Char$ <= "9" Then
Clean$ = Clean$ + A_Char$
End If
Next Pos

RemoveAlphas = Clean$

End Function

However I would like to do the opposite - i.e. remove numeric characters leaving only alpha characters. I'm sure this is a simple task but I am quite new to this. thanks
 
You may be able to use the in() function. ?

if in("1","2",etc)
 
Use the same code except change the equates:
If A_Char$ < "0" OR A_Char$ > "9" Then
 
All characters are stored as ASCII characters where
0 - 9 are stored as 48 - 57
A - Z is 65 - 90
a - z is 97 - 122

so
Function RemoveAlphas (ByVal AlphaNum as Variant) ' why is this a variant and not a string?
Dim Clean As String
Dim Pos As Integer
Dim temp As String

Pos = 1 ' what is this for??
'Use separate lines for each Dim statement or they will all be declared as 'variants'

If IsNull(AlphaNum) Then Exit Function
temp = AlphaNum
For Pos = Len(AlphaNum) To 1 Step -1 ' step backwards so that there is no complication about the length of temp

If Asc(Mid(AlphaNum, Pos, 1)) < 47 Or Asc(Mid(AlphaNum, Pos, 1)) > 58 Then ' this will remove all characters except numerals
AlphaNum = Left(AlphNum, Pos - 1) & Mid(AlphNum, Pos + 1)
End If
Next Pos
RemoveAlphas = temp
End Function
 
Function RemoveAlphas (ByVal AlphaNum as Variant) ' why is this a variant and not a string?

allows Nulls without crashing :)
 
I would have checked for zero length string before calling the function. Each to his own though.

Cheers
 
You can just reverse the NumbersOnly function I've posted in here for cleaner code.

Code:
Function CharsOnly(varConvert As Variant) As String
'Returns only the characters from a passed string. 

    Dim curChar As String
    Dim ctr As Integer
    
    If IsNull(varConvert) Then
        CharsOnly = ""
        Exit Function
    End If
    
    For ctr = 1 To Len(varConvert)
        curChar = Mid(varConvert, ctr, 1)
        If Not(IsNumeric(curChar)) Then
            CharsOnly = CharsOnly & curChar
        End If
    Next
    
End Function

To use it, just call it like any other function:

Code:
Your_Variable_Name = CharsOnly([String_Name_Where_You_Want_The_Numbers_Removed])

~Moniker
 

Users who are viewing this thread

Back
Top Bottom