Sub topic 7
Format sections, colors, and conditions
This chapter assumes you can open Format Cells, enter a custom format code, and read the digit placeholders 0, #, and ?. It covers how one format code can hold several rules, and how those rules pick a color or a layout based on the value.
Four sections in one code
A custom format can have up to four sections separated by semicolons. Each section applies to a different kind of value:
| Section | Applies to |
|---|---|
| 1 | Positive numbers |
| 2 | Negative numbers |
| 3 | Zero |
| 4 | Text |
Here is a format that uses all four:
#,##0.00;[Red]-#,##0.00;0.00;@Positive numbers show with a thousands separator and two decimals. Negative numbers appear in red with a minus sign. Zero shows as 0.00. The @ symbol stands for the text the cell contains, so text appears unchanged.
You do not have to supply every section. The fewer sections you give, the more values share a rule:
- One section applies to all numbers.
- Two sections: the first is for positive numbers and zero, and the second is for negative numbers.
- Three sections add a separate rule for zero.
To hide a kind of value, leave its section empty. The format 0;-0;;@ shows nothing for zero, because the third section is blank.
Colors
Put a color name in square brackets at the start of a section. Excel accepts eight names: [Black], [Blue], [Cyan], [Green], [Magenta], [Red], [White], and [Yellow]. You can also write [Color 1] through [Color 56] to use a palette entry.
The color changes only the display. The stored value is not affected.
Conditions
Instead of the default positive, negative, and zero split, you can write your own tests in square brackets. A condition has a comparison operator and a number, for example [>=1000].
[>=1000000]0.0,,"M";[>=1000]0.0,"K";0With conditions, the sections apply in order. The first section handles values of at least one million. The second handles values of at least one thousand. The last section handles everything else. So 2500000 shows as 2.5M, 4200 shows as 4.2K, and 85 shows as 85.
You can combine a condition with a color:
[Red][<50]0;[Blue][>=50]0Excel allows at most two conditions in one format. A third section, if present, handles every value that matched neither test.
How Excel picks a section
Exercise
Write a custom format that shows positive numbers in green with two decimals, negative numbers in red in parentheses with two decimals, and zero as the word "none". What does 0 display as?
Show answer
Use [Green]0.00;[Red](0.00);"none". A value of 0 uses the third section, so it displays as none. The negative section includes the parentheses itself, because Excel adds no sign of its own.