Custom Cell Format

AlexMihai

New Member
Joined
Jul 28, 2020
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Hello! The data i want to be displayed have diferent formats eg. "S-001" or "S-001.1" or " S-001/A" or "S-001.1/A".The "S-" part i want to be a prefix that will be added to text no matter what comes after it.Also the number that fallows the prefix should be 3 digits(if cell is filled with "1", excell should return "S-001").I have managed to do it with a custom format cells that looks like this : "S-"00#. But if cell is filled with "1.1", or "1/A", excell wont recognize it and it will return "S-001" instead of "S-001.1" or "S-001/A".Also if i fill cell with "1" i dont want the data to be displayed as "S-001.0".Is it posible to aply a single format to all the cells and my data to be displayed corectly?Thank you!
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Welcome to the forum
Provide several examples of typical sample data with raw data in column A and expected results in column B

XL2BB is a downloadable tool which allows you to post sample data for us
 
Upvote 0
Book1
AB
1Raw dataExpected results
21S-001
31.1S-001.1
41/AS-001/A
51.1/BS-001.1/B
6234.1S-234.1
7234/BS-234/B
8234.1/BS-234.1/B
Sheet1


Thank you!
 
Upvote 0
I cannot think of a simple way to achieve what you want
Are you prepared to use VBA ?
 
Upvote 0
Let's wait 24 hours to see if someone comes along and finds a non-VBA solution for you

If not I will post VBA solution later tomorrow
 
Upvote 0
Greetings
I Hobe to be helpful please feed me back

Note that the formula needs to be confirmed with CONTROL+SHIFT+ENTER.

VBA Code:
 =SUBSTITUTE(A2,--MID(A2,MIN(IFERROR(FIND({0,1,2,3,4,5,6,7,8,9,".",","},A2),"")),COUNT(--MID(SUBSTITUTE(SUBSTITUTE(A2,".",0),",",0),ROW($1:$99),1))),TEXT(--MID(A2,MIN(IFERROR(FIND({0,1,2,3,4,5,6,7,8,9,".",","},A2),"")),COUNT(--MID(SUBSTITUTE(SUBSTITUTE(A2,".",0),",",0),ROW($1:$99),1))),"""S-""000.##"))

Book1
AB
1Raw dataExpected results
21.2bS-001.2b
31.2S-001.2
41/AS-001./A
51.1/BS-001.1/B
6234.1S-234.1
7234/BS-234./B
8234.1/BS-234.1/B
9
10
11
12
13
Sheet1
Cell Formulas
RangeFormula
B2:B8B2=SUBSTITUTE(A2,--MID(A2,MIN(IFERROR(FIND({0;1;2;3;4;5;6;7;8;9;".";";"},A2),"")),COUNT(--MID(SUBSTITUTE(SUBSTITUTE(A2,".",0),";",0),ROW($1:$99),1))),TEXT(--MID(A2,MIN(IFERROR(FIND({0;1;2;3;4;5;6;7;8;9;".";";"},A2),"")),COUNT(--MID(SUBSTITUTE(SUBSTITUTE(A2,".",0),";",0),ROW($1:$99),1))),"""S-""000.##"))
Press CTRL+SHIFT+ENTER to enter array formulas.



Below For Other Regions Language using ;
VBA Code:
=SUBSTITUTE(A2;--MID(A2;MIN(IFERROR(FIND({0;1;2;3;4;5;6;7;8;9;".";";"};A2);""));COUNT(--MID(SUBSTITUTE(SUBSTITUTE(A2;".";0);";";0);ROW($1:$99);1)));TEXT(--MID(A2;MIN(IFERROR(FIND({0;1;2;3;4;5;6;7;8;9;".";";"};A2);""));COUNT(--MID(SUBSTITUTE(SUBSTITUTE(A2;".";0);";";0);ROW($1:$99);1)));"""S-""000.##"))
 

Attachments

  • C S E.gif
    C S E.gif
    38.6 KB · Views: 3
Upvote 0
Greetings
I Hobe to be helpful please feed me back

Note that the formula needs to be confirmed with CONTROL+SHIFT+ENTER.
I think OP doesn't want to change the underlying value of each cell, just displaying them in the desired format. As yongle pointed out, there is no one custom format that would do it. I think it takes three custom formats to do what the OP wants.
 
Upvote 0
@AlexMihai
Does the formula provided by @Dossfm0q achieve what you require ?

If not then ....
I cannot see any way to do this by simple formatting
VBA would replace the raw data with required results (either by overwrite or in an adjacent column)

Please let us know how you want to proceed
 
Upvote 0
VBA can do it without altering the underlying data.

I copied Col A to C, applied the VBA code to C, copied C and pasted by value to D, just to show that the underlying data is intact.

The VBA code, however, doesn't save much effort. If any of the following codes can be applied to a large area, say, C2:C100, then it would save some effort. If, however, every row has its own format, then VBA doesn't save much effort because you have to code for every row. That's not much different than setting custom format by hand.

VBA Code:
Sub num_format()
Range("C2").NumberFormat = """S-""000;;;"
Range("C3:C6").NumberFormat = """S-""000.0;;;""S-00""@"
Range("C7:C8").NumberFormat = ";;;""S-""@"
End Sub

Doc.xlsm
ABCD
1Raw dataExpected resultsCopied from A, after VBACopy C and paste to D by value
21S-001S-0011
31.1S-001.1S-001.11.1
41/AS-001/AS-001/A1/A
51.1/BS-001.1/BS-001.1/B1.1/B
6234.1S-234.1S-234.1234.1
7234/BS-234/BS-234/B234/B
8234.1/BS-234.1/BS-234.1/B234.1/B
Sheet2
 
Upvote 0

Forum statistics

Threads
1,214,981
Messages
6,122,565
Members
449,089
Latest member
Motoracer88

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