formula needed for numbers

ericlch16

Active Member
Joined
Nov 2, 2007
Messages
313
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
in a column A there is these values

F23456 H9454555 J232334
F#23450 H9832444 J893338
H89232123 F#99999 J#888888
78734 567788 6788967
K899932 L89765 J9999999

i want a formula to return the 5 digit number i.e
23456
23450
99999
78734
89765

is that possible? there is no consistency in the data in the cells. the number are not in order but there is always 5 digit number, 6 digit number, 7 digit numbers. i want to pick up the 5 digit number in another column?is that possible?
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
=mid(left(a1,find(" ",a1)-1),min(find({"0","1","2","3","4","5","6","7","8","9"},a1&"0123456789")),find(" ",a1))+0
 
Last edited:
Upvote 0
thanks

I am trying to understand the formula..but how to get the 6 digit number or 7 digit number? which parameters you change in the formula?
 
Upvote 0
Another way:
Code:
Function GetDigits(ByVal sInp As String, nDigits As Long) As String
    sInp = " " & sInp & " "
    
    With CreateObject("VBScript.Regexp")
        .Pattern = "\D(\d{" & nDigits & "})\D"
        .Global = False
        If .Test(sInp) Then GetDigits = .Execute(sInp)(0).SubMatches(0)
    End With
End Function

Code:
      ------------A------------- --B-- --C--- ---D---
  1                                5     6       7   
  2   F23456 H9454555 J232334    23456 232334 9454555
  3   F#23450 H9832444 J893338   23450 893338 9832444
  4   H89232123 F#99999 J#888888 99999 888888        
  5   78734 567788 6788967       78734 567788 6788967
  6   K899932 L89765 J9999999    89765 899932 9999999
  7   abcd 12345                 12345

The formula in B2 and copied across and down is

=GetDigits($A2, B$1)
 
Upvote 0
FYI, approach I used
Code:
Function Five(ByVal inp As String) As Variant
 
  Dim i As Long
  Dim ar As Variant
 
  Five = CVErr(xlErrNA)
  ar = Split(inp)
  For i = LBound(ar) To UBound(ar)
    If ar(i) Like "[0-9][0-9][0-9][0-9][0-9]" Then
      Five = CLng(ar(i))
      Exit For
    ElseIf ar(i) Like "*[!0-9][0-9][0-9][0-9][0-9][0-9]" Then
      Five = CLng(Right$(ar(i), 5))
      Exit For
    End If
  Next i
End Function
 
Upvote 0
is there anyway i can get it without writing an internal function?
 
Upvote 0
perilously long but this should work where A1 contains the data and B1 the length of the number to be extracted

=LOOKUP(10^B1,MID(SUBSTITUTE(A1," ","x"),ROW(INDIRECT("1:"&LEN(A1)-B1+1)),B1)/ISERR(MID("x"&A1&"x",ROW(INDIRECT("1:"&LEN(A1)-B1+1)),1)+0)/ISERR(MID("x"&A1&"x",ROW(INDIRECT(2+B1&":"&LEN(A1)+2)),1)+0))
 
Upvote 0

Forum statistics

Threads
1,224,559
Messages
6,179,513
Members
452,921
Latest member
BBQKING

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