Fixing the names for sorting in Excel

sakrams

Board Regular
Joined
Sep 28, 2009
Messages
59
Office Version
  1. 2016
Platform
  1. Windows
Good Afternoon Excel Gurus,


Need help from the gurus with a quick formula to achieve the following result.


Column A has Names that I am trying to change to match the results as shown in column B.

AB
A1INSP-6INSP-00006
A2INSP-16INSP-00016
A3INSP-116INSP-00116
A4INSP-1116INSP-01116
A5INSP-11116INSP-11116
A6INSP-6-PTINSP-00006-PT
A7INSP-16-PTINSP-00016-PT
A8INSP-116-PTINSP-00116-PT
A9INSP-1116-PTINSP-01116-PT
A10INSP-11116-PTINSP-11116-PT

<tbody>
</tbody>
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Here's one way, maybe not the best way, but works for the sample data provided.

=SUBSTITUTE(A1,"-","-"&REPT("0",10+((RIGHT(A1,2)="PT")*3)-LEN(A1)),1)

I wonder if there are more nuances to this than you have described in your OP ?
 
Upvote 0
Solution
Or perhaps a much longer array formula ;)

Code:
=SUBSTITUTE(A1,
TEXTJOIN("",TRUE,IFERROR((MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)*1),"")),
TEXT(TEXTJOIN("",TRUE,IFERROR((MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)*1),"")),
"00000"))

To enter an array formula commit with {CTRL}{SHIFT}{ENTER}
The formula will look like this (but it will not work if you try to enter { } manually!)
{=SUBSTITUTE(A1,
TEXTJOIN("",TRUE,IFERROR((MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)*1),"")),
TEXT(TEXTJOIN("",TRUE,IFERROR((MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)*1),"")),
"00000"))}

Excel 2016 (Windows) 32 bit
A
B
1
INSP-6INSP-00006
2
INSP-16INSP-00016
3
INSP-116INSP-00116
4
INSP-1116INSP-01116
5
INSP-11116INSP-11116
6
INSP-6-PTINSP-00006-PT
7
INSP-16-PTINSP-00016-PT
8
INSP-116-PTINSP-00116-PT
9
INSP-1116-PTINSP-01116-PT
10
INSP-11116-PTINSP-11116-PT
Sheet: Sheet1
 
Last edited:
Upvote 0
Brilliant. This worked like a charm. Thanks a million Gerald. Will save me a lot of time.

Here's one way, maybe not the best way, but works for the sample data provided.

=SUBSTITUTE(A1,"-","-"&REPT("0",10+((RIGHT(A1,2)="PT")*3)-LEN(A1)),1)

I wonder if there are more nuances to this than you have described in your OP ?
 
Upvote 0
Tried this and committed with {CTRL}{SHIFT}{ENTER}, however, i am getting an error. I am using Excel 2010, could that be the reason?

Thanks a million for your reply.

Or perhaps a much longer array formula ;)

Code:
=SUBSTITUTE(A1,
TEXTJOIN("",TRUE,IFERROR((MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)*1),"")),
TEXT(TEXTJOIN("",TRUE,IFERROR((MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)*1),"")),
"00000"))

To enter an array formula commit with {CTRL}{SHIFT}{ENTER}
The formula will look like this (but it will not work if you try to enter { } manually!)
{=SUBSTITUTE(A1,
TEXTJOIN("",TRUE,IFERROR((MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)*1),"")),
TEXT(TEXTJOIN("",TRUE,IFERROR((MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)*1),"")),
"00000"))}

Excel 2016 (Windows) 32 bit
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]A[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]B[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]1[/COLOR]​
INSP-6INSP-00006
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]2[/COLOR]​
INSP-16INSP-00016
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]3[/COLOR]​
INSP-116INSP-00116
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]4[/COLOR]​
INSP-1116INSP-01116
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]5[/COLOR]​
INSP-11116INSP-11116
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]6[/COLOR]​
INSP-6-PTINSP-00006-PT
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]7[/COLOR]​
INSP-16-PTINSP-00016-PT
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]8[/COLOR]​
INSP-116-PTINSP-00116-PT
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]9[/COLOR]​
INSP-1116-PTINSP-01116-PT
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]10[/COLOR]​
INSP-11116-PTINSP-11116-PT

<tbody>
</tbody>
Sheet: Sheet1

<tbody>
</tbody>
 
Upvote 0
i am getting an error. I am using Excel 2010, could that be the reason?

Yes - TEXTJOIN is a recent function :oops:
 
Last edited:
Upvote 0
Hi Yongle - I don't have access to TEXTJOIN and I'm curious . . . what does it do ?

Maybe we can do something similar with some other function(s) ?
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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