VBA user defined function: Find position of last number in cell

Showroom

New Member
Joined
Feb 13, 2017
Messages
15
Hi

I have a cell (Let just say A1) which include both numbers and text. For example:

12x24Z

I want a user defined function, which give me the position of the last number in that cell. In this case the position for number 4 (position = 5).

I think I find a solution with the standard Excel functions, but it is a little heavy
smile.gif
So I thought I could so something smart by creating a user defined function in VBA, but so far I only have:



Function FindNum() as long

dim i as long

FindNum = 0



and nothing more. I am blank. Any ideas?
smile.gif


Thank you so much.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
How about this?

VBA Code:
Function LASTPOS(s As String)
Dim b() As Byte:    b = s
For i = UBound(b) - 1 To 0 Step -2
    If b(i) > 47 And b(i) < 58 Then LASTPOS = (i / 2) + 1: Exit Function
Next i
End Function
 
Upvote 0
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
What version of Excel are you using?

I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
VBA Code:
Function LastNumber(rng As Range)
        Dim Regex As Object
        Set Regex = CreateObject("VbScript.Regexp")
        Regex.Pattern = "[0-9]"
        Regex.Global = True
        Dim mc As MatchCollection
        Set mc = Regex.Execute(rng.Value)
       
        LastNumber = mc(mc.Count - 1).FirstIndex + 1
       
End Function
 

Attachments

  • 1674047784378.png
    1674047784378.png
    20 KB · Views: 9
Upvote 0
If you have 365 how about a formula
Excel Formula:
=LEN(TEXTBEFORE(A1,SEQUENCE(10,,0),-1))+1
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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