Find postion of number character within a string

G

Guest

I need some code to find the position of the last number character within a
string.

The string could be any length and may or may not have a number character.

for example a function that would do this:

pos = PosNum("Vessel 8 (2)") 'pos equals 11

Any ideas?

Thanks
 
G

Guest

This should do it... It returns -1 if no number is found...

Public Function LastNumber(ByVal InputString As String) As Integer
Dim intCounter As Integer
Dim intStringLength As Integer
Dim intReturnValue As Integer

intReturnValue = -1
intStringLength = Len(InputString)

For intCounter = intStringLength To 1 Step -1
If IsNumeric(Mid(InputString, intCounter, 1)) Then
intReturnValue = intCounter
Exit For
End If
Next intCounter

LastNumber = intReturnValue
End Function
 
G

Guest

thanks jim spot on

Jim Thomlinson said:
This should do it... It returns -1 if no number is found...

Public Function LastNumber(ByVal InputString As String) As Integer
Dim intCounter As Integer
Dim intStringLength As Integer
Dim intReturnValue As Integer

intReturnValue = -1
intStringLength = Len(InputString)

For intCounter = intStringLength To 1 Step -1
If IsNumeric(Mid(InputString, intCounter, 1)) Then
intReturnValue = intCounter
Exit For
End If
Next intCounter

LastNumber = intReturnValue
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top