Extract 7 digit number from text

Jackadair

New Member
Joined
Sep 22, 2022
Messages
3
Office Version
  1. 365
Evening!

First post, mostly be back in the future if this gets answered!

Need to extract 7 digit number from text

Example

Abbb bbb b 1234567 (os3)(n)
Is so3 bb | 7654321 (33pp)
So3 bea | 1111111(or8)

Need a formula that would extract the 7 digit number from all those text at once so it would look like

1234567
7654321
1111111
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
How about this?

COMPSTAT Template1 (version 2).xlsb
AB
1Abbb bbb b 1234567 (os3)1234567
2Is so3 bb | 7654321 (33pp)7654321
3So3 bea | 1111111(or8)1111111
Sheet3


VBA Code:
Sub EXTRACTSEVEN()
Dim r As Range:         Set r = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
Dim AR() As Variant:    AR = r.Value2

With CreateObject("VBScript.RegExp")
    .Pattern = "\d{7}"
    For i = 1 To UBound(AR)
        AR(i, 1) = .Execute(AR(i, 1))(0)
    Next i
End With

r.Offset(, 1) = AR
End Sub
 
Upvote 0
If the numbers you want will always be 7 digits long and every cell will have a 7-digit number in the text and there will never be a longer number in any of the cells, then put the first number (1234567) in cell B1, the second number (7654321) in cell B2, then select cell B3 (its empty) and press CTRL+E and Excel should pull all the numbers for you.
 
Upvote 0
Here's an Excel formula option, where you would change the two 7's to a different number for a number with n digits. (But the formula displayed works for 7 digit numbers. And if you change it to 6 to look for 6 digit numbers instead, and there is a 7 digit number, it will show the first 6 digits of the 7 digit number, etc.):
b.xlsb
AB
1Abbb bbb b 1234567 (os3)1234567
2Is so3 bb | 7654321 (33pp)7654321
3So3 bea | 1111111(or8)1111111
Sheet2
Cell Formulas
RangeFormula
B1:B3B1=MID(A1,SEARCH(REPT("1",7),TEXTJOIN(,,IFERROR(MID(A1,SEQUENCE(,LEN(A1)),1)*0+1,2))),7)
 
Last edited:
Upvote 0
Or (works from Ex 2010)
Test.xlsm
AB
1Abbb bbb b 1234567 (os3)1234567
2Is so3 bb | 7654321 (33pp)7654321
3So3 bea | 1111111(or8)1111111
Sheet3
Cell Formulas
RangeFormula
B1:B3B1=AGGREGATE(14,6,MID(A1,ROW(INDIRECT("1:" & LEN(A1))),7)+0,1)
 
Upvote 0
Here's an alternate/shorter solution for 365:
b.xlsb
AB
1Abbb bbb b 1234567 (os3)1234567
2Is so3 bb | 7654321 (33pp)7654321
3So3 bea | 1111111(or8)1111111
Sheet2
Cell Formulas
RangeFormula
B1:B3B1=INDEX(SORT(MID(SUBSTITUTE(A1," ",""),SEQUENCE(,LEN(A1)),7)+0,,,1),1)
 
Upvote 0
Without knowing what might really be in the data, I would say that posts 5 & 6 suggestions are not robust. Examples of incorrect results:

22 09 23.xlsm
AB
1A12561E2 Abbb 1234567 (os3)1256100
2A12DEC22 Abbb 1234567 (os3)44907
Extract (2)
Cell Formulas
RangeFormula
B1B1=AGGREGATE(14,6,MID(A1,ROW(INDIRECT("1:" & LEN(A1))),7)+0,1)
B2B2=INDEX(SORT(MID(SUBSTITUTE(A2," ",""),SEQUENCE(,LEN(A2)),7)+0,,,1),1)


Assuming numbers longer than 7 digits are not possible, I would suggest some minor tweaks to the post 4 formula.
The double quotes in REPT can be omitted
The addition near the end can be avoided by using zero instead of 1
The two blank arguments in TEXTJOIN can be avoided by using CONCAT instead

22 09 23.xlsm
AB
1Abbb bbb b 1234567 (os3)1234567
2Is so3 bb | 7654321 (33pp)7654321
3So3 bea | 1111111(or8)1111111
4A12561E2 Abbb 1234567 (os3)1234567
5A12DEC22 Abbb 1234567 (os3)1234567
Extract
Cell Formulas
RangeFormula
B1:B5B1=MID(A1,SEARCH(REPT(0,7),CONCAT(IFERROR(MID(A1,SEQUENCE(LEN(A1)),1)*0,2))),7)
 
Upvote 0
How about this?

COMPSTAT Template1 (version 2).xlsb
AB
1Abbb bbb b 1234567 (os3)1234567
2Is so3 bb | 7654321 (33pp)7654321
3So3 bea | 1111111(or8)1111111
Sheet3


VBA Code:
Sub EXTRACTSEVEN()
Dim r As Range:         Set r = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
Dim AR() As Variant:    AR = r.Value2

With CreateObject("VBScript.RegExp")
    .Pattern = "\d{7}"
    For i = 1 To UBound(AR)
        AR(i, 1) = .Execute(AR(i, 1))(0)
    Next i
End With

r.Offset(, 1) = AR
End Sub
Brilliant, apologise I am fairly new to excel. Some functions on excel aren't avaliable due to this being a work computer.


I don't belive I'm able to add a VBA
 
Upvote 0
Assuming numbers longer than 7 digits are not possible, I would suggest some minor tweaks to the post 4 formula.
I don't see the difference between the output of my formula in post#4 and yours in this regard. For example, I put an 8 digit number in the cell, and both formulas outputted the first 7 digits of it.

(And my formula gave the same outputs as the ones you tested yours with.)

But thanks for making improvements to it!

(And thanks for catching that my second formula was faulty.)
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,275
Members
452,902
Latest member
Knuddeluff

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