Sub topic 6
Custom format codes and digit placeholders
This chapter assumes you can open Format Cells and apply a built-in number format. It also assumes you know that a format changes only how a value looks, not the value itself.
Where custom codes live
Select the cells and press Ctrl+1. On the Number tab, choose Custom. Type a code in the Type box, and the Sample line previews the result. Excel saves the code in the workbook so you can reuse it.
A custom code is a pattern. Each digit of the value is poured into the placeholders in the pattern, and any other characters are copied through as they are.
The three digit placeholders
| Placeholder | Behavior when the value has no digit for that position |
|---|---|
| `0` | Shows a zero |
| `#` | Shows nothing |
| `?` | Shows a space, so decimal points line up in a column |
This code pads a number to five digits:
00000The value 42 displays as 00042. The stored value is still 42, so formulas and sorting are unaffected.
Digits to the left of the decimal point are never cut off. If the value has more digits than the code has placeholders, Excel shows all of them. Digits to the right of the decimal point are rounded for display, as the next table shows.
| Code | Value | Displays |
|---|---|---|
| `0.00` | 3.1 | 3.10 |
| `#.##` | 3.1 | 3.1 |
| `#.##` | 3 | 3. |
| `0.00` | 2.456 | 2.46 |
| `#,##0` | 1234567 | 1,234,567 |
The third row is a common surprise. With #.##, a whole number still shows the decimal point. Use 0.## or #,##0.## if you want to avoid a dangling point, though 3 then shows as 3.. The cleanest fix for mixed values is the General format.
Thousands separators
A comma between placeholders turns on grouping. The pattern #,##0 is the usual choice. The final 0 makes zero display as 0 instead of an empty cell.
Literal text and fixed characters
Some characters are copied without quotes: $ - + / ( ) : ! ^ & ' ~ { } < > = and the space. For other text, wrap it in double quotes, or put a backslash before a single character.
0.0 "kg"The value 2.5 displays as 2.5 kg. A value that is a phone-style number can use fixed hyphens:
000-0000The value 5551234 displays as 555-1234.
Common errors
- Forgetting quotes around text. A letter such as
d,m,y,horsis read as a date or time code, not as text. Quote any words you add. - Expecting a custom code to round the value. It rounds only the display. Use ROUND when the stored number must change.
- Typing the code on a cell that already holds text. A format cannot turn text into a number. That problem is covered in a later chapter.
Exercise
Write a custom code that displays the number 8 as Room 008, and say what the cell still contains.
Show answer
Use "Room "000 (the text in quotes, then three zeros). The cell still contains the number 8. Only the display changes.