Advanced LEFT/RIGHT/REPLACE function for formatting

sheller

New Member
Joined
Dec 12, 2016
Messages
22
Hi everyone,
I am trying to standardize items listed in different formats (people didn't enter consistently) and can't figure out an easy way to use a function to obtain the correct formatting. See screen shot below with entry examples (column A) and needed output (column B). Is there any way to enter a function(s) to achieve outcome listed in column B (I manually entered for sake of this question)? Thanks much in advance!

COL A COL B

Iterations Needed Format: P-XXXX-A (unless different letter listed in coulmn A)
1234 P-1234-A
P1235 P-1235-A
1236-A P-1236-A
1236-B P-1236-B
1237 P-1237-A
P1237C P-1237-C
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Maybe:

AB
11234P-1234-A
2P1235P-1235-A
31236-AP-1236-A
41236-BP-1236-B
51237P-1237-A
6P1237CP-1237-C
7555P-555-A
8788GP-788-G
9P-66666-IP-66666-I
10Q33asdf-XP-33-X
11P-0-

<colgroup><col style="width: 25pxpx"><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet6

Array Formulas
CellFormula
B1{="P-"&MAX(IFERROR(MID(A1,{1,2,3},{1;2;3;4;5;6;7;8;9})+0,0))&"-"&IF(ISERROR(RIGHT(A1)+0),RIGHT(A1),"A")}

<thead>
</thead><tbody>
</tbody>
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself

<tbody>
</tbody>





This assumes the number will start no later than the 3rd position, and isn't longer than 9 digits.
 
Upvote 0
It worked! Thank you very much, Eric W!! What a huge help. :)


Maybe:

AB
11234P-1234-A
2P1235P-1235-A
31236-AP-1236-A
41236-BP-1236-B
51237P-1237-A
6P1237CP-1237-C
7555P-555-A
8788GP-788-G
9P-66666-IP-66666-I
10Q33asdf-XP-33-X
11P-0-

<tbody>
</tbody>
Sheet6

Array Formulas
CellFormula
B1{="P-"&MAX(IFERROR(MID(A1,{1,2,3},{1;2;3;4;5;6;7;8;9})+0,0))&"-"&IF(ISERROR(RIGHT(A1)+0),RIGHT(A1),"A")}

<tbody>
</tbody>
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself

<tbody>
</tbody>





This assumes the number will start no later than the 3rd position, and isn't longer than 9 digits.
 
Upvote 0
Maybe:

AB
11234P-1234-A
2P1235P-1235-A
31236-AP-1236-A
41236-BP-1236-B
51237P-1237-A
6P1237CP-1237-C
7555P-555-A
8788GP-788-G
9P-66666-IP-66666-I
10Q33asdf-XP-33-X
11P-0-

<tbody>
</tbody>
Sheet6

Array Formulas
CellFormula
B1{="P-"&MAX(IFERROR(MID(A1,{1,2,3},{1;2;3;4;5;6;7;8;9})+0,0))&"-"&IF(ISERROR(RIGHT(A1)+0),RIGHT(A1),"A")}

<tbody>
</tbody>
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself

<tbody>
</tbody>
If we assume the example value you put in cell A10 is not one the OP has to handle (because his posted examples did not even hint at that shaped value), then this normally-entered formula will also work...

=IFERROR("P-"&MID(IF(ISNUMBER(-RIGHT(A1)),A1&"-A",IF(MID(A1,LEN(A1)-1,1)="-",A1,REPLACE(A1,LEN(A1),0,"-"))),MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")),99),"")
 
Upvote 0
If we assume the example value you put in cell A10 is not one the OP has to handle (because his posted examples did not even hint at that shaped value), then this normally-entered formula will also work...

=IFERROR("P-"&MID(IF(ISNUMBER(-RIGHT(A1)),A1&"-A",IF(MID(A1,LEN(A1)-1,1)="-",A1,REPLACE(A1,LEN(A1),0,"-"))),MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")),99),"")

Hi RIck,
Thanks very much! I love being able to use a 'normally-entered' formula such as yours above.
 
Upvote 0

Forum statistics

Threads
1,216,444
Messages
6,130,659
Members
449,585
Latest member
Nattarinee

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