Extracting left most numbers from excel

dlsdioquino

New Member
Joined
Mar 23, 2018
Messages
7
Hello All
I need to extract the left most numbers in a cell.
Here is an example:

CELL EXTRACT
ASD-12 12
ASD-12-D-123 12
ASD-1-2-3 1

I already have this formula but it extracts all numbers

=SUM(MID(0&A5,LARGE(ISNUMBER(--MID(A5,ROW(INDIRECT("1:"&LEN(A5))),1))*ROW(INDIRECT("1:"&LEN(A5))),ROW(INDIRECT("1:"&LEN(A5))))+1,1)*10^ROW(INDIRECT("1:"&LEN(A5)))/10)

Would appreciate your help. Thank you!
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Welcome to the MrExcel board!

Based on that sample data, this should do it.

=REPLACE(LEFT(A2,FIND("-",A2&"-",5)-1),1,4,"")+0

Edit: Or even ..
=MID(A2,5,FIND("-",A2&"-",5)-5)+0


If that is not it, please give a more representative set of sample data and expected results.
 
Last edited:
Upvote 0
Sorry I also noticed that I put a sample size with all the same format. Here are more sample data with the expected results:
QLD PO-Q160041A160041

<tbody>
</tbody>
HMK-323-MANA-01323
HMK-743-MS1500-01743

<colgroup><col><col></colgroup><tbody>
</tbody>

WD PO-15471, ORDER C15471

<tbody>
</tbody>
Westrac 777G-PI-IS777

<tbody>
</tbody>
 
Upvote 0
Here are more sample data with the expected results:
Much more useful! :)

You could try this user-defined function. 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 LeftNum(s As String) As Long
  Static RX As Object
  
  If RX Is Nothing Then Set RX = CreateObject("VBScript.RegExp")
  RX.Pattern = "\d+"
  LeftNum = RX.Execute(s)(0)
End Function

Excel Workbook
AB
2ASD-1212
3ASD-12-D-12312
4ASD-1-2-31
5QLD PO-Q160041A160041
6HMK-323-MANA-01323
7HMK-743-MS1500-01743
8WD PO-15471, ORDER C15471
9Westrac 777G-PI-IS777
Sheet1
 
Upvote 0
Upon testing other data I've stumbled upon some fields that would not seem to work such as:
BAR PO-4500016742
BHP PO- 4504238970
3004544191

<tbody>
</tbody>

<tbody>
</tbody>

<tbody>
</tbody>


Sorry to keep on asking, I'm really new to excel and had no experience with it.
 
Upvote 0
If you need a formula solution give this a try:

Excel Workbook
AB
1QLD PO-Q160041A160041
2
3HMK-323-MANA-01323
4HMK-743-MS1500-01743
5
6
7WD PO-15471, ORDER C15471
8
9Westrac 777G-PI-IS777
10BAR PO-45000167424500016742
11BHP PO- 450423897030045441914.50424E+19
Sheet1
 
Upvote 0
The following function will return numbers up to 28 digits long, but those numbers will be text numbers, not real numbers.
Code:
[table="width: 500"]
[tr]
	[td]Function LeftNum(ByVal S As String) As Variant
  Dim X As Long, Z As Long
  For X = 1 To Len(S)
    If IsNumeric(Mid(S, X, 1)) Then
      S = Mid(S, X) & "X"
      For Z = 1 To Len(S)
        If Not IsNumeric(Mid(S, Z, 1)) Then
          LeftNum = Format$(Left(S, Z - 1), "############################")
          Exit Function
        End If
      Next
    End If
  NextThTT
End Function[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Upon testing other data I've stumbled upon some fields that would not seem to work such as:
BAR PO-4500016742
BHP PO- 4504238970
3004544191

<tbody>
</tbody>

<tbody>
</tbody>

<tbody>
</tbody>
It is to do with with the length of those numbers. If you need the results to be numbers, try changing the first line of the function to
Rich (BB code):
Function LeftNum(s As String) As Double

If you are happy to have the results as text strings then
Rich (BB code):
Function LeftNum(s As String) As String
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,581
Members
449,089
Latest member
Motoracer88

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