Pages

Wednesday, February 22, 2012

Format Cells with Conditions – Part II

In the previous post (Format Cells with Conditions) we formed a format string that displays value in thousands or millions with two digits rounding off along with K or M as suffix to indicate thousand/million. The format string that we formed is given below:

[>=1000000]0.00,,_K "M";[>0]0.00,_M "K";"-"_M_K

This string has 3 sections;
  1. First section displays the value in Millions if the value entered in a cell is greater than or equal to 10 Lakhs. 
  2. Second Section displays the value in thousands, if the value entered in a cell is in between 0 and 10 lakhs. 
  3. Third Section displays Zero as –.
Negative Entries:

Do you have a doubt that how the negative entry will be displayed? Apply the above format in a cell and try with any negative entry. Are you getting two hyphens as result? (i.e. --)

As per our earlier discussion in the post – Post Name, the first section of the format string is for positive values, second section is for negative Values, third section is for zeros and the fourth section is for text (refer Picture A – General custom format string)
Picture A - General Custom Format String
But when you use ‘conditions’ in custom format string, excel will not interpret the sections as given in Picture A. In other words, if a format string has conditions, excel will not apply first section to the positive values, second section to negative values, third section to zeros and fourth section to text.

Excel simply ignores general formatting rules and applies the formats based on the conditions given in the format string. Hence, when you enter a negative entry in a cell with the above format style, it displays two hyphens, i.e. one hyphen is the mines symbol that you have entered and the other is an indicator of zeros specified in third section of the format string. In other words, the negative entry will not satisfy the first two sections condition so, excel will chose the third section and apply the format style.

Solution:

We cannot handle this with a SINGLE custom format string and unfortunately, in a cell, we cannot apply MORE THAN ONE custom format.

Why we cannot handle this with a single custom format string? All four conditions are given below (Table A)
Table A: Conditions and Formats
Conditions
Formats
Result
>=1000000
0.00,, "M"
1.00 M
>0
0.00, “K”
1.00 K
<0
(0.00,) “K”
(1.00) K
<=-1000000
(0.00,) “M”
(1.00) M

All these four conditions cannot be placed in a single custom cell format string. Maximum, we can place only two conditions in a string. Hence, we have to break this as TWO Custom Cell format string. They are given below.

0.00, “K”;(0.00,) “K”; “-”
0.00,, “M”;(0.00,,) “M”; “-”

The former is for displaying the values in thousands and later is for displaying the values in millions.

Conditional Formatting

We cannot apply this two format style in a single cell; hence, we need to do some work around. Using conditional format feature of excel, we can solve this issue.

Conditional formatting works based on rules. In this situation, we need to define 4 rules and need to set the corresponding formatting style; they are given below (Table B)

Table B: Rules and formats
Conditions
Formats
Cell value <=-1000000
0.00,, “M”;(0.00,,) “M”; “-”
Cell Value >=-1000000
0.00,, “M”;(0.00,,) “M”; “-”
Cell Value between -999999 and 0
0.00, “K”;(0.00,) “K”; “-”
Cell Value between 0 and 999999
0.00, “K”;(0.00,) “K”; “-”

Steps to apply the conditional formatting

Step 1:

On the Home tab, in the Style group, click the arrow next to the Conditional Formatting, and click New Rules. (Shortcut: Alt + H + L + N for Excel2007/2010)

Step 2:

In New Formatting Rules dialog box, select Format only cells that contain rule type (i.e. second rule type in Select a Rule type panel) and set the rule description (refer Picture B)
Picture B - New Formatting Rule
Note:

Format only cells that contain: Use this rule type to create rules that format cells based on mathematical comparisons (greater than, less than, greater than or equal to, less than or equal to, equal to, not equal to, between, not between).

Step 3:

Click Format button in New Formatting Rule dialog box once you define the rule description.

Step 4:

Select the Format String that you defined under the Custom Format Category and click OK (Refer Picture C)

Picture C - Apply Custom Format Style
Step 5:

Click OK in New Formatting Rule Dialog Box

