Adding zeroes

ekhawaja

Board Regular
Joined
Dec 16, 2018
Messages
60
Office Version
  1. 365
Hello,

Looking for a formula that needs to ensure that after the first "-" or if there is no dash then after the letter commodity code, the number length should be 5 digits by adding leading zeroes. If there are two "-", second dash length should be changed to two by adding a single leading 0.

See below:

Thanks in advance!

10ZE-130710ZE-01307
05WR-1405WR-00014
05PL-105PL-00001
33UA85933UA00859
05PW85905PW00859
05PA-100205PA-01002
05WC-TT199-105WC-TT00199-01
05WC-TT20-0105WC-TT00020-01
05WC-PV1490-105WC-PV01490-01
05PL-1A05PL-00001A
05UA859A05UA00859A
33WC-AXL120J-133WC-AXL01320J-01



 
Maybe this.

Book1.xlsx
ABCD
1TextFormulaDesiredCheck
210ZE-130710ZE-0130710ZE-01307TRUE
305WR-1405WR-0001405WR-00014TRUE
405PL-105PL-0000105PL-00001TRUE
533UA85933UA0085933UA00859TRUE
605PW85905PW0085905PW00859TRUE
705PA-100205PA-0100205PA-01002TRUE
805WC-TT199-105WC-TT00199-0105WC-TT00199-01TRUE
905WC-TT20-0105WC-TT00020-0105WC-TT00020-01TRUE
1005WC-PV1490-105WC-PV01490-0105WC-PV01490-01TRUE
1105PL-1A05PL-00001A05PL-00001ATRUE
1205UA859A05UA00859A05UA00859ATRUE
1333WC-AXL120J-133WC-AXL00120J-0133WC-AXL00120J-01TRUE
Sheet2
Cell Formulas
RangeFormula
B2:B13B2=LET(a,TEXTBEFORE(A2,"-",,,,A2),b,IFERROR(CHOOSE(SUM(LEN(A2)-LEN(SUBSTITUTE(A2,"-","")))/LEN("-"),TEXTAFTER(A2,"-",1),TEXTBEFORE(TEXTAFTER(A2,"-"),"-")),""),c,TEXTAFTER(A2,"-",2,,,""),TEXTJOIN("-",TRUE,updateStr(a),TEXT(updateStr(b),"00000"),TEXT(c,"00")))
D2:D13D2=B2=C2
Lambda Functions
NameFormula
updateStr=LAMBDA(str,LET(firstChar,IFERROR(FIND(UNICHAR(SEQUENCE(1,26,65)),str),0), end,MAX(IFERROR(LET(alpha,UNICHAR(SEQUENCE(1,26,65)),FIND("@",SUBSTITUTE(str,alpha,"@",(LEN(str)-LEN(SUBSTITUTE(str,alpha,"")))/LEN(alpha)))),0)), start,IF(end=LEN(str),LARGE(UNIQUE(HSTACK(end,firstChar),1),2),end), noLastChar,LEFT(str,start)&TEXT(MID(str,start+1,10),"00000"), hasLastChar,LEFT(str,start)&TEXT(MID(str,start+1,LEN(str)-start-1),"00000")&RIGHT(str,1), IF(NOT(ISNUMBER(NUMBERVALUE(MID(str,LEN(str)-1,1)))),str,IF(end=start,noLastChar,hasLastChar))))
@Cubist thank you so much again!. The only time i am getting different result is when i have 3 letter string at the beginning:

05V-330 is turning out as 00005V-00330 (too many 0s at the front)
 
Upvote 0

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
With a UDF

VBA Code:
Function jec(c As String) As String
 Dim ar
 With CreateObject("vbscript.regexp")
   .Global = True
   .Pattern = "[A-Z-][0-9]+"
   Set ar = .Execute(c)
   If ar.Count = 1 Then
     jec = Replace(c, ar(0), Left(ar(0), 1) & Format(Mid(ar(0), 2), "00000"))
   Else
     jec = Replace(Replace(c, ar(1), Left(ar(1), 1) & Format(Mid(ar(1), 2), "00")), ar(0), Left(ar(0), 1) & Format(Mid(ar(0), 2), "00000"))
   End If
 End With
End Function

Book12.xlsm
ABCD
110ZE-130710ZE-0130710ZE-01307
205WR-1405WR-0001405WR-00014
305PL-105PL-0000105PL-00001
433UA85933UA0085933UA00859
505PW85905PW0085905PW00859
605PA-100205PA-0100205PA-01002
705WC-TT199-105WC-TT00199-0105WC-TT00199-01
805WC-TT20-0105WC-TT00020-0105WC-TT00020-01
905WC-PV1490-105WC-PV01490-0105WC-PV01490-01
1005PL-1A05PL-00001A05PL-00001A
1105UA859A05UA00859A05UA00859A
1233WC-AXL120J-133WC-AXL00120J-0133WC-AXL00120J-01
Sheet1
Cell Formulas
RangeFormula
D1:D12D1=jec(A1)
@JEC , this macro seems to be working with all criteria, thank you so much, the community here is complete genius!! =) @JEC @Cubist @dreid1011 . Made my work so much easier!
 
Upvote 0
I am glad you got a solution after I tried and realized it was well beyond my logic bending abilities. (After looking at the solutions, my head it still swirling).
 
Upvote 0
@Cubist thank you so much again!. The only time i am getting different result is when i have 3 letter string at the beginning:

05V-330 is turning out as 00005V-00330 (too many 0s at the front)
With a small adjustment, this should fix that. I prefer @JEC's solution over this, btw.

