- •About the Author
- •About the Technical Editor
- •Credits
- •Is This Book for You?
- •Software Versions
- •Conventions This Book Uses
- •What the Icons Mean
- •How This Book Is Organized
- •How to Use This Book
- •What’s on the Companion CD
- •What Is Excel Good For?
- •What’s New in Excel 2010?
- •Moving around a Worksheet
- •Introducing the Ribbon
- •Using Shortcut Menus
- •Customizing Your Quick Access Toolbar
- •Working with Dialog Boxes
- •Using the Task Pane
- •Creating Your First Excel Worksheet
- •Entering Text and Values into Your Worksheets
- •Entering Dates and Times into Your Worksheets
- •Modifying Cell Contents
- •Applying Number Formatting
- •Controlling the Worksheet View
- •Working with Rows and Columns
- •Understanding Cells and Ranges
- •Copying or Moving Ranges
- •Using Names to Work with Ranges
- •Adding Comments to Cells
- •What Is a Table?
- •Creating a Table
- •Changing the Look of a Table
- •Working with Tables
- •Getting to Know the Formatting Tools
- •Changing Text Alignment
- •Using Colors and Shading
- •Adding Borders and Lines
- •Adding a Background Image to a Worksheet
- •Using Named Styles for Easier Formatting
- •Understanding Document Themes
- •Creating a New Workbook
- •Opening an Existing Workbook
- •Saving a Workbook
- •Using AutoRecover
- •Specifying a Password
- •Organizing Your Files
- •Other Workbook Info Options
- •Closing Workbooks
- •Safeguarding Your Work
- •Excel File Compatibility
- •Exploring Excel Templates
- •Understanding Custom Excel Templates
- •Printing with One Click
- •Changing Your Page View
- •Adjusting Common Page Setup Settings
- •Adding a Header or Footer to Your Reports
- •Copying Page Setup Settings across Sheets
- •Preventing Certain Cells from Being Printed
- •Preventing Objects from Being Printed
- •Creating Custom Views of Your Worksheet
- •Understanding Formula Basics
- •Entering Formulas into Your Worksheets
- •Editing Formulas
- •Using Cell References in Formulas
- •Using Formulas in Tables
- •Correcting Common Formula Errors
- •Using Advanced Naming Techniques
- •Tips for Working with Formulas
- •A Few Words about Text
- •Text Functions
- •Advanced Text Formulas
- •Date-Related Worksheet Functions
- •Time-Related Functions
- •Basic Counting Formulas
- •Advanced Counting Formulas
- •Summing Formulas
- •Conditional Sums Using a Single Criterion
- •Conditional Sums Using Multiple Criteria
- •Introducing Lookup Formulas
- •Functions Relevant to Lookups
- •Basic Lookup Formulas
- •Specialized Lookup Formulas
- •The Time Value of Money
- •Loan Calculations
- •Investment Calculations
- •Depreciation Calculations
- •Understanding Array Formulas
- •Understanding the Dimensions of an Array
- •Naming Array Constants
- •Working with Array Formulas
- •Using Multicell Array Formulas
- •Using Single-Cell Array Formulas
- •Working with Multicell Array Formulas
- •What Is a Chart?
- •Understanding How Excel Handles Charts
- •Creating a Chart
- •Working with Charts
- •Understanding Chart Types
- •Learning More
- •Selecting Chart Elements
- •User Interface Choices for Modifying Chart Elements
- •Modifying the Chart Area
- •Modifying the Plot Area
- •Working with Chart Titles
- •Working with a Legend
- •Working with Gridlines
- •Modifying the Axes
- •Working with Data Series
- •Creating Chart Templates
- •Learning Some Chart-Making Tricks
- •About Conditional Formatting
- •Specifying Conditional Formatting
- •Conditional Formats That Use Graphics
- •Creating Formula-Based Rules
- •Working with Conditional Formats
- •Sparkline Types
- •Creating Sparklines
- •Customizing Sparklines
- •Specifying a Date Axis
- •Auto-Updating Sparklines
- •Displaying a Sparkline for a Dynamic Range
- •Using Shapes
- •Using SmartArt
- •Using WordArt
- •Working with Other Graphic Types
- •Using the Equation Editor
- •Customizing the Ribbon
- •About Number Formatting
- •Creating a Custom Number Format
- •Custom Number Format Examples
- •About Data Validation
- •Specifying Validation Criteria
- •Types of Validation Criteria You Can Apply
- •Creating a Drop-Down List
- •Using Formulas for Data Validation Rules
- •Understanding Cell References
- •Data Validation Formula Examples
- •Introducing Worksheet Outlines
- •Creating an Outline
- •Working with Outlines
- •Linking Workbooks
- •Creating External Reference Formulas
- •Working with External Reference Formulas
- •Consolidating Worksheets
- •Understanding the Different Web Formats
- •Opening an HTML File
- •Working with Hyperlinks
- •Using Web Queries
- •Other Internet-Related Features
- •Copying and Pasting
- •Copying from Excel to Word
- •Embedding Objects in a Worksheet
- •Using Excel on a Network
- •Understanding File Reservations
- •Sharing Workbooks
- •Tracking Workbook Changes
- •Types of Protection
- •Protecting a Worksheet
- •Protecting a Workbook
- •VB Project Protection
- •Related Topics
- •Using Excel Auditing Tools
- •Searching and Replacing
- •Spell Checking Your Worksheets
- •Using AutoCorrect
- •Understanding External Database Files
- •Importing Access Tables
- •Retrieving Data with Query: An Example
- •Working with Data Returned by Query
- •Using Query without the Wizard
- •Learning More about Query
- •About Pivot Tables
- •Creating a Pivot Table
- •More Pivot Table Examples
- •Learning More
- •Working with Non-Numeric Data
- •Grouping Pivot Table Items
- •Creating a Frequency Distribution
- •Filtering Pivot Tables with Slicers
- •Referencing Cells within a Pivot Table
- •Creating Pivot Charts
- •Another Pivot Table Example
- •Producing a Report with a Pivot Table
- •A What-If Example
- •Types of What-If Analyses
- •Manual What-If Analysis
- •Creating Data Tables
- •Using Scenario Manager
- •What-If Analysis, in Reverse
- •Single-Cell Goal Seeking
- •Introducing Solver
- •Solver Examples
- •Installing the Analysis ToolPak Add-in
- •Using the Analysis Tools
- •Introducing the Analysis ToolPak Tools
- •Introducing VBA Macros
- •Displaying the Developer Tab
- •About Macro Security
- •Saving Workbooks That Contain Macros
- •Two Types of VBA Macros
- •Creating VBA Macros
- •Learning More
- •Overview of VBA Functions
- •An Introductory Example
- •About Function Procedures
- •Executing Function Procedures
- •Function Procedure Arguments
- •Debugging Custom Functions
- •Inserting Custom Functions
- •Learning More
- •Why Create UserForms?
- •UserForm Alternatives
- •Creating UserForms: An Overview
- •A UserForm Example
- •Another UserForm Example
- •More on Creating UserForms
- •Learning More
- •Why Use Controls on a Worksheet?
- •Using Controls
- •Reviewing the Available ActiveX Controls
- •Understanding Events
- •Entering Event-Handler VBA Code
- •Using Workbook-Level Events
- •Working with Worksheet Events
- •Using Non-Object Events
- •Working with Ranges
- •Working with Workbooks
- •Working with Charts
- •VBA Speed Tips
- •What Is an Add-In?
- •Working with Add-Ins
- •Why Create Add-Ins?
- •Creating Add-Ins
- •An Add-In Example
- •System Requirements
- •Using the CD
- •What’s on the CD
- •Troubleshooting
- •The Excel Help System
- •Microsoft Technical Support
- •Internet Newsgroups
- •Internet Web sites
- •End-User License Agreement
Chapter 24: Using Custom Number Formats
Note
If the cell displays a series of hash marks after you apply a number format (such as #########), it usually means that the column isn’t wide enough to display the value by using the number format that you selected. Either make the column wider (by dragging the right border of the column header) or change the number format. A series of hash marks also can mean that the cell contains an invalid date or time. n
Creating a Custom Number Format
When you create a custom number format, it can be used to format any cells in the workbook. You can create as many custom number formats as you need.
Figure 24.1 shows the Custom category in the Number tab of the Format Cells dialog box. Here, you can create number formats not included in any of the other categories. Excel gives you a great deal of flexibility in creating custom number formats.
FIGURE 24.1
The Custom category of the Number tab in the Format Cells dialog box.
Tip
Custom number formats are stored with the workbook in which they are defined. To make the custom format available in a different workbook, you can just copy a cell that uses the custom format to the other workbook. n
555
Part IV: Using Advanced Excel Features
You construct a number format by specifying a series of codes as a number format string. You enter this code sequence in the Type field after you select the Custom category on the Number tab of the Format Cells dialog box. Here’s an example of a simple number format code:
0.000
This code consists of placeholders and a decimal point; it tells Excel to display the value with three digits to the right of the decimal place. Here’s another example:
00000
This custom number format has five placeholders and displays the value with five digits (no decimal point). This format is good to use when the cell holds a five-digit zip code. (In fact, this is the code actually used by the Zip Code format in the Special category.) When you format the cell with this number format and then enter a Zip Code, such as 06604 (Bridgeport, CT), the value is displayed with the leading zero. If you enter this number into a cell with the General number format, it displays 6604 (no leading zero).
Scroll through the list of number formats in the Custom category in the Format Cells dialog box to see many more examples. In many cases, you can use one of these codes as a starting point, and you’ll need to customize it only slightly.
On the CD
The companion CD-ROM contains a workbook with many custom number format examples (see Figure 24.2). The file is named number formats.xlsx.
Changing the Default Number Format for a Workbook
As I mention earlier, the default number format is General. If you prefer a different default number format, you have two choices: Preformat the cells with the number format of your choice, or change the number format for the Normal style.
You can preformat specific cells, entire rows or columns, or even the entire worksheet.
Rather than preformat an entire worksheet, however, a better solution is to change the number format for the Normal style. Unless you specify otherwise, all cells use the Normal style. Therefore, by changing the number format for the Normal style you are essentially creating a new default number format for the workbook.
Change the Normal style by displaying the Style gallery. Right-click the Normal style icon (in the Home Styles group) and choose Modify to display the Style dialog box. In the Style dialog box, click the Format button and then choose the new number format that you want to use for the Normal style.
556
Chapter 24: Using Custom Number Formats
FIGURE 24.2
Examples of custom number formatting.
Parts of a number format string
A custom format string can have up to four sections, which enables you to specify different format codes for positive numbers, negative numbers, zero values, and text. You do so by separating the codes with a semicolon. The codes are arranged in the following order:
Positive format; Negative format; Zero format; Text format
If you don’t use all four sections of a format string, Excel interprets the format string as follows:
•If you use only one section: The format string applies to all types of entries.
•If you use two sections: The first section applies to positive values and zeros, and the second section applies to negative values.
•If you use three sections: The first section applies to positive values, the second section applies to negative values, and the third section applies to zeros.
•If you use all four sections: The last section applies to text stored in the cell.
557
Part IV: Using Advanced Excel Features
The following is an example of a custom number format that specifies a different format for each of these types:
[Green]General;[Red]General;[Black]General;[Blue]General
This custom number format example takes advantage of the fact that colors have special codes. A cell formatted with this custom number format displays its contents in a different color, depending on the value. When a cell is formatted with this custom number format, a positive number is green, a negative number is red, a zero is black, and text is blue.
Cross-Reference
If you want to apply cell formatting automatically (such as text or background color) based on the cell’s contents, a much better solution is to use the Excel Conditional Formatting feature. Chapter 20 covers conditional formatting. n
Custom number format codes
Table 24.3 lists the formatting codes available for custom formats, along with brief descriptions. I use most of these codes in examples later in this chapter.
TABLE 24.3
Codes Used to Create Custom Number Formats
Code Comments
General Displays the number in General format.
#Digit placeholder. Displays only significant digits, and does not display insignificant zeros.
0 (zero) |
Digit placeholder. Displays insignificant zeros if a number has fewer digits than there are |
|
zeros in the format. |
|
|
?Digit placeholder. Adds spaces for insignificant zeros on either side of the decimal point so that decimal points align when formatted with a fixed-width font. You can also use ? for fractions that have varying numbers of digits.
. |
Decimal point. |
% |
Percentage. |
|
|
, |
Thousands separator. |
|
|
E- E+ e- e+ |
Scientific notation. |
|
|
$ - + / ( ) : |
Displays this character. |
space |
|
|
|
\ |
Displays the next character in the format. |
|
|
* |
Repeats the next character, to fill the column width. |
|
|
_ (underscore) |
Leaves a space equal to the width of the next character. |
|
|
558
Chapter 24: Using Custom Number Formats
Code Comments
“text” Displays the text inside the double quotation marks.
@Text placeholder.
[color] |
Displays the characters in the color specified. Can be any of the following text strings (not |
|
case sensitive): Black, Blue, Cyan, Green, Magenta, Red, White, or Yellow. |
|
|
[Color n] |
Displays the corresponding color in the color palette, where n is a number from 0 to 56. |
|
|
[condition |
Set your own criterion for each section of a number format. |
value] |
|
|
|
Table 24.4 lists the codes used to create custom formats for dates and times.
TABLE 24.4
Codes Used in Creating Custom Formats for Dates and Times
Code |
Comments |
|
|
m |
Displays the month as a number without leading zeros (1–12). |
|
|
mm |
Displays the month as a number with leading zeros (01–12). |
|
|
mmm |
Displays the month as an abbreviation (Jan–Dec). |
|
|
mmmm |
Displays the month as a full name (January–December). |
|
|
mmmmm |
Displays the first letter of the month (J–D). |
|
|
d |
Displays the day as a number without leading zeros (1–31). |
|
|
dd |
Displays the day as a number with leading zeros (01–31). |
|
|
ddd |
Displays the day as an abbreviation (Sun–Sat). |
|
|
dddd |
Displays the day as a full name (Sunday–Saturday). |
|
|
yy or yyyy |
Displays the year as a two-digit number (00–99) or as a four-digit number (1900–9999). |
|
|
h or hh |
Displays the hour as a number without leading zeros (0–23) or as a number with leading |
|
zeros (00–23). |
|
|
m or mm |
Displays the minute as a number without leading zeros (0–59) or as a number with lead- |
|
ing zeros (00–59). |
|
|
s or ss |
Displays the second as a number without leading zeros (0–59) or as a number with lead- |
|
ing zeros (00–59). |
|
|
[ ] |
Displays hours greater than 24 or minutes or seconds greater than 60. |
|
|
AM/PM |
Displays the hour using a 12-hour clock; if no AM/PM indicator is used, the hour uses a |
|
24 hour clock. |
559