Extract only numbers from cells with different lengths

Stuepef

Board Regular
Joined
Oct 23, 2017
Messages
128
Office Version
  1. 2021
Platform
  1. Windows
I have a worksheet with different values within column A and I am looking to extract all of the numbers only. The values in column A can vary in terms of length and characters (ie $, %, .). I understand my current formula is replacing all "-" with " ", but I am looking for assistance to refine it. Open to all suggestions/solutions.

Current formula:
Code:
=INT(LEFT(REPLACE(SUBSTITUTE(A6,"-"," "),1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A6&1/17))-1,""),5))

Workbook:
Code:
[TABLE="width: 412"]
<tbody>[TR]
[TD][/TD]
[TD]Formula[/TD]
[TD]Desired result[/TD]
[/TR]
[TR]
[TD]Ford $25-95%[/TD]
[TD]#VALUE![/TD]
[TD]25-95[/TD]
[/TR]
[TR]
[TD]Focus $1,000-$30[/TD]
[TD]1000[/TD]
[TD]1000-30[/TD]
[/TR]
[TR]
[TD]Honda $1,000-$45[/TD]
[TD]1000[/TD]
[TD]1000-45[/TD]
[/TR]
[TR]
[TD]LED Bright $2,000-100%[/TD]
[TD]2000[/TD]
[TD]2000-100[/TD]
[/TR]
[TR]
[TD]Best $2,000-70% Quarter back[/TD]
[TD]2000[/TD]
[TD]2000-70[/TD]
[/TR]
[TR]
[TD]HIU Book $2,500-100% Ge Plus[/TD]
[TD]2500[/TD]
[TD]2500-100[/TD]
[/TR]
[TR]
[TD]USA Emboss. green $3,000-80%[/TD]
[TD]3000[/TD]
[TD]3000-80[/TD]
[/TR]
[TR]
[TD]USA Emboss. Green $4,000-100%[/TD]
[TD]4000[/TD]
[TD]4000-100[/TD]
[/TR]
[TR]
[TD]Masters Gold $300[/TD]
[TD]300[/TD]
[TD]300[/TD]
[/TR]
[TR]
[TD]Masters Gold $750[/TD]
[TD]750[/TD]
[TD]750[/TD]
[/TR]
[TR]
[TD]UK 2500-55-30% Orange[/TD]
[TD]2500[/TD]
[TD]2500-55-30[/TD]
[/TR]
[TR]
[TD]F.ACE.500.80.40[/TD]
[TD]500[/TD]
[TD]500.80.40[/TD]
[/TR]
[TR]
[TD]F.ACE.1000.80.40[/TD]
[TD]1000[/TD]
[TD]1000.80.40[/TD]
[/TR]
[TR]
[TD]F.Ace.1500.60.75[/TD]
[TD]1500[/TD]
[TD]1500.60.75[/TD]
[/TR]
[TR]
[TD]F.ACE.1500.100.25[/TD]
[TD]1500[/TD]
[TD]1500.100.25[/TD]
[/TR]
</tbody>[/TABLE]

I appreciate your help!
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Better to use regex in VBA, but a formula is possible:


Excel 2010
ABC
1Ford $25-95%25-9525-95
2Focus $1,000-$301000-301,000-30
3Honda $1,000-$451000-451,000-45
4LED Bright $2,000-100%2000-1002,000-100
5Best $2,000-70% Quarter back2000-702,000-70
6HIU Book $2,500-100% Ge Plus2500-1002,500-100
7USA Emboss. green $3,000-80%3000-803,000-80
8USA Emboss. Green $4,000-100%4000-1004,000-100
9Masters Gold $300300300
10Masters Gold $750750750
11UK 2500-55-30% Orange2500-55-302500-55-30
12F.ACE.500.80.40500.80.40500.80.40
13F.ACE.1000.80.401000.80.401000.80.40
14F.Ace.1500.60.751500.60.751500.60.75
Sheet3
Cell Formulas
RangeFormula
C1{=SUBSTITUTE(MID(A1,MATCH(TRUE,ISNUMBER(0+MID(A1,ROW(INDIRECT(1&":"&LEN(A1))),1)),0),1+LOOKUP(2,1/ISNUMBER(0+MID(A1,ROW(INDIRECT(1&":"&LEN(A1))),1)),ROW(INDIRECT(1&":"&LEN(A1))))-MATCH(TRUE,ISNUMBER(0+MID(A1,ROW(INDIRECT(1&":"&LEN(A1))),1)),0)),"$","")}
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
This normally-entered, non-volatile formula is rather long (a tad shorter than sheetspread's formula though), but it seems to do what you want...

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(MID(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")),FIND(" ",A1&" ",MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")))-MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))),"$",""),"%",""),",","")
 
