How to check if a string contains only Numeric Characters
Posted on January 4, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 'String Manipulation - How to check if a string contains only Numeric Characters Option Explicit Public Function IsNumericOnly(sExpression As String) As Boolean 'Returns True if all characters in string are numeric 'Returns False otherwise or for empty string 'This is different than VB's built in function isNumeric 'isNumeric returns true for something like 10.926 'This function will return false Dim sTemp As String Dim iLen As Integer Dim iCount As Integer Dim sChar As String sTemp = sExpression iLen = Len(sTemp) If iLen > 0 Then For iCount = 1 To iLen sChar = Mid(sTemp, iCount, 1) If Not sChar Like "[0-9]" Then Exit Function Next IsNumericOnly = True End If End Function |