Formula Extract Characters at the Beginning of a String

gonesalsa

New Member
Joined
May 5, 2014
Messages
7
Hello
I need to extract the numbers at the beginning of a string. The problem is that there could 1, 2 or 3 digits. Here are examples of the string:

4JXXXXXXXXX1204
10EXXXXXXXXX1204
127JXXXXXXXXX1204

I only want to extract the 4, 10 and the 127 in the above strings. I tried =LEFT(B1,LEN(B1)-14), but the formula isn't working on the last string where the number is 3 digits. it only pulls 12, instead of 127.

Thanks
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hi
Welcome to the board

Your formula works for me too. If you always have 14 characters after the digits your formula seems OK.
 
Upvote 0
Hi,
If not always 14 characters after number try this
LOOKUP(2,1/--LEFT(A1,ROW($1:$3)),--LEFT(A1,ROW($1:$3)))
or if your string can begin with letter
=IFERROR(LOOKUP(2,1/--LEFT(A1,ROW($1:$3)),--LEFT(A1,ROW($1:$3))),"")
 
Upvote 0
Thanks billszysz. That works. Can you explain what the first formula is doing? I want understand the formula, instead of blindly using it.
 
Upvote 0
If your string starts with a number of up to 4 digits, try:

=-LOOKUP(1,-LEFT(A1,{1,2,3,4}))
 
Upvote 0
gonesalsa,
My english is to bad to do this (it is not na native language) So this is a little tricky.
ok... your second record is 10EXXXXXXXXX1204 so value from --LEFT(A2,ROW($1:$3)) are {1,10,#ARG!}

1/--LEFT(A2,ROW($1:$3)) gives us {1,0.1,#ARG!}

Lookup is searching 2 in vector above (but 2 is never there) and errors are ommited. If Lookup don't find 2 (always) then give us the last value in vector (if exist) - in this case 0.1
And second vector --LEFT(A2,ROW($1:$3)) has at the same position value 10 {1,10,#ARG!} so lookup give us 10

I don't know if my explanation is undertandable :) but i can't better...so sorry if it is insufficient :((

You can replace ROW($1:$3) by array constant {1,2,3} - if you want more than 3 digits you can simply change this array (i.e for 5 digits {1,2,3,4,5})
 
Upvote 0
We should be careful in case of more than 3 digit at the beginning.
Why?.... Try this string 2SEPXXXXX3210 :)
 
Upvote 0
We should be careful in case of more than 3 digit at the beginning.
Why?.... Try this string 2SEPXXXXX3210 :)

Hi Bill

You are right. This formula tries to convert a string to a number. 2SEP is some languages will be interpreted as 2 September and will therefore give you a wrong result.
Notice that it does not even have to be more than 3, 3 is enough. Try 1E2ABC.

Conclusion: these are simple formula that will usually work for most cases.
If you want to be sure that you're really getting the digits at the beginning of a string you have to test them one by one, not in bulk like with Left().
 
Upvote 0

Forum statistics

Threads
1,214,790
Messages
6,121,607
Members
449,037
Latest member
Arbind kumar

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