Sub topic 6
Custom Format Codes and Digit Placeholders
This chapter assumes you can open Format Cells, you know that a cell's stored value differs from what it displays, and you have used the built-in number formats. Here you will write your own format codes. You will learn what each placeholder does, how literal text and spacing work, and where custom codes fail.
What a custom format code is
A number format is a short string of symbols. Excel reads it from left to right and uses it to turn a stored number into display text. The stored value never changes. You can see this by selecting the cell and reading the formula bar.
To enter a custom code, select the cells and press Ctrl+1. On the Number tab, choose Custom, and type the code in the Type box. The list below the box holds the built-in codes and any custom codes you have saved in the workbook. Picking an entry from it is a quick way to start from a working code and edit it.
Digit placeholders
Three characters stand for digits. They differ in what they do when the number has fewer digits than the code asks for.
| Placeholder | Behavior when the number has no digit for that position |
|---|---|
| `0` | Shows a zero |
| `#` | Shows nothing |
| `?` | Shows a space, which keeps decimal points and fractions aligned |
The table below shows how the same values display under different codes.
| Stored value | Code | Displayed |
|---|---|---|
| 42 | `00000` | 00042 |
| 42 | `#####` | 42 |
| 3.5 | `0.00` | 3.50 |
| 3.5 | `#.##` | 3.5 |
| 0.5 | `#.##` | .5 |
| 0.5 | `0.##` | 0.5 |
| 3 | `#.##` | 3. |
The last row catches many people. The code #.## always prints the decimal point, so a whole number ends with a dangling period. Use 0.## or General if that looks wrong.
The code from the Topic Spine pads to five digits:
00000The value 42 shows as 00042, but the cell still holds 42. A formula such as =A1+1 returns 43, not 00043.
Extra digits and rounding
If a number has more digits to the right of the decimal point than the code has placeholders, Excel rounds the display. The stored value keeps every digit. If the number has more digits to the left than the code has placeholders, Excel shows all of them. A code never truncates the integer part.
So the code 0.00 displays 2.678 as 2.68 and 1234.5 as 1234.50. Chapter content on formatting versus rounding covers why totals can then appear to be off by a cent.
The thousands separator
A comma between digit placeholders turns on the thousands separator. It needs only one comma, placed between any two placeholders.
#,##0shows 1234567 as 1,234,567 and 0 as 0.#,###shows 1234567 as 1,234,567 but shows 0 as an empty cell.
The 0 as the last integer placeholder is the usual choice. It makes sure that zero is visible.
A comma placed after the last digit placeholder has a different job. It scales the value by 1,000 for each comma, which a later chapter covers in detail. The comma's position decides which behavior you get.
Literal text and special characters
Anything in the code that is not a format symbol must be marked as literal text. There are three ways to do this.
- Wrap the text in double quotes:
0.0" kg"shows 12.5 as 12.5 kg. - Put a backslash before a single character:
0.0\k\ggives the same result. - Use one of the characters Excel displays without quoting.
Excel shows these characters as they are, with no quotes: $ + - ( ) : ^ ' { } < > = / ! and the space. Letters usually need quotes because many of them are format codes. A bare m, d, h, s, e, or E is interpreted as a date, time, or scientific symbol. Excel often adds the quotes or backslashes for you when you press OK, but it does not always guess correctly, so quote letters yourself.
A short list of other symbols:
| Symbol | Meaning |
|---|---|
| `_x` | Leaves a space as wide as the character x. `_)` reserves room for a closing parenthesis. |
| `*x` | Repeats the character x to fill the rest of the cell width. |
| `@` | Stands for the cell's text. |
| `%` | Multiplies the value by 100 and shows a percent sign. |
The _ and * symbols are how the Accounting format aligns its currency symbol. A later chapter on currency uses them.
Scaling by a comma
The Topic Spine example scales a value:
0.0,,"M"Each trailing comma divides the displayed value by 1,000. Two commas divide by one million, so 1234567 shows as 1.2M. The stored value stays 1234567. A chart or formula that uses the cell still sees the full number.
Sections
A format code can have up to four sections, separated by semicolons. The order is fixed: positive, negative, zero, text.
#,##0.00;[Red]-#,##0.00;0.00;@This code shows positive numbers with a thousands separator, negative numbers in red with a minus sign, zero as 0.00, and text unchanged. The number of sections changes how Excel reads them.
| Sections given | How Excel applies them |
|---|---|
| One | Applies to all numbers. Negative numbers get an automatic minus sign. |
| Two | First is positive and zero. Second is negative. |
| Three | Positive, negative, zero. |
| Four | Positive, negative, zero, text. |
Two details cause errors.
- In the negative section, Excel does not add a minus sign for you. If you write
0.00;0.00, a negative number displays without any sign. Add-yourself, or use parentheses. - If you leave a section empty, the values for that section are hidden. The code
0;-0;;@hides zeros. The code;;;hides everything, including text.
Hidden is not deleted. The value still shows in the formula bar and still counts in formulas.
Colors and conditions
You can place a color name in square brackets at the start of a section. Excel accepts Black, Blue, Cyan, Green, Magenta, Red, White, and Yellow, plus [Color n] where n is from 1 to 56.
You can also replace the default positive, negative, and zero tests with conditions in brackets, such as [>=1000]. With conditions, you may write at most two, and the remaining section covers everything else.
[Red][<0]0.0;[Blue][>100]0;0.00Excel tests the first condition, then the second, and then uses the last section for everything that failed both. A mismatch between conditions and sections is a frequent cause of rejected codes.
How Excel processes a code
Common errors
- Zeros disappear or the code is rejected. Check that quotes are paired and that letters that are format symbols are quoted.
- A dangling decimal point. Replace
#.##with0.##, or use#,##0.##only if the trailing period is acceptable. - Negatives lose their sign. Add a
-to the second section. - Leading zeros do not survive typing. A code of
00000only affects a number. If the cell holds text, such as "00042", the code does not apply to it. Use the text format when you need to keep identifiers as typed. - Pasting changes the format. Pasted values bring their own formats. Use Paste Special, then Values, to keep the destination's format.
- Code works but a formula result looks wrong. A format does not round. Use
=ROUND(A1,2)when you need the stored value to change.
=ROUND(A1,2)Exercises
Exercise
A cell holds 7. You want it to display as ID-0007 while the cell still holds the number 7. Write the custom format code.
Show answer
"ID-"0000. The quoted text prints literally, and four zeros pad the number to four digits. ID-0007 is shown, and the stored value stays 7. Writing ID\-0000 also works.
Exercise
You apply the code 0.00;0.00;0.00 to a cell that holds -12.5. What does it display, and how do you change the code so that the sign shows?
Show answer
It displays 12.50, because Excel does not add a minus sign in a custom negative section. Change the second section to -0.00, giving 0.00;-0.00;0.00.
Exercise
The stored value is 2500000. What does the code 0.0,,"M" display, and what does =A1*2 return if the value is in A1?
Show answer
It displays 2.5M. The formula returns 5000000, because the format changes only the display.