Sub topic 7
Format sections, colors, and conditions
This chapter assumes you can open Format Cells and type a custom format code in the Custom category. It also assumes you know the digit placeholders 0, #, and ?. Here you will learn how a single format code can behave differently for positive numbers, negative numbers, zero, and text. You will also learn how to color the output and how to choose a section by testing the value.
Sections split one format into several
A custom format code can contain up to four sections separated by semicolons. Excel reads the cell's value, picks one section, and formats the value with it. The stored value never changes. Only the display does.
#,##0.00;[Red]-#,##0.00;0.00;@The four sections have fixed roles:
- Positive numbers.
- Negative numbers.
- Zero.
- Text.
Fewer than four sections are allowed, and the meaning shifts with the count.
| Sections written | How Excel uses them |
|---|---|
| 1 | The section applies to every number. Text is shown unchanged. |
| 2 | The first section applies to positive numbers and zero. The second applies to negative numbers. |
| 3 | Positive, negative, and zero, in that order. Text is shown unchanged. |
| 4 | Positive, negative, zero, and text. |
The negative section drops the minus sign
When Excel uses the negative section, it does not add a minus sign for you. You must write it. In the example above, -#,##0.00 in section two shows -1,234.50. If you leave out the sign, the number looks positive.
#,##0.00;#,##0.00This format shows both 5 and -5 as 5.00. That is almost never what you want, but it is a useful way to see the rule. The sign is part of the section's text, not part of the number's formatting.
You can also use parentheses instead of a minus sign, which is the usual accounting style.
#,##0.00_);(#,##0.00)The _) in the first section adds a space as wide as a closing parenthesis. Positive numbers then line up with negative numbers that end in ).
Colors
Put a color name in square brackets at the start of a section. Excel recognizes eight names:
[Black][Blue][Cyan][Green][Magenta][Red][White][Yellow]
You can also use [Color n], where n is a number from 1 to 56 that picks from the workbook's palette. For example, [Color10] is a dark green, which is easier to read than [Green] on a white background. The palette depends on the workbook's theme, so test it if the file will be shared.
[Color10]+#,##0;[Red]-#,##0;0This format shows a gain in dark green with a plus sign, a loss in red with a minus sign, and zero in the default font color. The literal characters + and - do not need quotation marks. Most other letters and symbols do, as the next sections show.
A color in a format overrides the cell's font color for that section only. A section without a color uses the normal font color.
Colors also work on the text section.
0;-0;0;[Blue]@Typed text shows in blue. Numbers use the font color.
Literal text and hiding values
Text in double quotes appears as written. A section does not have to contain a digit placeholder at all.
#,##0;-#,##0;"none";"Note: "@Here a zero shows the word none. Text shows with the prefix Note: in front of it. The @ stands for whatever text the cell holds.
An empty section hides that kind of value. Leave the section blank and keep the semicolons.
0;-0;;@The third section is empty, so zeros show as blank cells. The format ;;; has four empty sections and hides everything.
Conditions
You can replace the default positive, negative, and zero test with your own. Put a comparison in square brackets at the start of a section. The operators are =, <>, >, >=, <, and <=.
[>=1000000]0.0,,"M";[>=1000]0.0,"K";0This format has two conditions and a third section for everything else. A value of 2,500,000 shows as 2.5M. A value of 4,200 shows as 4.2K. A value of 87 shows as 87. The trailing commas divide the displayed value, and a later chapter covers them in detail.
Three rules apply to conditions:
- A format can hold at most two conditions. A third section without a condition handles all other numbers.
- Excel checks sections from left to right and uses the first one whose condition is true.
- A condition replaces the default role of its section. In
[>100]0;0, the first section is no longer "positive" but "greater than 100".
When you combine a color and a condition, write the color first.
[Green][>=0.5]0%;[Red][<0.5]0%;0%Values of 50% and above show in green. Values below 50% show in red. The third section is never reached for numbers here, because the two conditions cover every number.
Negative numbers under conditions
Once you use conditions, test negative values on purpose. Excel's rule for when it shows or hides the minus sign depends on which section is used and on what the condition is. Do not assume the sign is dropped as it is in a default negative section. Enter -5, 0, and 5 in test cells and check the result before you apply the format to a column.
Worked example: a variance column
Suppose column D holds budget variance in units. You want green with a plus sign for positive values, red with a minus sign for negative values, a dash for zero, and gray-free default text for notes.
- Select the cells and press Ctrl+1 to open Format Cells.
- Choose Custom.
- Type this code in the Type box.
[Color10]+#,##0;[Red]-#,##0;"-";@- Click OK.
A value of 340 shows as +340 in green. A value of -85 shows as -85 in red. A value of 0 shows as -. A text note such as TBC shows unchanged.
Now check the stored values. Enter =SUM(D2:D10) in another cell. The sum uses the real numbers, so the plus signs, colors, and dash have no effect on it.
Common errors
- Missing quotation marks. Letters in a format are often codes.
d,m,y,h,s, andehave special meanings. Quote any word you want to appear as plain text, such as"units". - Forgotten minus sign in section two. The number appears positive. Add
-or parentheses yourself. - Wrong section order. Positive, negative, zero, text. A zero rule written second will apply to negatives.
- Color after the condition. Write
[Red][<0], not[<0][Red]. Test the order if Excel rejects the code. - Expecting text rules in a one-section format. Text is shown as typed unless you add a fourth section with
@. - Separators in other locales. The section separator is a semicolon, but the decimal and thousands characters in the code follow your regional settings. A code copied from a different locale may need its separators changed.
Exercise
Write a custom format that shows positive numbers with a thousands separator, negative numbers in red inside parentheses, zero as the word "nil", and text unchanged. How does Excel show -2500 with this format?
Show answer
Use #,##0;[Red](#,##0);"nil". Text needs no fourth section because text is shown unchanged when the format has three sections. Excel shows -2500 as (2,500) in red. The parentheses come from the format code, and the stored value stays -2500.
Exercise
A cell holds the format [>=100][Blue]0;[<0][Red]-0;0. What do the values 150, -20, and 40 display, and in what color?
Show answer
150 meets the first condition, so it shows 150 in blue. -20 meets the second condition, so it shows -20 in red. 40 meets neither condition and uses the third section, so it shows 40 in the default font color.