ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Find postion of number character within a string (https://www.excelbanter.com/excel-programming/387155-find-postion-number-character-within-string.html)

mcphc

Find postion of number character within a string
 
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


Jim Thomlinson

Find postion of number character within a string
 
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
--
HTH...

Jim Thomlinson


"mcphc" wrote:

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


mcphc

Find postion of number character within a string
 
thanks jim spot on

"Jim Thomlinson" wrote:

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
--
HTH...

Jim Thomlinson


"mcphc" wrote:

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



All times are GMT +1. The time now is 07:23 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com