Extracting Numbers Greater Than 3 Characters from a Text

Ongbey

New Member
Joined
Oct 16, 2018
Messages
29
Office Version
  1. 2013
My goal is to find numbers larger than 3 characters in a text and copy them into consecutive cells.
I explained this in the attached file.

Not sure how to do it without using macros. If it can be handled with the formula, it is my preference.
Thanks in advance.
 

Attachments

  • Ekran Alıntısı.JPG
    Ekran Alıntısı.JPG
    38.3 KB · Views: 17
In fact, my purpose is to get only numeric part of alpha-numeric expression. For example; if there is 89567X, I need to get only 89567. If there is 66555LL, I need to get only 66555.
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
If I wanted one more thing, I hope it wouldn't be too much.

Our codes are sometimes alphanumeric, but the letters are always at the end of the numeric. For example, next to 89567 and 66555 we have similar codes 89567X or 66555LL. Is it possible to get alphanumeric part codes along with these numeric codes?
That really changes the nature of the question quite significantly. My code depends on the entire "word" (string) being numeric.

I have some ideas, but it will take some time for me to play around with it. I will see if I can find some time to look at it a little later today.
 
Upvote 0
Try this code:
VBA Code:
Function GetNum(str As String, num As Integer) As Variant
'   Get numbers in string that are at least 3 digits
'   - str: string to search
'   - num: which number to bring back (1 = first, 2 = second, ...)

    Dim arr() As String
    Dim i As Long
    Dim n As Long
    Dim ret As Double
    Dim nn As Long
    
'   Split string by spaces and store in an array
    arr = Split(str, " ")
    
'   Loop through values in array
    For i = LBound(arr) To UBound(arr)
'       Count the number of numbers at the beginning of the string
        nn = CountNums(arr(i))
'       See if three or more numbers
        If nn >= 3 Then
'           Increment counter
            n = n + 1
'           See if this is the number you are looking for
            If n = num Then
'               Record number
                ret = Left(arr(i), nn)
'               Exit for loop
                Exit For
            End If
        End If
    Next i
    
'   See if it found a value
    If ret = 0 Then
        GetNum = "-"
    Else
        GetNum = ret
    End If
        
End Function


Function CountNums(str As String) As Long
'   Counts the number of numbers at the beginning of a string/word

    Dim i As Long
    Dim j As Long
    Dim n As Long
    
'   Find length of string
    j = Len(str)

'
    If j > 0 Then
        For i = 1 To j
            If IsNumeric(Mid(str, i, 1)) Then
                n = i
            Else
                Exit For
            End If
        Next i
    End If
    
    CountNums = n
    
End Function
You should be able to use the same formula on your worksheet as I gave before. These changes just update the VBA code behind the functions.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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