The same way create all the rules and apply them to the cells.

To conclude, we can now enter negative entries also in the cells. Based on the rules description, excel will display the values in the cells.

Friday, February 17, 2012

Format cells with Conditions

In the post, we are going to see how to format the cell value with conditions. Excel formats the cell values based on the result of the condition that we specify in the format string.

Before I walk through the format cells with conditions concept, let us see what comparison Operator is, as the comparison operator plays a vital role in custom formats.

Table A: Comparison Operators
Operator
That means
How to use in
logical tests?
(assume
A1=10; B1=15)
Result:
Logical test can return
only a true or
false answer
=
Equals
=(A1=B1)
False
< 
Less than
=(A1<B1)
True
<=
Less than or equal to
=(A1<=B1)
True
> 
Greater than
=(A1>B1)
False
>=
Greater than or 
equal to
=(A1>=B1)
False
<> 
Not equal to
=(A1<>B1)
True


Note: we use the comparison operator in IF formula; based on the logical test result, we show the value as either “TRUE” or “FALSE”. 

How to use comparison operator in Custom Cell Format?

The condition consists of a comparison operator and a value. Both should be specified in Square Brackets.

Example A:
[Red][>=100];[Blue][>100]

In this example, the format displays numbers that are less than or equal to 100 in Red and numbers that are greater than 100 in Blue.

Note: You can use any color that is specified in excel color palette (refer post: custom format –play with colors)

Formatting Number with Conditions:

Assume that we have some core operational income/expenses data and the same should be displayed in millions/thousands (with two digits rounding off) in the dashboard or in the top sheet of the MIS.

Example entries and how it should be displayed in the dashboard are given in Table B. Just have a look and try whether it can be possible using custom format to display the values in the cells as specified in Table B (column 2).

Table B: Example data to create custom formats with conditions

If the value entered in a cell is
It should be displayed as
1500000
1.50 M
1000000
1.00 M
1500
1.50 K
1000
1.00 K
500
0.50 K
0
-
-500
(0.50) K
-1000
(1.00) K
-1500
(1.50) K
-1000000
(1.00) M
-1500000
(1.50) M


I.e. if the value entered in a cell is greater than or equal to 10 lakhs, we need to convert the value in million with two digits rounding off and should add M as suffix; and if it is less than 10 lakhs and greater than 0 we need to convert the value in thousands and add K as suffix.

The conditions and the formats are given below:

Table C: Conditions and format Table

Conditions
Format
>=1000000
0.00,, "M"
>0
0.00, "K"
0
"-"
We can form the custom format string as given below:

[>=1000000]0.00,, “M”;[>0]0.00, “K”;“-”

As specified in Example A,
  1. First section of the format string (i.e. [>=1000000]0,00,, “M”) displays the numbers in millions with M as suffix to indicate Million, if entry in a cell is greater than or equal to 10 Lakhs
  2. Second section of the string displays the numbers in thousands with K as suffix to indicate thousand, if it is greater than Zero and less than 10 lakhs
  3. Third Section of the string displays zeros as -

Shall we apply this format and test the result?

Enter values in column A & B as given in Picture A and apply format style in Column B.
  1. How to apply the custom format in a cell?
  2. Press Ctrl + Shift + F
  3. Select Number tab
  4. Click Custom Category
  5. Enter the format string in Type box
  6. Click OK
For Pictorial presentation, refer the steps specified in post – Custom Cell Formats (Numbers)
Picture A
Alignment:

Did you notice that the decimals are not properly aligned? See the picture given below (Picture B), you can notice the alignment.
Picture B
How to fix this?

Before fixing this issue, let us understand the reason why the numbers are not properly aligned. It is because of character space. We have used K and M as suffix to the display the values in thousands and millions. The character space of K is not equal to the space of M and vice versa. Hence, we are facing this alignment issue.

We need to add
  1. _K in the first sections in between 0.00,, and “M”;
  2. _M in the second section in between 0.00, and “K”; and
  3. _M_K in the third section after “-”
