Extract Numbers from alphanumeric strings

Sandeep Warrier

Well-known Member
Joined
Oct 31, 2008
Messages
2,680
Hey All,

I have this formula that extracts numbers from alphanumeric strings.

Code:
{=1*MID(A1,MATCH(TRUE,ISNUMBER(1*MID(A1,ROW($1:$100),1)),0),COUNT(1*MID(A1,ROW($1:$100),1)))}

However this extracts only the 1st instance of the numbers

In a string like 123avfbsdf4556.. it'll extract only 123.

My questions are the following:
1. Is there a way that i could get the result as 1234556
2. A way which refers to a cell where I put in a number and it'll extract those many number instances. In the above example, if I put the number as 1, it'll extract 123. If I put the number as 2, it'll extract 4556 and so on.

I guess this would require some modifications to the Match function so that it does not look at only the 1st instance.

Thanks in advance for any help.

Regards,
Sandeep.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Is it a formula solution you're looking for, or would a macro work for you?
 
Upvote 0
Try this:

Code:
Function NUMINSTR(ByVal str As String, Optional ByVal idx As Integer = 1) As Double
    Dim first As Integer
    Dim last As Integer
    Dim test As String
    Dim a As String
    Dim x As Integer
    Dim z As Integer
    Dim StartVal As Integer
    
    StartVal = 1
    test = "0123456789."
    
    
    If Len(str) = 0 Then
        NUMINSTR = Null
        Exit Function
    End If
    
    For z = 1 To idx
    
        If z > 1 Then StartVal = last + 1
            
        If StartVal > Len(str) Then
            NUMINSTR = Null
            Exit Function
        End If
        
        For x = StartVal To Len(str)
            a = Mid(str, x, 1)
            If InStr(test, a) > 0 Then
                first = x
                Exit For
            End If
        Next x
        
        If first = 0 Then
            NUMINSTR = Null
            Exit Function
        End If
        
        last = Len(str) + 1
        
        For x = first To Len(str)
            a = Mid(str, x, 1)
            If InStr(test, a) = 0 Then
                last = x
                Exit For
            End If
        Next x
    
    Next z
    
    NUMINSTR = val(Mid(str, first, last - first))
    
End Function

It's actually a user defined function you use it like a built in function and pass the string and the n th number set you want from it.
 
Upvote 0
Hi

Another option:

Code:
Function NUMINSTR(ByVal str As String, Optional ByVal idx As Integer = 1)
Dim oMatches As Object
 
With CreateObject("vbscript.regexp")
    .Pattern = "\d+"
    .Global = True
    Set oMatches = .Execute(str)
    If oMatches.Count >= idx Then NUMINSTR = oMatches(idx - 1) Else NUMINSTR = ""
End With
End Function
 
Upvote 0
A formula option to extract all digits:

=SUM(MID(0&A1,LARGE(IF(ISNUMBER(--MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)),ROW(INDIRECT("1:"&LEN(A1)))+1,1),ROW(INDIRECT("1:"&LEN(A1)))),1)*10^(ROW(INDIRECT("1:"&LEN(A1)))-1))

Confirmed with Ctrl+Shift+Enter
 
Last edited:
Upvote 0
Thanks a lot Richard & PGC

PGC, Your code worked perfectly.. Do you have any tutorials on the .Pattern stuff? I've come across it in many codes and seems to be very useful.


Richard, excel didnt allow me to enter your formula. I guess that since I use excel 2003 its too long.
 
Upvote 0
I think it's that 'regexp' bit that's significant. I've seen it before but I hadn't realised how versatile it was!
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,240
Members
452,898
Latest member
Capolavoro009

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