0

How can I get numeric from end of a string For example if string is 24Text147 i should get 147 only or if string is text12 i should get 12 only. Here is what I have found but it gives all the numerics in a string

Function onlyDigits(s As String) As String

    Dim retval As String    
    Dim i As Integer        

    retval = ""
                                     '
    For i = 1 To Len(s)
        If mId(s, i, 1) >= "0" And mId(s, i, 1) <= "9" Then
            retval = retval + mId(s, i, 1)
        End If
    Next
                             '
    onlyDigits = retval
End Function

2 Answers 2

1
Function onlyDigits(s As String) As String
    Dim retval As String
    Dim i As Integer

    retval = ""
                                     '
    For i = Len(s) To 1 Step -1
        If Mid(s, i, 1) Like "#" Then
            retval = Mid(s, i, 1) & retval
        Else
            Exit For
        End If
    Next
    onlyDigits = retval
End Function
Sign up to request clarification or add additional context in comments.

1 Comment

or just if isnumeric (mid(s,i,1)) then. +1 anyway, i like the like
0

Another way:

Function onlyDigits(s As String) As String
For i = Len(s) To 1 Step -1
    If IsNumeric(Right(s, i)) Then
    onlyDigits = Right(s, i)
    Exit Sub
    End If
Next
End Function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.