Book1
ABCD
1TextFormulaDesiredCheck
210ZE-130710ZE-0130710ZE-01307TRUE
305WR-1405WR-0001405WR-00014TRUE
405PL-105PL-0000105PL-00001TRUE
533UA85933UA0085933UA00859TRUE
605PW85905PW0085905PW00859TRUE
705PA-100205PA-0100205PA-01002TRUE
805WC-TT199-105WC-TT00199-0105WC-TT00199-01TRUE
905WC-TT20-0105WC-TT00020-0105WC-TT00020-01TRUE
1005WC-PV1490-105WC-PV01490-0105WC-PV01490-01TRUE
1105PL-1A05PL-00001A05PL-00001ATRUE
1205UA859A05UA00859A05UA00859ATRUE
1333WC-AXL120J-133WC-AXL00120J-0133WC-AXL00120J-01TRUE
1405V-33005V-00330
Sheet1
Cell Formulas
RangeFormula
D2:D13D2=B2=C2
B2:B14B2=LET(h,SUM(LEN(A2)-LEN(SUBSTITUTE(A2,"-","")))/LEN("-"),a,TEXTBEFORE(A2,"-",,,,A2),b,IFERROR(CHOOSE(h,TEXTAFTER(A2,"-",1),TEXTBEFORE(TEXTAFTER(A2,"-"),"-")),""),c,TEXTAFTER(A2,"-",2,,,""),TEXTJOIN("-",TRUE,IF(h>0,a,updateStr(a)),TEXT(updateStr(b),"00000"),TEXT(c,"00")))
Lambda Functions
NameFormula
updateStr=LAMBDA(str,LET(firstChar,IFERROR(FIND(UNICHAR(SEQUENCE(1,26,65)),str),0), end,MAX(IFERROR(LET(alpha,UNICHAR(SEQUENCE(1,26,65)),FIND("@",SUBSTITUTE(str,alpha,"@",(LEN(str)-LEN(SUBSTITUTE(str,alpha,"")))/LEN(alpha)))),0)), start,IF(end=LEN(str),LARGE(UNIQUE(HSTACK(end,firstChar),1),2),end), noLastChar,LEFT(str,start)&TEXT(MID(str,start+1,10),"00000"), hasLastChar,LEFT(str,start)&TEXT(MID(str,start+1,LEN(str)-start-1),"00000")&RIGHT(str,1), IF(NOT(ISNUMBER(NUMBERVALUE(MID(str,LEN(str)-1,1)))),str,IF(end=start,noLastChar,hasLastChar))))
 
Last edited:
Upvote 1
With a small adjustment, this should fix that. I prefer @JEC's solution over this, btw.

Book1
ABCD
1TextFormulaDesiredCheck
210ZE-130710ZE-0130710ZE-01307TRUE
305WR-1405WR-0001405WR-00014TRUE
405PL-105PL-0000105PL-00001TRUE
533UA85933UA0085933UA00859TRUE
605PW85905PW0085905PW00859TRUE
705PA-100205PA-0100205PA-01002TRUE
805WC-TT199-105WC-TT00199-0105WC-TT00199-01TRUE
905WC-TT20-0105WC-TT00020-0105WC-TT00020-01TRUE
1005WC-PV1490-105WC-PV01490-0105WC-PV01490-01TRUE
1105PL-1A05PL-00001A05PL-00001ATRUE
1205UA859A05UA00859A05UA00859ATRUE
1333WC-AXL120J-133WC-AXL00120J-0133WC-AXL00120J-01TRUE
1405V-33005V-00330
Sheet1
Cell Formulas
RangeFormula
D2:D13D2=B2=C2
B2:B14B2=LET(h,SUM(LEN(A2)-LEN(SUBSTITUTE(A2,"-","")))/LEN("-"),a,TEXTBEFORE(A2,"-",,,,A2),b,IFERROR(CHOOSE(h,TEXTAFTER(A2,"-",1),TEXTBEFORE(TEXTAFTER(A2,"-"),"-")),""),c,TEXTAFTER(A2,"-",2,,,""),TEXTJOIN("-",TRUE,IF(h>0,a,updateStr(a)),TEXT(updateStr(b),"00000"),TEXT(c,"00")))
Lambda Functions
NameFormula
updateStr=LAMBDA(str,LET(firstChar,IFERROR(FIND(UNICHAR(SEQUENCE(1,26,65)),str),0), end,MAX(IFERROR(LET(alpha,UNICHAR(SEQUENCE(1,26,65)),FIND("@",SUBSTITUTE(str,alpha,"@",(LEN(str)-LEN(SUBSTITUTE(str,alpha,"")))/LEN(alpha)))),0)), start,IF(end=LEN(str),LARGE(UNIQUE(HSTACK(end,firstChar),1),2),end), noLastChar,LEFT(str,start)&TEXT(MID(str,start+1,10),"00000"), hasLastChar,LEFT(str,start)&TEXT(MID(str,start+1,LEN(str)-start-1),"00000")&RIGHT(str,1), IF(NOT(ISNUMBER(NUMBERVALUE(MID(str,LEN(str)-1,1)))),str,IF(end=start,noLastChar,hasLastChar))))
this seems to be working as well!!
 
Upvote 0
@JEC , this macro seems to be working with all criteria, thank you so much, the community here is complete genius!! =) @JEC @Cubist @dreid1011 . Made my work so much easier!
The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.
 
Upvote 0

Forum statistics

Threads
1,215,106
Messages
6,123,124
Members
449,097
Latest member
mlckr

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