Jade
Jade

Reputation: 77

Add leading 0 after the dash (-) but before the numbers

I have a formula that adds a 0 before the numbers 1-9 if they don't have one.

Old..................New

D-8..................D-08

FE-09..............FE-09

I-18..................I-18

P-1..................P-01

FG-08A...........FG-08

=LEFT(A1,FIND("-",A1))&TEXT(MID(A1,FIND("-",A1)+1,2),"00")

However, for values like FG-08A, I do not want it to clear the A at the end. So if it's FG-08A, the result would be FG-08A (remain unchanged). If it's FG-8A, it would be FG-08A (adds the leading 0 but keeps the "A").

Upvotes: 0

Views: 337

Answers (2)

cybernetic.nomad
cybernetic.nomad

Reputation: 6418

As stated in my comment, if you never have more than two consecutive numbers and if these will never be 00, you can use:

=SUBSTITUTE(SUBSTITUTE(A1,"-","-0"),"00","0")

Upvotes: 1

Scott Craner
Scott Craner

Reputation: 152605

Add check for the ending letter:

=LEFT(A1,FIND("-",A1))&TEXT(MID(A1,FIND("-",A1)+1,ISNUMBER(--MID(A1,FIND("-",A1)+1,2))+1),"00")&IF(ISERROR(--RIGHT(A1)),RIGHT(A1),"")

enter image description here

Upvotes: 4

Related Questions