Add zero to beginning of number extraction

SaraWitch

Active Member
Joined
Sep 29, 2015
Messages
322
Office Version
  1. 365
Platform
  1. Windows
Hello peeps,

I have a formula to extract the first number from the previous cell and would like to add a "0" in formula if one hasn't been entered previously.
¦ MrExcel Queries.xlsm
ABC
1Contact NumberNumberText
201111 11111101111 11111
301111 111111 (sister's number)01111 11111(sister's number)
411111111111111111111
Sheet3
Cell Formulas
RangeFormula
B2:B4B2=LEFT(A2,SUM(LEN(A2)-LEN(SUBSTITUTE(A2,{"0","1","2","3","4","5","6","7","8","9"},""))))

Any help would be appreciated :)
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
try this formula:

Excel Formula:
=IFERROR(IF(NUMBERVALUE(LEFT(A2))<>0;"0"&LEFT(A2;SUM(LEN(A2)-LEN(SUBSTITUTE(A2;{"0","1","2","3","4","5","6","7","8","9"};""))));LEFT(A2;SUM(LEN(A2)-LEN(SUBSTITUTE(A2;{"0","1","2","3","4","5","6","7","8","9"};"")))));A2)
 
Upvote 1
Lovely! Thank you, @PeteWright! :biggrin: I just replaced the semicolons with a comma and it works perfectly - just wondering if there is a way for the custom format (0#### ######) to apply?
¦ MrExcel Queries.xlsm
ABCD
1Contact NumberNumberWould like!Text
201111 11111101111 1111101111 111111
301111 111111 (sister's number)01111 1111101111 111111(sister's number)
411111111110111111111101111 111111
Sheet3
Cell Formulas
RangeFormula
B2:B4B2=IFERROR(IF(NUMBERVALUE(LEFT(A2))<>0,"0"&LEFT(A2,SUM(LEN(A2)-LEN(SUBSTITUTE(A2,{"0","1","2","3","4","5","6","7","8","9"},"")))),LEFT(A2,SUM(LEN(A2)-LEN(SUBSTITUTE(A2,{"0","1","2","3","4","5","6","7","8","9"},""))))),A2)
 
Upvote 0
I've got a workaround of pasting values in another column and then using Text to Columns, which does work. Would be nice to know if there is a way to do this in the formula column though... :unsure:
 
Upvote 0
@SaraWitch just wrap it in a text function

Book1
ABC
1Contact NumberNumberWould like!
201111 11111101111 1111101111 11111
301111 111111 (sister's number)01111 1111101111 11111
411111111110111111111101111 111111
Sheet1
Cell Formulas
RangeFormula
B2:B4B2=IFERROR(IF(NUMBERVALUE(LEFT(A2))<>0,"0"&LEFT(A2,SUM(LEN(A2)-LEN(SUBSTITUTE(A2,{"0","1","2","3","4","5","6","7","8","9"},"")))),LEFT(A2,SUM(LEN(A2)-LEN(SUBSTITUTE(A2,{"0","1","2","3","4","5","6","7","8","9"},""))))),A2)
C2:C4C2=TEXT(IFERROR(IF(NUMBERVALUE(LEFT(A2))<>0,"0"&LEFT(A2,SUM(LEN(A2)-LEN(SUBSTITUTE(A2,{"0","1","2","3","4","5","6","7","8","9"},"")))),LEFT(A2,SUM(LEN(A2)-LEN(SUBSTITUTE(A2,{"0","1","2","3","4","5","6","7","8","9"},""))))),A2),"0#### ######")


Although it appears that in your last post you aren't removing a character in column C , are you sure that is what you wanted (as the formula above is working with the same result as in column B that you stated is perfect)?
 
Last edited:
Upvote 1
I kind of think there is supposed to be 1 less 1 in that last example. If so, I have a much simpler formula that looks like it does what you want it to do.

=TEXT(MAX(--MID(SUBSTITUTE(A2," ",""),1,SEQUENCE(10))),"00000 00000")

Book1
AB
1Contact Number
201111 11111101111 11111
301111 111111 (sister's number)01111 11111
411111111101111 11111
556700000 00567
Sheet1
Cell Formulas
RangeFormula
B2:B5B2=TEXT(MAX(--MID(SUBSTITUTE(A2," ",""),1,SEQUENCE(10))),"00000 00000")
 
Upvote 0
Solution
Thank you, all. After changing the numbers slightly to make it easier to view, I notice that @Scott Huish solution works best (there should be 11 numbers, so I just updated accordingly):
¦ MrExcel Queries.xlsm
ABCDEFG
1Contact NumberNumberWould like!Text@PeteWright@MARK858@Scott Huish
201111 22233301111 2223301111 22233301111 2223301111 2223301111 222333
301111 222333 (sister's number)01111 2223301111 222333(sister's number)01111 2223301111 2223301111 222333
41111222333111122233301111 2223330111122233301111 22233301111 222333
Sheet3
Cell Formulas
RangeFormula
E2:E4E2=IFERROR(IF(NUMBERVALUE(LEFT(A2))<>0,"0"&LEFT(A2,SUM(LEN(A2)-LEN(SUBSTITUTE(A2,{"0","1","2","3","4","5","6","7","8","9"},"")))),LEFT(A2,SUM(LEN(A2)-LEN(SUBSTITUTE(A2,{"0","1","2","3","4","5","6","7","8","9"},""))))),A2)
F2:F4F2=TEXT(IFERROR(IF(NUMBERVALUE(LEFT(A2))<>0,"0"&LEFT(A2,SUM(LEN(A2)-LEN(SUBSTITUTE(A2,{"0","1","2","3","4","5","6","7","8","9"},"")))),LEFT(A2,SUM(LEN(A2)-LEN(SUBSTITUTE(A2,{"0","1","2","3","4","5","6","7","8","9"},""))))),A2),"0#### ######")
G2:G4G2=TEXT(MAX(--MID(SUBSTITUTE(A2," ",""),1,SEQUENCE(11))),"00000 000000")
B2:B4B2=LEFT(A2,SUM(LEN(A2)-LEN(SUBSTITUTE(A2,{"0","1","2","3","4","5","6","7","8","9"},""))))

Thank you all so much for your help and have a lovely weekend! :biggrin:
 
Upvote 0
Just been having a play with the other formulas, and they all work (except for one CF)! So an extra big thanks to you all! :biggrin:
¦ MrExcel Queries.xlsm
ABCDEFG
1Contact NumberNumberWould like!Text@PeteWright@MARK858@Scott Huish
201111 22233301111 22233301111 22233301111 22233301111 222333
301111 222333 (sister's number)01111 222333(sister's number)01111 22233301111 22233301111 222333
41111222333111122233301111 2223330111122233301111 22233301111 222333
Extract number
Cell Formulas
RangeFormula
E2:E4E2=IFERROR(IF(NUMBERVALUE(LEFT(A2))<>0,"0"&LEFT(A2,SUM(LEN(A2)-LEN(SUBSTITUTE(A2,{"0","1","2","3","4","5","6","7","8","9","0"},"")))),LEFT(A2,SUM(LEN(A2)-LEN(SUBSTITUTE(A2,{"0","1","2","3","4","5","6","7","8","9","0"},""))))),A2)
F2:F4F2=TEXT(IFERROR(IF(NUMBERVALUE(LEFT(A2))<>0,"0"&LEFT(A2,SUM(LEN(A2)-LEN(SUBSTITUTE(A2,{"0","1","2","3","4","5","6","7","8","9","0"},"")))),LEFT(A2,SUM(LEN(A2)-LEN(SUBSTITUTE(A2,{"0","1","2","3","4","5","6","7","8","9","0"},""))))),A2),"0#### ######")
G2:G4G2=IFERROR(TEXT(MAX(--MID(SUBSTITUTE(A2," ",""),1,SEQUENCE(11))),"00000 000000"),"")
B2:B4B2=LEFT(A2,SUM(LEN(A2)-LEN(SUBSTITUTE(A2,{"0","1","2","3","4","5","6","7","8","9","0"},""))))

Wish I could mark you all as the solution, but will do @MARK858 because he got there first (even though I'm going to use @Scott Huish! ;) )...
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,977
Members
449,095
Latest member
Mr Hughes

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