Find Last Number Within String

halcyon1

New Member
Joined
Feb 11, 2010
Messages
2
I'm currently working on some string manipulation functions. I need to find the last number within a string, and return the position of it.

I can use InstrRev to find the last position of a single number but not sure how to do it to show the last position of any number (or if there is a better way).

Say my example data is as follows

1. Some text Would go Here 123
2. 321 Some text would go here
3. Some 456 Text would go here

I would like the function to return

1. 27
2. 3
3. 8

Any help would be greatfully appreciated!
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi and welcome to MrExcel,
Try
Code:
Sub GetLastNum()
    Dim lngCounter As Long
    Dim lngPosn As Long
    Dim strVal As String
    Dim strValRev As String
    
    strVal = "abc 123 defg"
    strValRev = StrReverse(strVal)
    For lngCounter = 1 To Len(strValRev)
        If IsNumeric(Mid(strValRev, lngCounter, 1)) = True Then
            Exit For
        End If
    Next lngCounter
    
    lngCounter = Len(strVal) - (lngCounter - 1)
      
End Sub
 
Upvote 0
Hi, Try this:-
Code:
Function Lnum(Rng [COLOR=navy]As[/COLOR] Range)
[COLOR=navy]Dim[/COLOR] n [COLOR=navy]As[/COLOR] [COLOR=navy]Integer,Lst as Integer[/COLOR]
    [COLOR=navy]For[/COLOR] n = 0 To 9
        Lst = InStrRev(Rng, n)
        Lnum = IIf(InStrRev(Rng, n) > Lnum, Lst, Lnum)
    [COLOR=navy]Next[/COLOR] n
[COLOR=navy]End[/COLOR] Function
Regards Mick
 
Last edited:
Upvote 0
Thank you both for your quick replies, both examples worked exactly as I wanted, much appreciated! :)
 
Upvote 0
1] You can try this array formula :

{=MATCH(1,-MID(A1,ROW($1:$99),1))}

2] Or, this non-array formula :

=LOOKUP(1,-MID(A1,ROW($1:$99),1),ROW($1:$99))

Regards
Bosco
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top