Link to home
Start Free TrialLog in
Avatar of kghi
kghiFlag for Canada

asked on

How to remove non numeric values from a text string in vb6/VBA

How to remove non numeric values from a string in vb6/VBA
Avatar of Robin Uijt
Robin Uijt
Flag of Netherlands image

Can you give us an example of strings and how you want them to be?
Avatar of TRobinJames
TRobinJames

if isNumeric(part of string)
   part of string is numeric
else
   part of string is not numeric
endif
Avatar of kghi

ASKER

A-9B9-9DC-9999
I want to remove any non numeric char . These alpha values can be anywhere in the string. So my result should be 9999999
ASKER CERTIFIED SOLUTION
Avatar of archdeac0n
archdeac0n
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You could use the function below:


Public Function RemoveAlphaNumChars(ByVal p_sStr As String) As String
    Dim l_lIdx As Long
    Dim l_sRet As String, l_sCh As String
    
    l_sRet = ""
    For l_lIdx = 1 To Len(p_sStr)
        l_sCh = Mid$(p_sStr, l_lIdx, 1)
        If InStr(1, "1234567890", l_sCh) > 0 Then
            l_sRet = l_sRet & l_sCh
        End If
    Next l_lIdx
    RemoveAlphaNumChars = l_sRet
End Function

Open in new window

Dim newstr as String
Dim ch as String
newstr = ""
For I = 1 to len(yourstr)
      ch = Mid(yourstr, I, 1)
      if IsNumeric(ch) Then newstr = newstr & ch
Next I
Here you go.
Private Sub Form_Load()
    '0-9: 48-57
    Dim strInput As String
    strInput = "he11o"
    Dim strOutput As String
    Dim i As Integer
    Dim chrCode As Integer
    For i = 1 To Len(strInput)
        chrCode = Asc(Mid(strInput, i, 1))
        If chrCode >= 48 And chrCode <= 57 Then
            strOutput = strOutput & Chr(chrCode)
        End If
    Next i
    MsgBox strOutput
End Sub

Open in new window