Convert Alphanumeric to Numeric

Thomas Jones
Thomas Jones used Ask the Experts™
on
I would like to create a VBA function that removes alphanumeric characters from a string leaving only integers.  For example, "A123BC4" would be parsed to be 1234.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
President / Owner CARDA Consultants Inc.
Distinguished Expert 2018
Commented:
Public Function NumbersOnly(strString As String) As String
    Dim lngCtr                As Long
    Dim intChar               As Integer

    For lngCtr = 1 To Len(strString)
        intChar = Asc(Mid(strString, lngCtr, 1))
        If intChar >= 48 And intChar <= 57 Then
            NumbersOnly = NumbersOnly & Chr(intChar)
        End If
    Next lngCtr
End Function

Open in new window

NumbersOnly("A123BC4") ' ->1234

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial