excel vba keep only numbers in string

✔ Recommended Answer

A simple method is by using Regular Expressions in a function.Merely pass the string to this function, at it will remove all of the non-digits:

Function removeNonDigits(str As String) As String 'or as long, if you prefer    Dim re As Object    Set re = CreateObject("vbscript.regexp")With re    .Pattern = "\D+"    .Global = True    removeNonDigits = .Replace(str, "")End WithEnd Function

If you don't want to use Regular Expressions, you can loop through the string:

Function removeNonDigits(str As String) As String    Dim I As Long    Dim tmp As Stringtmp = ""For I = 1 To Len(str)    If IsNumeric(Mid(str, I, 1)) Then        tmp = tmp & Mid(str, I, 1)    End IfNext IremoveNonDigits = tmpEnd Function

Source: stackoverflow.com

Answered By: Ron Rosenfeld

Comments

Most Popular

Remove Unicode Zero Width Space PHP

PhpStorm, return value is expected to be 'A', 'object' returned

Laravel file upload returns forbidden 403, file permission is 700 not 755