Extract numbers from text string

bertie3856

New Member
Joined
Aug 15, 2018
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hi, I have a line of text that contains numbers. I only want to extract the first set of numbers. Example, the text in cell A1 will always say "Deposit Black PB 500.00". There will always be 2 spaces after PB, however the amount will vary, from say 1.00 to 50000.00. I only want to extract the 500. Any ideas? Apologies if this has been posted previously. Thanks.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi, I have a line of text that contains numbers. I only want to extract the first set of numbers. Example, the text in cell A1 will always say "Deposit Black PB 500.00". There will always be 2 spaces after PB, however the amount will vary, from say 1.00 to 50000.00. I only want to extract the 500. Any ideas? Apologies if this has been posted previously. Thanks.

make a copy of your workbook and test the function there - do not test it directly on your work.

place the function code below in a module, then in the cell place the formula as =Digits(A2) (example)
if you wish to convert it to numbers =value(Digits(A2)) or =Digits(A2)*1


Code:
Function Digits(ByVal S As String) As String 'Courtesy of Rick Rothstein
  Dim X As Long
  For X = 1 To Len(S)
    If Mid(S, X, 1) Like "[!0-9,.]" Then Mid(S, X) = Chr(1)
  Next
  Digits = Trim(Replace(S, Chr(1), ""))
End Function
 
Last edited:
Upvote 0
Sorry, my first question was a bit misleading, as I forgot the other numbers. The line actually reads "Deposit Black PB 500.00 8596512555". The 500.00 or 1000.00 or whatever it is is the only variable. Your code didn't take into account the decimal point, so it returned 50000 instead of 500 or 500.00. Either would do.
 
Upvote 0
Sorry, my first question was a bit misleading, as I forgot the other numbers. The line actually reads "Deposit Black PB 500.00 8596512555". The 500.00 or 1000.00 or whatever it is is the only variable. Your code didn't take into account the decimal point, so it returned 50000 instead of 500 or 500.00. Either would do.
Give this function a try...
Code:
Function FirstNumber(ByVal S As String) As Double
  Dim X As Long
  For X = 1 To Len(S)
    If Mid(S, X, 1) Like "[!0-9,.]" Then Mid(S, X) = " "
  Next
  FirstNumber = Split(Trim(S))(0)
End Function
 
Upvote 0
Solution
Give this function a try...
Code:
Function FirstNumber(ByVal S As String) As Double
  Dim X As Long
  For X = 1 To Len(S)
    If Mid(S, X, 1) Like "[!0-9,.]" Then Mid(S, X) = " "
  Next
  FirstNumber = Split(Trim(S))(0)
End Function

Perfect, thank you very much.
 
Upvote 0
Hi,

A formula solution, based on your samples:


Book1
AB
1Deposit Black PB 500.00500.00
2Deposit Black PB 500.00 8596512555500.00
3Deposit Black PB 50000.00 859651255550000.00
Sheet185
Cell Formulas
RangeFormula
B1=TRIM(LEFT(SUBSTITUTE(MID(A1,SEARCH("PB ",A1)+4,255)," ",REPT(" ",30)),30))+0


Formula copied down.
Result converted to Real Number.
Format result cell as Number to show desired decimal places.
 
Upvote 0

Forum statistics

Threads
1,214,996
Messages
6,122,636
Members
449,092
Latest member
bsb1122

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