Last edited:
Upvote 0
Thank you both so much, it works like a charm! Sending Rep!! :)
 
Upvote 0
Another shorter option :

=SUBSTITUTE(SUBSTITUTE(MID(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&1/17)),FIND("%",A1&"%")-MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&1/17))),"$",""),",","")

Regards
Bosco
 
Upvote 0
Another shorter option :

=SUBSTITUTE(SUBSTITUTE(MID(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&1/17)),FIND("%",A1&"%")-MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&1/17))),"$",""),",","")
That will work as long as none of the non-percentage numbers do not have text following them. In other words, it won't work for something like this...

F.Ace.1500.60.75 Orange

Admittedly, none of the OP's examples showed a value like this, and I don't know if such a combination is even possible, but I thought it could possibly be which is why I posted the formula that I did.
 
Upvote 0
@Rick Rothstein - How would I update the formula to show these results:

Code:
[TABLE="width: 387"]
<tbody>[TR]
[TD]Product[/TD]
[TD]Desired Result[/TD]
[/TR]
[TR]
[TD]Pontiac $500-$20[/TD]
[TD]20[/TD]
[/TR]
[TR]
[TD]Feet $500-$40[/TD]
[TD]40[/TD]
[/TR]
[TR]
[TD]USA Feet $2,000-100%[/TD]
[TD]100[/TD]
[/TR]
[TR]
[TD]USA Feet $2,000-100% Gt Plus[/TD]
[TD]100[/TD]
[/TR]
[TR]
[TD]Orange $3,000-70% Treat ACE Free[/TD]
[TD]70[/TD]
[/TR]
[TR]
[TD]USA Emboss. Orange $3,000-80%[/TD]
[TD]80[/TD]
[/TR]
[TR]
[TD]USA Emboss. Orange $4,000-100%[/TD]
[TD]100[/TD]
[/TR]
[TR]
[TD]USA Emboss. Orange $4,000-100% Gt Plus[/TD]
[TD]100[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
As suggested by sheetspread, you could consider using user-defined functions like these. To implement ..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot below and copy down.
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)

Code:
Function GetNums(s As String) As String
  Static RX As Object
  
  If RX Is Nothing Then
    Set RX = CreateObject("VBScript.RegExp")
    RX.Global = True
  End If
  RX.Pattern = "(\D*)([\d\$\-\.,]+)(\D*)"
  GetNums = Replace(Replace(RX.Replace(s, "$2"), "$", ""), ",", "")
End Function


Function LastNum(s As String) As Long
  Static RX As Object
  
  If RX Is Nothing Then
    Set RX = CreateObject("VBScript.RegExp")
    RX.Global = True
  End If
  RX.Pattern = "[\d,]+(?=\D*$)"
  LastNum = RX.Execute(s)(0)
End Function

Excel Workbook
ABC
2Ford $25-95%25-9595
3Focus $1,000-$301000-3030
4Honda $1,000-$451000-4545
5LED Bright $2,000-100%2000-100100
6Best $2,000-70% Quarter back2000-7070
7HIU Book $2,500-100% Ge Plus2500-100100
8USA Emboss. green $3,000-80%3000-8080
9USA Emboss. Green $4,000-100%4000-100100
10Masters Gold $300300300
11Masters Gold $750750750
12UK 2500-55-30% Orange2500-55-3030
13F.ACE.500.80.40500.80.4040
14F.ACE.1000.80.401000.80.4040
15F.Ace.1500.60.751500.60.7575
16F.ACE.1500.100.251500.100.2525
Extract numbers
 
Upvote 0
A code like Peter's is the most flexible for variable criteria. Your particular sample in Post 7 can be solved with this formula:


Excel 2010
AB
1ProductDesired Result
2Pontiac $500-$2020
3Feet $500-$4040
4USA Feet $2,000-100%100
5USA Feet $2,000-100% Gt Plus100
6Orange $3,000-70% Treat ACE Free70
7USA Emboss. Orange $3,000-80%80
8USA Emboss. Orange $4,000-100%100
9USA Emboss. Orange $4,000-100% Gt Plus100
Sheet6
Cell Formulas
RangeFormula
B2=SUBSTITUTE(TRIM(MID(SUBSTITUTE(SUBSTITUTE(A2,"-",REPT(" ",100)),"%",REPT(" ",100)),100,100)),"$","")


But it doesn't really handle exceptions.
 
Upvote 0
Thank you for the replies! Unfortunately I wont be able to incorporate any VBA into this particular workbook though it would be awesome to do so. There are variable inputs in column A and it wont maintain the same format throughout.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,180
Members
453,021
Latest member
Justyna P

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