leave numbers only in a range

ktab

Well-known Member
Joined
Apr 21, 2005
Messages
1,297
Hello,

How can i leave only the numbers contained in a range (like: cell(1,1)= a12/r . I want it as 12 only).

Thank you
Kostas
 
Hi Kostas

For just the first number present please try:

Code:
Function NumOnly(txt As String) As String
Dim oMatches As Object

With CreateObject("VBScript.RegExp")
    .Pattern = "\d+"

    Set oMatches = .Execute(txt)
    
    If oMatches.Count Then
        NumOnly = oMatches(0).Value
    Else
        NumOnly = "Not found"
    End If
End With
End Function

By the way, you are right, it's the regular expressions present in UNIX since the late seventies.

Hope this helps
PGC
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hi Kostas

Give this a go:

Code:
Function NumOnly(txt As String) As String
Dim regex As Object, currMatches
Set regex = CreateObject("VBScript.RegExp")
With regex
    .Pattern = "\d+"
    .Global = True
End With
Set currMatches = regex.Execute(txt)
NumOnly = ""
If currMatches.Count > 0 Then
    NumOnly = currMatches(0)
End If
End Function

Richard
 
Upvote 0
I tried directly yours because I got confused by PGC's comment "just for the first number" but it works too.
So I'd like to thank you both for prompt & fine replies
 
Upvote 0

Forum statistics

Threads
1,216,031
Messages
6,128,422
Members
449,450
Latest member
gunars

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