Note: for better understanding that how to add character space in custom formats, refer post - Post Custom Number format – How to display a negative sign on the right side of the number?)

Once you add all these characters in the format string, it will look like as given below:

[>=1000000]0.00,,_K "M";[>0]0.00,_M "K";"-"_M_K

Apply this format in column B and see the final result. The decimals will be properly aligned.

Final Result is given in Picture C
Picture C
Where can we use these values? (Either in millions/thousands)
  • To present the values in
    • Dashboards
    • MIS Top Sheet
    • Executive Summery sheet (in financial presentations)
    • Charts data
For better presentation of financial reports, we never show the value or amounts both in Millions and in thousands. To explain the custom format with conditions, I used this example. But in reality, we should use either amounts in thousands or in millions.

Sunday, February 12, 2012

Custom Format – Play with colors

There are four in-built number formats available in excel under “Number Category” in Format cell dialog box, they are explained in given table

S.No
Value
Format
Result
Color
1.
-1234.10
0.00
-1234.00
This Format string has only one section, hence it is applicable to all the entries. (I.e. Positive, negative and zero entries). It just adds two decimals with the value you entered and show the negative sign on the left side of the negative entries.
2.
-1234.10
0.00;[Red]0.00
1234.10
This format string has separate section for negative entries with [Red] color.
3.
-1234.10
0.00_);(0.00)
(1234.10)
This format adds parentheses. _ (Underscore) is used to line up the numbers. (i.e. positive numbers line up correctly with negative numbers that are enclosed in parentheses)
4.
-1234.10
0.00_);[Red](0.00)
(1234.10)
This format adds parentheses as well as [Red] color text format.
 
How to play with colors?

We can replace [Red] color string with any of the color code specified in the given table (Table B).
  1. For the first 8 colors, we can use the color names directly (they are not case-sensitive) 
  2. For remaining 48 colors, we have to use the color code.
Example:
[color 11]0.00;[red]0.00;[blue]0.00;[color 44]general

Table: B – Excel Color palette

FONT
INTERIOR
FONT
INTERIOR
FONT
INTERIOR
FONT
INTERIOR
[Color 1]
Black
[Color 15]
[Color 15]
[Color 29]
[Color 29]
[Color 43]
[Color 43]
[Color 2]
White
[Color 16]
[Color 16]
[Color 30]
[Color 30]
[Color 44]
[Color 44]
[Color 3]
Red
[Color 17]
[Color 17]
[Color 31]
[Color 31]
[Color 45]
[Color 45]
[Color 4]
Green
[Color 18]
[Color 18]
[Color 32]
[Color 32]
[Color 46]
[Color 46]
[Color 5]
Blue
[Color 19]
[Color 19]
[Color 33]
[Color 33]
[Color 47]
[Color 47]
[Color 6]
Yellow
[Color 20]
[Color 20]
[Color 34]
[Color 34]
[Color 48]
[Color 48]
[Color 7]
Magenta
[Color 21]
[Color 21]
[Color 35]
[Color 35]
[Color 49]
[Color 49]
[Color 8]
Cyan
[Color 22]
[Color 22]
[Color 36]
[Color 36]
[Color 50]
[Color 50]
[Color 9]
[Color 9]
[Color 23]
[Color 23]
[Color 37]
[Color 37]
[Color 51]
[Color 51]
[Color 10]
[Color 10]
[Color 24]
[Color 24]
[Color 38]
[Color 38]
[Color 52]
[Color 52]
[Color 11]
[Color 11]
[Color 25]
[Color 25]
[Color 39]
[Color 39]
[Color 53]
[Color 53]
[Color 12]
[Color 12]
[Color 26]
[Color 26]
[Color 40]
[Color 40]
[Color 54]
[Color 54]
[Color 13]
[Color 13]
[Color 27]
[Color 27]
[Color 41]
[Color 41]
[Color 55]
[Color 55]
[Color 14]
[Color 14]
[Color 28]
[Color 28]
[Color 42]
[Color 42]
[Color 56]
[Color 56]