- •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
Introducing Array
Formulas
One of Excel’s most interesting (and most powerful) features is its ability to work with arrays in formulas. When you understand this concept, you’ll be able to create elegant formulas that appear to per-
form spreadsheet magic.
This chapter introduces the concept of arrays and is required reading for anyone who wants to become a master of Excel formulas. Chapter 17 continues with lots of useful examples.
On the CD
Most of the examples in this chapter are available on the companion CD-ROM. The filename is array examples.xlsx.
Understanding Array Formulas
If you do any computer programming, you’ve probably been exposed to the concept of an array. An array is simply a collection of items operated on collectively or individually. In Excel, an array can be one dimensional or two dimensional. These dimensions correspond to rows and columns. For example, a one-dimensional array can be stored in a range that consists of one row (a horizontal array) or one column (a vertical array). A two-dimensional array can be stored in a rectangular range of cells. Excel doesn’t support threedimensional arrays (but its VBA programming language does).
As you’ll see, arrays need not be stored in cells. You can also work with arrays that exist only in Excel’s memory. You can then use an array formula to manipulate this information and return a result. An array formula can occupy multiple cells or reside in a single cell.
CHAPTER
IN THIS CHAPTER
The definition of an array and an array formula
One-dimensional versus twodimensional arrays
How to work with array constants
Techniques for working with array formulas
Examples of multicell array formulas
Examples of array formulas that occupy a single cell
355
Part II: Working with Formulas and Functions
This section presents two array formula examples: an array formula that occupies multiple cells and another array formula that occupies only one cell.
A multicell array formula
Figure 16.1 shows a simple worksheet set up to calculate product sales. Normally, you’d calculate the value in column D (total sales per product) with a formula such as the one that follows, and then you’d copy this formula down the column.
=B2*C2
After copying the formula, the worksheet contains six formulas in column D.
FIGURE 16.1
Column D contains formulas to calculate the total for each product.
An alternative method uses a single formula (an array formula) to calculate all six values in D2:D7. This single formula occupies six cells and returns an array of six values.
To create a single array formula to perform the calculations, follow these steps:
1.Select a range to hold the results. In this case, the range is D2:D7. Because you can’t display more than one value in a single cell, six cells are required to display the resulting array — so you select six cells to make this array work.
2.Type the following formula:
=B2:B7*C2:C7
3.Press Ctrl+Shift+Enter to enter the formula. Normally, you press Enter to enter a formula. Because this is an array formula, however, press Ctrl+Shift+Enter.
Caution
You can’t insert a multicell array formula into a range that has been designated a table (using Insert Tables Table). In addition, you can’t convert a range that contains a multicell array formula to a table. n
356
Chapter 16: Introducing Array Formulas
The formula is entered into all six selected cells. If you examine the Formula bar, you see the following:
{=B2:B7*C2:C7}
Excel places curly brackets around the formula to indicate that it’s an array formula.
This formula performs its calculations and returns a six-item array. The array formula actually works with two other arrays, both of which happen to be stored in ranges. The values for the first array are stored in B2:B7, and the values for the second array are stored in C2:C7.
This array formula returns exactly the same values as these six normal formulas entered into individual cells in D2:D7:
=B2*C2
=B3*C3
=B4*C4
=B5*C5
=B6*C6
=B7*C7
Using a single array formula rather than individual formulas does offer a few advantages:
•It’s a good way to ensure that all formulas in a range are identical.
•Using a multicell array formula makes it less likely that you’ll overwrite a formula accidentally. You can’t change one cell in a multicell array formula. Excel displays an error message if you attempt to do so.
•Using a multicell array formula will almost certainly prevent novices from tampering with your formulas.
Using a multicell array formula as described in the preceding list also has some potential disadvantages:
•It’s impossible to insert a new row into the range. But in some cases, the inability to insert a row is a positive feature. For example, you might not want users to add rows because it would affect other parts of the worksheet.
•If you add new data to the bottom of the range, you need to modify the array formula to accommodate the new data.
A single-cell array formula
Now it’s time to take a look at a single-cell array formula. Check out Figure 16.2, which is similar to Figure 16.1. Notice, however, that the formulas in column D have been deleted. The goal is to calculate the sum of the total product sales without using the individual calculations that were in column D.
357
Part II: Working with Formulas and Functions
FIGURE 16.2
The array formula in cell C9 calculates the total sales without using intermediate formulas.
The following array formula is in cell C9:
{=SUM(B2:B7*C2:C7)}
When you enter this formula, make sure that you use Ctrl+Shift+Enter (and don’t type the curly brackets because Excel automatically adds them for you).
This formula works with two arrays, both of which are stored in cells. The first array is stored in B2:B7, and the second array is stored in C2:C7. The formula multiplies the corresponding values in these two arrays and creates a new array (which exists only in memory). The SUM function then operates on this new array and returns the sum of its values.
Note
In this case, you can use the SUMPRODUCT function to obtain the same result without using an array formula:
=SUMPRODUCT(B2:B7,C2:C7)
As you see, however, array formulas allow many other types of calculations that are otherwise not possible.
Creating an array constant
The examples in the preceding section used arrays stored in worksheet ranges. The examples in this section demonstrate an important concept: An array need not be stored in a range of cells. This type of array, which is stored in memory, is referred to as an array constant.
To create an array constant, list its items and surround them with brackets. Here’s an example of a five-item horizontal array constant:
{1,0,1,0,1}
358
Chapter 16: Introducing Array Formulas
The following formula uses the SUM function, with the preceding array constant as its argument. The formula returns the sum of the values in the array (which is 3):
=SUM({1,0,1,0,1})
Notice that this formula uses an array, but the formula itself isn’t an array formula. Therefore, you don’t use Ctrl+Shift+Enter to enter the formula — although entering it as an array formula will still produce the same result.
Note
When you specify an array directly (as shown previously), you must provide the curly brackets around the array elements. When you enter an array formula, on the other hand, you do not supply the brackets. n
At this point, you probably don’t see any advantage to using an array constant. The following formula, for example, returns the same result as the previous formula. The advantages, however, will become apparent.
=SUM(1,0,1,0,1)
This formula uses two array constants:
=SUM({1,2,3,4}*{5,6,7,8})
This formula creates a new array (in memory) that consists of the product of the corresponding elements in the two arrays. The new array is
{5,12,21,32}
This new array is then used as an argument for the SUM function, which returns the result (70). The formula is equivalent to the following formula, which doesn’t use arrays:
=SUM(1*5,2*6,3*7,4*8)
Alternatively, you can use the SUMPRODUCT function. The formula that follows is not an array formula, but it uses two array constants as its arguments.
=SUMPRODUCT({1,2,3,4},{5,6,7,8})
A formula can work with both an array constant and an array stored in a range. The following formula, for example, returns the sum of the values in A1:D1, each multiplied by the corresponding element in the array constant:
=SUM((A1:D1*{1,2,3,4}))
This formula is equivalent to
=SUM(A1*1,B1*2,C1*3,D1*4)
359