Zero Padding a value for sorting purposes

CripZZ23

New Member
Joined
Dec 22, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I have a field that will have a format similar to the following:

AA-1
AA-2
AA-10
AA-1 (1)
AA-10 (1)
AA-2 (12)
Etc.

Essentially, it is 2 Letters, a -, then a number (1 or 2 digits). Optionally there is a space following by 1 or 2 digits inside of ( ).

I would like to add a field that calculates a sort order based on the left value - similar to the following:
ValueSort Value
AA-1AA-01
AA-2AA-02
AA-10AA-10
AA-1 (1)AA-01 (01)
AA-10 (1)AA-10 (01)
AA-2 (12)AA-02 (12)


We want to be able to sort the column. Any assistance would be appreciated.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Welcome to the Board!

For a value in A1, you can use this formula to create your Sort value:
Excel Formula:
=IF(OR(LEN(A1)=4,MID(A1,5,1)=" "),LEFT(A1,3) & "0" &RIGHT(A1,LEN(A1)-3),A1)
 
Upvote 0
Thanks for the help! Does this add logic to pad the value inside the ( ) as well? It works for the -1 to -01, but it doesn't seem to work for the (1) to (01).
 
Upvote 0
Sorry, I missed that part. Makes it a bit more complex, but try this:
Excel Formula:
=LET(x,IF(OR(LEN(A1)=4,MID(A1,5,1)=" "),LEFT(A1,3) & "0" &RIGHT(A1,LEN(A1)-3),A1),IF(ISNUMBER(FIND("(",x)),IF(FIND(")",x)-FIND("(",x)=2,SUBSTITUTE(x,"(","(0"),x),x))

And here is the proof!
1703252755650.png
 
Upvote 0
Solution
=LET(x,IFERROR(REPLACE(A1,SEARCH("-? ",A1&" ")+1,0,"0"),A1),IFERROR(REPLACE(x,SEARCH("(?)",x)+1,0,"0"),x))

Book1
AB
1AA-1AA-01
2AA-2AA-02
3AA-10AA-10
4AA-1 (1)AA-01 (01)
5AA-10 (1)AA-10 (01)
6AA-2 (12)AA-02 (12)
Sheet1
Cell Formulas
RangeFormula
B1:B6B1=LET(x,IFERROR(REPLACE(A1,SEARCH("-? ",A1&" ")+1,0,"0"),A1),IFERROR(REPLACE(x,SEARCH("(?)",x)+1,0,"0"),x))
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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