You are on page 1of 105

ExcelTips:

Amazing Array
Formulas
Fourth Edition

Using Array Formulas to Create


Powerful New Ways of Viewing Your Data
ExcelTips: Amazing Array Formulas
Fourth Edition

Published by:

Sharon Parq Associates, Inc.


PO Box 794
Orem, UT 84059

Copyright © 2016 by Sharon Parq Associates, Inc. All rights reserved. No part of this document or the related files may be
reproduced or transmitted in any form, by any means (electronic, photocopying, recording, or otherwise) without the prior written
permission of the publisher.

For information on purchasing, distributing, or reselling books published by Sharon Parq Associates, Inc., please visit our website
(www.SharonParq.com) or call 801-607-2035. Our books are also available through select online resellers such as Amazon.

ISBN: 978-1-61359-346-2

Produced and published in the United States of America

Revision history:

1 October 2009: First edition


4 October 2011: Second edition
2 October 2013: Third edition
24 March 2016: Fourth edition

Limit of Liability and Disclaimer of Warranty: The publisher has used its best efforts in preparing this book, and the information
provided herein is provided "as is." Sharon Parq Associates, Inc., makes no representation or warranties with respect to the
accuracy or completeness of the contents of this book and specifically disclaims any implied warranties of merchantability or
fitness for any particular purpose and shall in no event be liable for any loss of profit or any other commercial damage, including
but not limited to special, incidental, consequential, or other damages.

Trademarks: This book identifies product names and services known to be trademarks, registered trademarks, or service marks
of their respective holders. They are used throughout this book in an editorial fashion only. In addition, terms suspected of being
trademarks, registered trademarks, or service marks have been appropriately capitalized, although Sharon Parq Associates, Inc.,
cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any
trademark, registered trademark, or service mark. Sharon Parq Associates, Inc., is not associated with any product or vendor
mentioned in this book.

Internet Addresses. This book includes various Internet addresses, including URLs and e-mail addresses. These addresses are
believed to be valid addresses at the time of writing. Due to the fluid nature of the Internet, it is possible that some addresses may
become invalid at any time. All addresses are provided for the convenience of the reader, but no address is guaranteed to be valid
or still useful to the reader at the time of reading.
Table of Contents

Introduction 1
WHAT ABOUT YOU, THE READER? 1
CAN'T ALL THIS STUFF BE DONE WITH MACROS? 2
WHAT ABOUT REGULAR FORMULAS? 2
A WORD ABOUT VERSIONS 2
I'D LIKE YOUR FEEDBACK 3
NEED MORE TIPS? 3
SHARING THIS DOCUMENT 4

Array Formula Basics 5


UNDERSTANDING ARRAY FORMULAS 5
SINGLE-CELL ARRAY FORMULAS 6
MULTI-CELL ARRAY FORMULAS 7
SOME ARRAY FORMULA GOTCHAS 10
ANALYZING AN ARRAY FORMULA 11
EDITING MULTI-CELL ARRAY FORMULAS 12
COPYING AN ARRAY FORMULA 12
EASILY SELECTING AN ARRAY FORMULA RANGE 14
ARRAY FORMULAS WITHOUT CELLS 14
CHECKING FOR PROPER ENTRY OF ARRAY FORMULAS 16
STOPPING THE DELETION OF CELLS 17

Using Array Formulas to Count 19


COUNTING WITH TWO CRITERIA 19
COUNTING RECORDS MATCHING MULTIPLE CRITERIA 21
COUNTING DATES IN A RANGE 25
COUNTING UNIQUE VALUES WITH FUNCTIONS 26
COUNTING UNIQUE VALUES 27
COUNTING CELLS WITH SPECIFIC CHARACTERS 28
COUNTING GROUPINGS BELOW A THRESHOLD 29
COUNTING COMMAS IN A SELECTION 30
COUNTING WINS AND LOSSES 31
GETTING A COUNT OF UNIQUE NAMES 32
COUNTING ASTERISKS 34
COUNTING ASTERISKS IN A COLUMN 35
COUNTING ODDS AND EVENS 37
FINDING ODD VALUES GREATER THAN 50 37
GETTING A CONDITIONAL COUNT OF CELLS CONTAINING VALUES 40

Using Array Formulas with Numbers 42


SUMMING DIGITS IN A VALUE 42
SUMMING ABSOLUTE VALUES 42
SUMMING EVERY FOURTH CELL IN A ROW 43
SUMMING BASED ON PART OF THE INFORMATION IN A CELL 45

ExcelTips: Amazing Array Formulas Page iii


USING A NUMERIC PORTION OF A CELL IN A FORMULA 47
CALCULATING THE MEDIAN AGE OF A GROUP OF PEOPLE 48
AN AVERAGE THAT EXCLUDES ZERO VALUES 49
DERIVING HIGH AND LOW NON-ZERO VALUES 50
MEDIAN OF SELECTED NUMBERS 51
CALCULATING A GEOMETRIC STANDARD DEVIATION 51
USING GEOMEAN WITH A LARGE LIST 52
EXCLUDING VALUES FROM AVERAGING 53
FINDING THE SMALLEST EVEN VALUE 54
RETURNING THE SMALLEST NON-ZERO VALUE 55
LAST NON-ZERO VALUE IN A ROW 55
EXTRACTING NUMBERS WITHIN A RANGE 57
PICKING DIFFERENT RANDOM NUMBERS FROM A RANGE 60

Using Array Formulas with Text 62


COMPARING LISTS FOR DUPLICATES 62
FINDING THE FIRST NON-DIGIT IN A TEXT VALUE 63
INSERTING DASHES BETWEEN LETTERS AND NUMBERS 64
GETTING RID OF EVERYTHING EXCEPT NUMBERS 65
BREAKING UP VARIABLE-LENGTH PART NUMBERS 65
RETURNING ITEM CODES INSTEAD OF ITEM NAMES 66
COMPILING A LIST OF STUDENTS IN A COURSE 69
SPLITTING CELLS BY CASE 69
CHECKING FOR MESSAGES IN CELLS 70
DELETING DUPLICATE COLUMNS 71
CHECKING FOR EITHER OF TWO TEXT VALUES 72
EXTRACTING STREET NUMBERS FROM AN ADDRESS 73

Using Array Formulas with Dates 75


WEEKDAYS IN A MONTH 75
FINDING THE DATE ASSOCIATED WITH A NEGATIVE VALUE 76
DETERMINING A NAME FOR A WEEK NUMBER 76
CALCULATING AVERAGES BY DATE 78
AVERAGING VALUES FOR A GIVEN MONTH AND YEAR 80
DERIVING MONTHLY MEDIAN VALUES 82
COUNTING JOBS COMPLETED ON A DATE 84
GETTING RID OF ALL ROWS EXCEPT THE ONE FOR THE LATEST DATE 85
CREATING A YEAR-TO-DATE COMPARISON CHART 88

Using Array Formulas for Lookups 93


COMPLEX LOOKUP FORMULAS 93
MAKING VLOOKUP CASE SENSITIVE 95
LOOKING BACKWARD THROUGH A DATA TABLE 95
PULLING A PHONE NUMBER WITH A KNOWN FIRST AND LAST NAME 96
SEARCHING FOR A VALUE USING A FUNCTION 98
TRANSPOSING AND LINKING 99

ExcelTips: Amazing Array Formulas Page iv


Introduction

A n array is a group of items that can, in some fashion, be treated as a whole. Those who
work with programming languages often use arrays as one method to work efficiently
with data. In Excel, an array formula is a special type of formula that allows you to
perform calculations on huge amounts of data, in a single operation. While you'll find more
about arrays in the first proper section of this book ("Array Formula Basics"), it doesn't hurt to
know that array formulas, once properly understood, provide new and exciting ways that you can
perform calculations with your data. In fact, there are some calculations that can be done with
array formulas that would otherwise be impossible with regular Excel formulas.

In the pages of ExcelTips: Amazing Array Formulas you'll find dozens of tips that can help you
better understand how to put array formulas to work with your data. This is the fourth edition,
and it is larger and more comprehensive than any previous edition. (But I haven't increased the
price of ExcelTips: Amazing Array Formulas at all; it is still the same price now as it has always
been—even though you get much more information.)

Many of the tips contained within the pages of this book have been pulled from past issues of the
ExcelTips newsletter, but their inclusion in this single volume brings a level of focus that is not
available in any other single source.

Not all the information presented here is from past issues of ExcelTips, however. A good chunk
of the information in these pages has never been presented in any of my newsletters. You'll
discover not only what array functions are and how to create them, but also different ways you
can use them right away.

What about You, the Reader?


This book makes some assumptions about you, the reader. This is not unusual; any book makes
assumptions about the background and interests of its readers. The first (and most obvious)
assumption is that you are interested in learning how array formulas work and how they can be
used with your data.

Another assumption is that you are the curious type—you don't mind testing things out to see if
(and how) they work. If this describes you, then you will definitely feel right at home in these
pages. Finally, I also assume that you have at least a vague idea of how regular formulas work in
Excel; you know how to type one in, you know what operators and operands are, and you know
what the more common worksheet functions (SUM, COUNT, COUNTA, etc.) do.

As you work through the information in this book, you'll find that there are tips for all levels of
users. Some of the tips are for beginners while others are definitely for more advanced users.
This means there is plenty of information here for a broad range of readers. It also means you

ExcelTips: Amazing Array Formulas Page 1


Introduction

can progress from tip to tip at your own speed, as you become comfortable with the content of
each tip.

Regardless of your experience level with Excel—and because any good exposition demands a
good foundation—you'll want to make sure you read through the first major section in this book
("Array Formula Basics"). It lays the groundwork that is needed to understand everything else in
the rest of the book, and without it you won't get as far as quickly as you will with it.

Can't All this Stuff Be Done with Macros?


Array formulas are typically used for some rather complex purposes. Many people reasonably
ask the question of whether the tasks performed with array formulas couldn't just as easily be
done with macros.

The catch here is "just as easily." For those who know how to program in VBA but not do array
formulas, it is probably easier to whip up a small macro to step through the data and get
whatever value or result you need. If you fit into that category, then by all means—go for it!

You should know, however, that just because something can be done in a macro doesn't mean
that it should be done in a macro. Macros are extremely powerful, but so are array formulas. All
things being equal, if you can create an array formula to do something rather than using a macro
to do the same thing, you should probably choose the array formula. Two immediate reasons
come to mind:

• A well-crafted array formula will execute faster than a macro doing the same task.
• When you add macros to a workbook, you run the risk of those macros not working—for
a variety of reasons—on another user's system. That rather large drawback doesn't exist
with array formulas.

With that in mind, understand that this book focus on array formulas. If you know how to create
and use macros, you may look at a task covered in a tip and say "I can do that in a macro." While
that may be true, it isn't an oversight that macro solutions aren't covered in this book; it was a
deliberate decision to keep the focus where it belongs—on array formulas.

What about Regular Formulas?


It is possible to accomplish some tasks using either an array formula or a regular formula. If a
task discussed in this book can be solved with both types of formulas, then both types of
solutions are presented. That way you can select from either option to accomplish whatever task
is at hand.

A Word about Versions


This book is written in such a way that it is immediately usable, regardless of which version of
Excel you are using. Array formulas have quite a legacy in Excel, so the information here should
be usable in all modern versions of the program, from Excel 97 through Excel 2016.

Due to the different user interfaces used in various versions of Excel, there are some differences
in how some tasks are accomplished. If a tip involves the use of the user interface, those

ExcelTips: Amazing Array Formulas Page 2


Introduction

differences are taken into account and differing instructions are provided. Keep your eyes peeled
for those differences.

I'd Like Your Feedback


After you get done working through ExcelTips: Amazing Array Formulas, I need your help. I'd
like you to answer a few simple questions: Did you like ExcelTips: Amazing Array Formulas?
Was it helpful? Is it a good value? Did you expect more (or less) than what you got?

What I'd like is your feedback about this book, as feedback is helpful in planning the future of
ExcelTips. If you have something you'd like to share with me, just drop me a line. Here's my
contact information:

Allen Wyatt
Sharon Parq Associates, Inc.
PO Box 794
Orem, UT 84059

allen@sharonparq.com

Also, if you have a formal "testimonial" about ExcelTips: Amazing Array Formulas, send it my
way. I have an uneven and inconsistent habit of rewarding concise, pithy, and glowing
testimonials. (I've even been known to reward negative feedback. All of it is important to me.)

Need More Tips?


Remember that this document contains tips presented in past issues of ExcelTips. This free
newsletter has been published weekly, without fail, since November 7, 1998. (This year, 2016,
marks 18 full years of publication.)

If you want to stay at the top of your Excel form, you should consider subscribing to ExcelTips.
It is free, after all! All you need is an e-mail account, and you can receive a weekly dose of tips
related to Excel. To subscribe to ExcelTips visit the ExcelTips Web site:

http://excelribbon.tips.net

If you prefer to only consider tips that work with older versions of Excel (Excel 97, Excel 2000,
Excel 2002, and Excel 2003), then you'll want to visit this site, instead:

http://excel.tips.net

ExcelTips is part of the Tips.Net network, a collection of websites


designed to bring you helpful tips, tricks, and ideas that can save you
time, effort, and money. You can see the full breadth of the Tips.Net
network by visiting our website:

http://www.tips.net

ExcelTips: Amazing Array Formulas Page 3


Introduction

Sharing this Document


There was a lot of work that went into putting this document together. I can't tell you how many
countless hours are spent putting together the weekly ExcelTips newsletter from which much of
this information was compiled. That means that this information has value, and your friends,
neighbors, and co-workers may want to share it.

The information in this document is copyrighted. I would ask that you do not share this
information with others—you purchased this book, and you have a right to use it on your system.
Another person who has not purchased this book does not have that right. It is the sales of this
valuable information that makes the continued publishing of ExcelTips possible. If enough
people disregard that simple economic fact, the newsletter will no longer be viable or available.

If your friends think this information is valuable enough to ask you for it, they should think it is
valuable enough to purchase on their own. After all, the price is low enough that just about
anyone should be able to afford it.

It should go without saying that you cannot post this document or the information it contains on
any electronic bulletin board, Web site, FTP site, newsgroup, or … well, you get the idea. The
only place from which this document should be available is the ExcelTips Web site. If you want
an original copy, visit the Tips.Net store at the following address:

http://store.tips.net/

ExcelTips: Amazing Array Formulas Page 4


Array Formula Basics

Understanding Array Formulas


The concept of arrays is familiar to programmers: An array is a collection of values that can be
treated as a single entity. Individual values in the array can still be accessed individually, but the
entire array can also be accessed as a whole. Just because the concept is familiar to programmers
doesn't mean you have to be a programmer to use arrays in Excel. The program has a special
class of formulas called array formulas that allow you to work with arrays of variables rather
than single variables.

In fact, even if the thought of programming makes you run out of the room screaming, there is a
good chance you are already familiar with the concept of arrays—you just don't know it.
Consider the following simple data table:

A simple data table.

Take a look at the values in B2:E2 (345, 258, 321, 456). Those values can be considered an
array. It is a one-dimensional array, as it represents a series of cells in a single row. There are
many such one-dimensional arrays in this data, such as B3:E3, B4:E4, etc. In addition, single-
dimension arrays can go down columns, as well, such as B2:B5 or C2:C5.

Arrays aren't limited to a single dimension, either. Just like your data table can occupy multiple
rows and columns, so can an array. In the example above, the range B2:E5 is a two-dimensional
array made up of four columns and four rows.

An array formula operates (performs some sort of calculation) on individual items within an
array. This is similar to how Excel works with regular formulas. Consider, for example, the
following very simple formula:

=A1 * B1

ExcelTips: Amazing Array Formulas Page 5


Array Formula Basics

This results in the contents of cell A1 being multiplied by the contents of cell B1; simple enough.
With an array formula you can enter a single formula that operates on each element in an array.
Consider the following array formula:

=B2:E5 * 1.25

The result is that each cell in the range B2:E5 is multiplied by 1.25. If you try to enter this
formula into a cell, however, Excel returns an error. The reason is because you need to enter an
array formula using a special process: You type the formula and then press CTRL+SHIFT+ENTER.
(This special key combination is why some people refer to array formulas as CSE formulas.)
Excel then knows that you want the formula treated as an array formula. (This special way of
entering array formulas is very important; you will see it mentioned more than once in this
book.)

When you successfully enter an array formula, you'll also notice something special in the
Formula bar: Excel surrounds the formula with braces. Thus, the previous formula ends up
looking like this in the Formula bar:

{=B2:E5 * 1.25}

You don't type the braces; Excel adds them automatically when you press CTRL+SHIFT+ENTER.
In fact, if you try to type the braces yourself you only succeed in confusing Excel, as it won't be
able to figure out that you want to create an array formula.

Arrays can contain either numeric values or text values. In practice, most array formulas will
contain numeric values, as those are the most easily and helpfully manipulated by Excel. As
intimated earlier in this tip, there are two types of arrays that Excel can work with: one-
dimensional arrays and multi-dimensional arrays. These two types of array formulas are
discussed more fully in tips that follow.

Single-Cell Array Formulas


One of the two types of array formulas you can create in Excel is known as a single-cell array
formula. (The other is a multi-cell array formula.) Single-cell formulas are those that return a
result that can be displayed in a single cell. For example, consider the following small data table:

ExcelTips: Amazing Array Formulas Page 6


Array Formula Basics

A small inventory evaluation report.

This shows a rather common scenario: individual items with their cost and on-hand quantities. If
you wanted to know the total value of your inventory, you could place a formula to the right of
each quantity, in column D, that multiplied the cost times the quantity. Then, at the bottom of
column D, you could enter a SUM formula that summed up all the individual values. In this case
you would be creating nine new formulas—eight for each item in the inventory and a formula to
sum them all up.

The alternative is that you can use a single array formula to determine the valuation. Consider
this formula, which was entered into cell C13:

=SUM(B4:B11 * C4:C11)

Entered as an array formula (CTRL+SHIFT+ENTER), Excel multiplies each item's cost (B4:B11)
times each item's quantity (C4:C11) and then uses the SUM function to add up all the results.
The result is a single value representing the total valuation for the inventory, and it only required
a single formula instead of nine formulas. Even if your inventory table had 1,000 items in it, you
still would have only needed a single array formula as opposed to 1,001 individual formulas.

Multi-Cell Array Formulas


One of the two types of array formulas you can create in Excel is known as a multi-cell array
formula. (The other is a single-cell array formula, discussed in the previous tip.) Multi-cell
formulas are those that return a result that requires a range of cells to display. For example,
consider the following small data table, which is the same table used in the previous tip:

ExcelTips: Amazing Array Formulas Page 7


Array Formula Basics

A small inventory evaluation report.

If you wanted to know the valuation of each item in your inventory, you could figure it out by
adding a formula to the right of each item's quantity. This would require a total of eight formulas,
but you could replace those eight with a single array formula. Follow these steps:

1. Put a column heading in cell D3, such as "Value".


2. Select cells D4:D11.
3. Type this formula, but don't hit ENTER: =B4:B11 * C4:C11
4. Press CTRL+SHIFT+ENTER to signify you are entering an array formula.

What you've done is to create an array formula that multiplies each item's cost (column B) by
each item's quantity (column C). This results in eight different results and therefore requires
eight different cells (which you selected) to display the results.

Keep in mind that multi-cell array formulas always require a specific number of cells to display
their results. For instance, take a look at the following data table that shows the sales for a series
of products (column A) by different accounts (row 3):

Product Sales Report for Widgets International

ExcelTips: Amazing Array Formulas Page 8


Array Formula Basics

If you wanted to determine new sales goals for each product for each account, you could use an
array formula such as the following:

=ROUND(B4:E11 * 1.035,0)

This formula applies a 3.5% increase for all products and all accounts. If you enter this array
formula into a single cell (using CTRL+SHIFT+ENTER), you'll get a single result. But this is a
multi-cell array formula that is designed to return multiple results. What are you seeing in the
single cell?

What you see in the single cell is the first value returned by the array formula—the result of the
first cell (B4) put through the formula. Since cell B4 contains 3,671, the single value shown in
the results cell is 3,799, the rounded-down value of 3,671 * 1.035.

To see the full results of the array formula, you need to select 32 cells—the same number of cells
as there are in your base array (B4:E11). For instance, if you select cells B20:E27 and then enter
the array formula you can see all the results:

An array formula for new sales goals.

If you select too many cells for the results of the multi-cell array formula, Excel let's you know
that you selected too many by filling the extra cells with an #N/A value:

ExcelTips: Amazing Array Formulas Page 9


Array Formula Basics

Too many cells selected for the array formula.

Some Array Formula Gotchas


As you are working with array formulas, there are a few things you need to keep in mind. In the
interest of brevity and comprehensiveness, I've gathered these potential "gotcha" items all into
one place—here:

• You must enter an array formula by pressing CTRL+SHIFT+ENTER. If you simply press
Enter, chances are good that the formula will not give you the results you expect.
• Array formulas are always depicted as having {braces} around them, as they appear this
way in the Formula bar. You cannot type those braces yourself; they are added by Excel,
automatically, when you press CTRL+SHIFT+ENTER.
• If your array formula requires multiple arguments, each argument must have (or
reference) the same number of rows and columns.
• An array formula cannot use an entire column as an argument; you can only use explicit
ranges of cells.
• You cannot insert a row or column if the insert would, in some way, intersect a multi-cell
array formula.
• You cannot delete a row or column is any part of that row or column is used by a multi-
cell array formula.
• In Excel 2007 Microsoft added the ability to create defined data tables in a worksheet.
Excel won't allow you to insert a multi-cell array formula into a data table, nor can you
convert a range of cells into a data table if that range contains a multi-cell array formula.
• Array formulas should be used judiciously. If you add a lot of them (I'm talking hundreds
here) to a worksheet, it will significantly slow down recalculations and Excel may start to
appear sluggish to you.

ExcelTips: Amazing Array Formulas Page 10


Array Formula Basics

Analyzing an Array Formula


Figuring out what a regular formula is doing is rather easy. For years, experienced Excel users
have broken complex formulas into their component parts, putting each part into a separate cell,
and when each component is working properly, combining the components into the full, long
formula.

That is not nearly as easy to do with array formulas. In fact, figuring out what an array formula is
doing can be very difficult. Fortunately, Excel provides a tool that can make analyzing an array
formula a bit easier: the Evaluate Formula tool. Follow these steps:

1. Select the cell that contains the array formula. (If you are analyzing a multi-cell array
formula, select the range of cells containing the formula.)
2. Display the Formulas tab of the ribbon.
3. Click the Evaluate Formula tool in the Formula Auditing group. Excel displays the
Evaluate Formula dialog box, with the array formula in the Evaluation box.

Evaluating an array formula.

4. The portion of the formula underlined is the portion that Excel will next evaluate—right
after you click the Evaluation button. (Click the Evaluate button multiple times to step
through how Excel evaluates the array formula.)

You'll notice that as you evaluate array formulas, Excel replaces multi-cell references (such the
screen shot's reference to B2:B601) with an array of values. In this case, they are replaced with
an array of True/False values, dependent on whether each evaluated cell is equal to "F" or not.

Using the Evaluate Formula tool in this iterative manner can help you understand how Excel
interprets your array formula and calculates a result.

ExcelTips: Amazing Array Formulas Page 11


Array Formula Basics

The Evaluate Formula tool is not available in all versions of Excel. It was introduced in Excel
2002 and has only been available since then.

Editing Multi-Cell Array Formulas


There is one interesting thing to note about multi-cell array formulas: Each cell in the results
array (D4:D11 in this case) is "locked" into the array formula. If you decided that you wanted to
put some other value in cell D9, you might think you could simply select the cell and start typing
the new formula. You can try it, but the moment you press ENTER you'll see a dialog box
indicating an error.

An error dialog box displayed when trying to edit part of a multi-cell array formula.

Excel doesn't let you change part of an array; you have to change the entire array, meaning all
the cells that are used to display the results of the array formula. In order to change the array, you
need to select all of the cells in the range (D4:D11) and then do one of the following:

• Press the DELETE key to get rid of the array formula.


• Press F2 and edit the formula in the Formula bar. (Press CTRL+SHIFT+ENTER when
done.)

When finishing the edit, make sure you actually press CTRL+SHIFT+ENTER. If, instead, you press
only CTRL+ENTER, Excel places the formula into each of the cells in the selected range, but not as
an array formula. If this happens to you, select the cells, press F2 (to go into edit mode), and
immediately press CTRL+SHIFT+ENTER.

Copying an Array Formula


Part of the power of working with Excel is that you can easily copy formulas from a source cell
to a target range, and Excel obligingly adjusts references within the formulas so that they make
sense in the cells to which they are copied.

For instance, let's say you have some simple data in a worksheet, as shown here:

ExcelTips: Amazing Array Formulas Page 12


Array Formula Basics

The start of a great (but simple) data table.

In order to copy the formula from cell B20 to the rest of the cells in the data table, you just need
to select that cell, press CTRL+C, select the target range (B20:E27) and then press CTRL+V.
Works great; Excel makes the edit very quickly.

What happens, though, if cell B20 contains an array formula? If you try to follow the same steps,
Excel generates an error indicating, "You cannot change part of an array." After protesting and
saying you aren't trying to change the array, only to copy it (which protestations Excel deafly
ignores), you are left wondering how to actually copy the array formula.

The problem occurred because you included the source cell (B20) in your target range
(B20:E27). Excel doesn't like the overlap when working with array formulas. Instead, you should
do a two-stage copy, in this manner:

1. Select the cell at B20.


2. Press CTRL+C to copy the array formula to the Clipboard.
3. Select the range B21:B27. Notice that these cells are all in a single column and they do
not include the source cell.
4. Press CTRL+V to paste the array formula into the target cells.
5. Select the range B20:B27. Note that you are selecting the original source cell and all the
cells into which you pasted in steps 3 and 4.
6. Press CTRL+C to copy the range of array formulas to the Clipboard.
7. Select the range C20:E20. Note that this target range does not include the source cells;
there is no overlap.
8. Press CTRL+V to paste the array formulas to the target cells.

Excel performs the copying process without any protestation, and you end up with the desired
array formulas in the entire target range.

ExcelTips: Amazing Array Formulas Page 13


Array Formula Basics

Easily Selecting an Array Formula Range


As you work with multi-cell array formulas, it is easy to forget the full range of cells that are
linked to the array formula. (Does that array formula cover 20 cells or 24? Is it 4 columns by 5
rows or 5 rows by 4 columns? I can't remember!)

The easiest way to figure out which cells are used for an array formula is to follow these steps:

1. Select any cell in the range used by the array formula; it doesn't matter which one.
2. Press F5. Excel displays the Go To tab of the Find and Replace dialog box.
3. Click Special. Excel displays the Go To Special dialog box.

The Go To Special dialog box.

4. Select the Current Array radio button.


5. Click OK.

That's it. Excel selects all the cells that are used by the multi-cell array formula. You can then
edit the formula to your heart's content.

Array Formulas without Cells


As you start to understand how array formulas work, you might think that they work with values
stored in worksheet cells. While this is, perhaps, the most common way that people use array
formulas, you are not limited to working with call values. Indeed, array formulas can use directly
specified values.

ExcelTips: Amazing Array Formulas Page 14


Array Formula Basics

For instance, let's say you wanted to find the sum of a series of numbers multiplied by a different
series of numbers. You could place these numbers in a series of cells and then use an array
formula to derive the single result value, as was done earlier in this book. But if you couldn't put
the values into cells (perhaps you don't want to mess up the layout of the worksheet), you could
simply enter the series of numbers as part of the array formula:

=SUM({1,2,3,4} * {5,6,7,8})

In this case, the brackets are typed by you; they indicate the start of an array of values that you
are using in the formula. In this instance there are two arrays: the first is {1,2,3,4} and the second
is {5,6,7,8}. In some documentation you will see these arrays referred to as array constants. You
still enter the whole formula using CTRL+SHIFT+ENTER to designate it as an array formula. The
result is a single value of 70, which is the sum of 1 * 5 + 2 * 6 + 3 * 7 + 4 * 8.

When you enter arrays in the manner described here, they are not limited to numbers. They can
also contain text, logical values (TRUE or FALSE), and error values (such as #N/A or
#VALUE). If you want to have text values in your array, then you must surround each value with
quote marks.

Note that values in the array {1,2,3,4} are separated by commas. For Excel's purposes this
designates a horizontal array—similar to values across a row. You could also designate values
vertically by separating the values with semicolons, as in {1;2;3;4}. This is similar to values
down a column.

You can specify multi-dimension arrays by combining how you use the commas and semicolons.
For instance, a three-column by four-row array could be specified in this manner:

{1,2,3;4,5,6;6,5,4;3,2,1}

When looking at an array like this, it is easiest to note that the commas designate going across a
row and a semicolon designates starting a new row. So, the "matrix" created by the array ends up
looking like this:

1 2 3
4 5 6
6 5 4
3 2 1

Is your head hurting yet? It needn't be; you don't need to understand all of this at first. Instead,
focus on using array formulas with values that are stored in cells and you will be off to a great
start. Refer back to this tip about non-cell arrays later, when you think you might have a need for
it.

ExcelTips: Amazing Array Formulas Page 15


Array Formula Basics

Checking for Proper Entry of Array Formulas


Jeffrey's company has a number of reports that use an extensive number of CSE
(CTRL+SHIFT+ENTER) array formulas. When someone forgets to hold CTRL and SHIFT when
pressing ENTER, the resulting formulas do not equal the correct answer. Auditing each cell,
looking for the { } brackets is both tedious and time consuming. Jeffrey wonders if there is a
quick way to find the "missing brackets" or raise an error flag if CTRL+SHIFT+ENTER is not
pressed when it should be?

There is no intrinsic or formulaic method of doing this in Excel. This means that you need to turn
to a solution that is based on a macro. Fortunately, VBA offers several different ways you can
approach this problem. One approach is to simply use a formula to make sure that each formula
within a selection is actually an array formula.

Sub MakeCSE1()
Dim rCell As Range

For Each rCell In Selection


rCell.FormulaArray = rCell.Formula
Next rCell
End Sub

This macro assumes that you'll select the cells to be "converted" before actually running the
macro. If you prefer, you could define a range of cells (give the range a name) and then run a
similar macro that always does its work on that range.

Sub MakeCSE2()
Dim rng As Range
Dim rCell As Range
Dim rArea As Range

Set rng = Range("CSERange")


For Each rArea In rng.Areas
For Each rCell In rArea.Cells
If rCell.HasArray = False Then
rCell.FormulaArray = rCell.Formula
End If
Next rCell
Next rArea
End Sub

This macro looks for a range named CSERange and then checks every cell in the range. If it
doesn't contain an array formula, then the formula is converted to an array formula.

Note the use of the HasArray property to check if a cell contains an array formula. This property
can actually be helpful in other ways. For instance, you could create a simple user-defined
function, such as this:

Function NoCellArray1(rng As Range) As Boolean


NoCellArray1 = Not rng.HasArray
End Function

ExcelTips: Amazing Array Formulas Page 16


Array Formula Basics

This function returns True if the cell being pointed to doesn't contain an array formula. If it does
contain one, then False is returned. You could then use this function as the basis for a conditional
format. All you need to do is create a format that uses it in this way:

=NoCellArray1(A5)

Since NoCellArray returns True if the cell doesn't contain an array formula, your conditional
format could set the color of the cell to red or set some other visible sign that the cell doesn't
have the requisite array formula. You could also use the following function to accomplish the
same task:

Function NoCellArray2(rng As Range) As Boolean


NoCellArray2 = (Evaluate(rng.FormulaArray) <> rng.Value)
End Function

An entirely different approach is to add something to your formulas that allows them to easily be
recognized as array formulas. For instance, you could add the following to the end of any of your
array formulas:

+N("{")

This doesn't affect the computation in any way, but can be easily checked to see if it is there. The
checking can be done by an event handler, such as the following:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)


If Right(Selection.FormulaArray, 5) = "(""{"")" Then
ActiveCell.Select
Selection.FormulaArray = ActiveCell.Formula
End If
End Sub

Note that the handler checks to see if the formula ends with ("{") and, if it does, forces the
formula to be treated as an array formula. The great thing about this approach is that you'll never
have to press CTRL+SHIFT+ENTER on the worksheet again—the event handler takes care of it for
you. If, at some point, you want to convert the formula back to a regular (non-array) version,
simply modify the formula so it doesn't include +N("{").

Stopping the Deletion of Cells


Vilas knows that he can protect a worksheet so that users cannot delete cells. However, he has a
need to prevent the deletion of cells without using worksheet protection. (Vilas is not talking
about the clearing of cell contents, but the actual deletion of cells so that surrounding cells must
move left or move up.) He wonders if there is a way to prevent a user from deleting cells,
without protecting the worksheet.

There is no direct way to do this in Excel. It would be nice if Excel provided a way to create an
event handler that was called whenever a cell was deleted, but it does not. (The

ExcelTips: Amazing Array Formulas Page 17


Array Formula Basics

Worksheet_Change event is apparently triggered whenever the contents of a cell are changed,
but not when a cell is entirely deleted.) Because of this, using a macro to protect your cells from
being deleted is not the way to go.

The best solution we've been able to find involves taking advantage of a quirk in how Excel
handles array formulas. For the sake of example, let's assume that you have data in the range
A1:L37, and you don't want any cells within this range to be deleted. Follow these general steps:

1. Select the range of cells just to the right of your block you want to protect. In this case,
select cells M1:M37.
2. Type ="" and press SHIFT+CTRL+ENTER. You've now created a do-nothing array
formula that takes the entire range of M1:M37.
3. Select the range of cells just beneath the block of cells you want to protect. In this case,
select cells A38:L38.
4. Type ="" and press SHIFT+CTRL+ENTER. You've now created a do-nothing array
formula that takes the entire range of A38:L38.

At this point you cannot delete any cell within the data block (A1:L37), nor can you delete any
row 1 through 37 or any column A through L. Whenever you try, Excel displays a message that
says "You cannot change part of an array." The only way to delete cells, rows, or columns within
the data block is to first get rid of the array formulas that would be affected. In other words, you
would need to delete column M or row 38 first.

ExcelTips: Amazing Array Formulas Page 18


Using Array Formulas to Count

Using Array Formulas to Count

Counting with Two Criteria


John has a worksheet that contains records used in a cost-tracking system. Record numbers are
entered in column A, locations in column B, and costs in column C. Not all records have a cost
value entered in column C. John wants to determine a count of records "with location X and cost
<> 0".

Your first impulse may be to use one of the worksheet functions designed for counting, such as
CountIf. The only problem is that CountIf doesn't permit two conditions to be checked in
calculating a solution. There are, however, a couple of solutions you can use, without the need of
adding additional columns or intermediate calculations.

The first (and perhaps simplest) solution is to use the SUMPRODUCT worksheet function. This
function allows you to count or sum data from a column, row, or array with as many criteria as
you want. The basic syntax is as follows:

=SUMPRODUCT( (CONDITION1) * (CONDITION2) * (CONDITION3) * (DATACELLS) )

In this particular instance, you could put the formula together like this:

=SUMPRODUCT((B2:B101="X")*(C2:C101>0))

What this does is provide two different conditions that are checked. First, the cells in column B
are checked to see if they equal "X", then the corresponding cells in column C are checked to see
if they are equal to 0. Both conditions return either True (1) or False (0). These results are then
multiplied by each other, resulting in either 1 or 0. The SUMPRODUCT function then adds them
together, resulting in a cumulative count.

Another solution is to create an array formula that will do the calculation for you. Array formulas
are different than regular formulas, in that they work on a number of cells, iterating through them
to produce a result. Consider the following formula:

=(B2="X")*(C2>0)

This returns a single value, either 1 or 0. The formula uses the same basic logic described in the
earlier explanation of the SUMPRODUCT solution. The two logical comparisons return 1 or 0,
which are multiplied by each other, resulting in 1 or 0 as an answer. Now, consider the following
formula:

ExcelTips: Amazing Array Formulas Page 19


Using Array Formulas to Count

=SUM((B2:B101="X")*(C2:C101>0))

This now looks very much like the earlier SUMPRODUCT formula, but it will not work
properly as a straight formula. This is because SUM is not designed to work in an iterative
fashion on a range of cells. If you enter this formula as an array formula (press
CTRL+SHIFT+ENTER to enter it), then Excel understands you want to work through each of the
ranges, in turn, to figure the final sum, which is a count of records that meet the stated criteria.

A third option is to use the database worksheet functions to return a count. Using these, you set
up a "criteria table" in your worksheet, and then the function uses the criteria to analyze the
records. The following steps assume that the column labels for the three columns are RecNum,
Location, and Cost:

1. Find a few empty cells, either on the same worksheet as your records or on a different
worksheet. (For the sake of this example, I assume you are using columns J and K.)
2. In cell J1, enter the word Location.
3. In cell K1, enter the word Cost.
4. In cell J2, enter X.
5. In cell K2, enter >0. You have now entered your criteria table in cells J1:K2.
6. Select cells J1:K2.
7. Display the New Name dialog box (Excel 2007 and later) or the Define Name dialog
box (Excel 2003 and earlier). (Display the Formulas tab of the ribbon and then click the
Define Name tool in the Defined Names group. If you are using a version of Excel
before Excel 2007, choose Insert | Name | Define.)

The New Name dialog box.

8. Replace whatever is in the Name box with the name you want (Criteria), then click OK.
9. In the cell where you want a count of records meeting your criteria, enter the following:

=DCOUNT(B1:C101,2,Criteria)

ExcelTips: Amazing Array Formulas Page 20


Using Array Formulas to Count

Notice that the first argument used with DCOUNT is the second and third columns of your
records list. This argument also includes the column labels, which are necessary so that
DCOUNT can locate the proper criteria matches from the criteria table (third argument).

Counting Records Matching Multiple Criteria


It is not unusual to use Excel to create small databases. For instance, you might keep a list of
your poodle-breeders club members in Excel, or you might use it to maintain a list of your active
sales contacts. In those instances, you might wonder how you could get a count of the number of
records that meet more than one criterion.

Let's say that you are analyzing your membership list, and you wanted to determine a count of
the records in which the gender column contains "F" and the city column contains a particular
city, such as "Norwood". This, of course, would be helpful because it would answer the burning
question of how many female members of your group live in Norwood.

Excel includes a number of worksheet functions that are handy for determining the count of
records in a list. How you can use these in a situation where two criteria must be met may not be
immediately obvious. Let's examine five specific ways you can achieve the desired goal of
female members from Norwood. (Assume that column B is the gender column and column H is
the city column.)

The first way to solve the problem is through the use of the DCOUNTA function. This function
allows you to define a set of criteria, and use those criteria as the basis for analyzing a list of
data. Like all the data functions in Excel, DCOUNTA relies upon three parameters: the data
range, the column to use in the comparisons, and the criteria range. To use the function, set up a
criteria table in an unused area of your worksheet. For instance, you could set up the following in
cells AA1 through AB2:

Setting up criteria for the DCOUNTA function.

Then, assuming your original data table is in cells A1:K500 (obviously a large poodle breeders'
club), then you could use the following to determine the count:

=DCOUNTA(A1:K500,1,AA1:AB2)

The result is a count that meets the criteria you specified in AA1:AB2. Note, as well, that the
names you used in AA1 and AB1 must exactly match the labels you used in your table records.
When they do, the contents of the Gender column (column B) must be F and the contents of the
City column (column H) must be Norwood in order for the record to be added to the count.

ExcelTips: Amazing Array Formulas Page 21


Using Array Formulas to Count

The second solution is to use an array formula to return a single answer. The array formula
interestingly uses the SUM function and a little bit of Boolean arithmetic to determine if a record
should be counted. Consider the following:

=SUM((B2:B500="F")*(H2:H500="Norwood"))

Simply type the above formula in a cell and then finish it by pressing CTRL+SHIFT+ENTER; this
lets Excel know you are entering an array formula. The formula works because it compares the
contents of each row in the array, in turn, according to the criteria specified in the formula. It
first compares the contents of the B column with "F"; if it matches, then the comparison returns
True, which is the numeric value 1. The contents of column F are then compared to "Norwood".
If that comparison is true, then 1 is returned. Thus, 1 * 1 would equal 1, and this is added to the
SUM of the array. If either comparison is False, then the numeric value 0 is returned, and 1 * 0
equals 0 (as does 0 * 0 and 0 * 1), which doesn't affect the running SUM.

A third and closely related approach is to use the SUMPRODUCT function, but not in an array
formula. You could simply use the following in any cell where you wanted to know if the two
criteria are met:

SUMPRODUCT((B2:B500="F")*(H2:H500="Norwood"))

Remember, this is not an array formula, so you don't need to press CTRL+SHIFT+ENTER. The
formula works, again, through the magic of Boolean math.

A fourth possible solution, which is a bit more manual than those discussed already, is to use the
AutoFilter feature along with a subtotal. Assuming your data records are in A1:K500, with
column labels in row 1, you would follow these steps:

1. Select any cell in the data table.


2. If you are using Excel 2007 or a later version, display the Data tab of the ribbon and
click the Filter tool within the Sort & Filter group. If you are using an older version of
Excel, choose Filter from the Data menu, then choose AutoFilter. Excel should display
the AutoFilter drop-down indicators next to each column label in row 1.
3. Using the drop-down indicator for the gender column (column B), choose F. Your list is
automatically filtered to only display female members.
4. Using the drop-down indicator for the city column (column H), choose Norwood. Your
list is automatically displayed to only display female members living in Norwood.
5. At the bottom of the data table (row 501), in any column desired, enter the following
formula:

=SUBTOTAL(3,B2:B500)

This formula causes the SUBTOTAL function to apply the COUNTA function to derive a
subtotal. In other words, it returns a count of all records that are displayed by the filtering; this is
the count desired.

ExcelTips: Amazing Array Formulas Page 22


Using Array Formulas to Count

A fifth approach is to use the Conditional Sum Wizard to come up with a formula for you. The
Conditional Sum Wizard is available as an Excel add-in. This add-in is available in versions of
Excel up through Excel 2007; it was discontinued starting in Excel 2010. It comes enabled on
Excel 2007 by default. On older systems, choose Tools | Add-Ins to make sure that the wizard is
installed and available. If you are using Excel 2007, follow these steps to use the Conditional
Sum Wizard:

1. Select a cell somewhere within the data you want to analyze.


2. Display the Formulas tab of the ribbon.
3. In the Solutions group (right side of the ribbon) click Conditional Sum. Excel displays
the first step of the Conditional Sum Wizard. The full range of your data should already
be shown in the dialog box.

The Conditional Sum Wizard.

4. Click on Next. Excel displays the next step of the wizard.


5. In the Column to Sum drop-down list, choose Gender.
6. In the Column drop-down, choose Gender.
7. In the Is drop-down, choose the equal sign.
8. In the This Value drop-down, choose F.
9. Click on Add. The condition you specified now appears in the dialog box.
10. In the Column drop-down, choose City.
11. In the Is drop-down, choose the equal sign.
12. In the This Value drop-down, choose Norwood.
13. Click on Add. The second condition now appears in the dialog box.

ExcelTips: Amazing Array Formulas Page 23


Using Array Formulas to Count

14. Click on Next. Excel displays the third step of the wizard.
15. Choose Copy Just the Formula To a Single Cell.
16. Click on Next. Excel displays the fourth (and final) step of the wizard.
17. In the worksheet, click on the cell you want to contain the result of your formula.
18. Click on Finish.

If you are using a version of Excel prior to Excel 2007, follow these steps, instead:

1. Select a cell somewhere within the data you want to analyze.


2. Choose Conditional Sum from the Tools menu. Excel displays the first step of the
Conditional Sum Wizard. The full range of your data should already be shown in the
dialog box.

The Conditional Sum Wizard.

3. Click on Next. Excel displays the next step of the wizard.


4. In the Column to Sum drop-down list, choose Gender.
5. In the Column drop-down, choose Gender.
6. In the Is drop-down, choose the equal sign.
7. In the This Value drop-down, choose F.
8. Click on Add. The condition you specified now appears in the dialog box.
9. In the Column drop-down, choose City.
10. In the Is drop-down, choose the equal sign.
11. In the This Value drop-down, choose Norwood.

ExcelTips: Amazing Array Formulas Page 24


Using Array Formulas to Count

12. Click on Add. The second condition now appears in the dialog box.
13. Click on Next. Excel displays the third step of the wizard.
14. Choose Copy Just the Formula To a Single Cell.
15. Click on Next. Excel displays the fourth (and final) step of the wizard.
16. In the worksheet, click on the cell you want to contain the result of your formula.
17. Click on Finish.

The result is a formula, appropriate for the conditions you specified, in the cell you selected in
step 1.

There are undoubtedly countless other possible solutions you could use to figure out the count of
records. These, however, are the "pick of the lot," and allow you to determine the answer quickly
and easily.

Counting Dates in a Range


If you have a range of cells that are filled with random, non-sorted dates, you may want to figure
out how many of those dates fall between a start date and an end date. For instance, if B1:B101
contains the random dates, cell E1 contains the start date, and cell E2 contains the end date, you
may want to know what type of formula you can use in cell E4 to return the number of dates in
B1:B101 that fall between E1 and E2.

There are actually several different ways you can arrive at a solution. The first, of course, would
be to simply add a formula in each cell to the right of the dates in column B, and have that
formula return a 1 if the date is between E1 and E2, or a 0 if not. You could then sum the column
to get the desired count. You would use the following formula in each cell to the right of the
dates:

=IF(AND(B1>=E$1,B1<=E$2),1,0)

This particular formula goes into cell C1, and can then be copied down to cells C2 through C101.
Then, in cell E4, all you would need to do is use the following formula:

=SUM(C1:C101)

The drawback to this, of course, is that the additional values in C1:C101 can play havoc with the
appearance of a carefully crafted worksheet. Fortunately there are ways to find the proper result
without the need to use an intermediate value.

One way is to use an array formula. The following formula, placed in cell E4, will do the trick.
All you need to do is remember to enter the formula using SHIFT+CTRL+ENTER. (This signifies to
Excel that you are entering an array formula.)

=COUNT(IF((B1:B101>$E$1)*(B1:B101<$E$2),B1:B101))

ExcelTips: Amazing Array Formulas Page 25


Using Array Formulas to Count

If you prefer to not use an array formula, you can use the following standard COUNTIF-based
formula in E4:

=-COUNT(B1:B101)+COUNTIF(B1:B101,">"&E1)+COUNTIF(B1:B101,"<"&E2)

When you enter this formula, Excel will assume that you are returning a date value, and will
therefore helpfully format the cell as a date. All you need to do to correct this is to use Format |
Cell to format the cell using something besides a date format, such as General.

The formula works by counting all dates that are after the early date plus all dates that are before
the late date. This essentially counts all dates once and double-counts the desired selection. By
subtracting the number of dates in the range (at the beginning of the formula), the formula
effectively eliminates all but the desired result.

If you prefer, you can also use the DCOUNT function to return the desired count. This, however,
is just a bit more involved. Unlike the earlier solutions, DCOUNT relies on the use of named
ranges. Follow these steps:

1. Place a blank row at the top of your data, which pushes everything down by one cell.
2. With your dates now in cells B2:B102, select cell B1 and place a label, such as
MyDates, in the cell.
3. Format cell B1 as desired.
4. Select the entire range, including the label (cells B1:B102).
5. Using the Name Box (just above the row headers and to the left of the column headers),
define a name for the range. The name should be the same as the name you used in step
2 (MyDates).
6. In cells D1 and E1, place your label name (MyDates).
7. In cell D2 enter a greater-than sign and the lower date for your range of dates, as in
">4/1/2002" (without the quote marks).
8. In cell E2 enter a less-than sign and the upper date for your range of dates, as in
"<6/1/2002" (without the quote marks).
9. In cell E4, enter the following formula:

=DCOUNT(MyDates,1,D1:E2)

The DCOUNT function uses the criteria you specified in D1:E2 to examine the data range
defined as MyDates and return the count of cells that meets those criteria.

Counting Unique Values with Functions


David has a worksheet in which there is a list of countries. This list, in cells A1:A100, can
contain duplicates. David wants to determine the number of unique countries in the list.

ExcelTips: Amazing Array Formulas Page 26


Using Array Formulas to Count

There are several ways you can go about deriving a count. The method you should use depends
on the characteristics of the data in the list. A good place to start, however, is to define a named
range that represents the list of countries. In the following examples, it is assumed that the range
is named Countries. (Catchy name, huh?)

If the list contains only text entries and does not contain any blank cells, then the following will
provide a count:

=SUM(1/COUNTIF(Countries,Countries))

This should be entered as an array formula, by pressing CTRL+SHIFT+ENTER. If the list contains
blank cells, then the formula will be a little different. The following long array formula will work
if there are blanks:

=SUM((Countries<>"")/(COUNTIF(Countries,Countries)+(Countries="")))

Another array function works, but the formula is a little more complicated:

=SUM(IF(FREQUENCY(IF(LEN(Countries)>0,MATCH
(Countries,Countries,0),""), IF(LEN(Countries)
>0,MATCH(Countries,Countries,0),""))>0,1))

This approach—using the FREQUENCY function—is fully recounted in the Microsoft


Knowledge Base:

http://support.microsoft.com/?kbid=100122

If you prefer to not use array formulas (for whatever reason), then you can utilize a blank column
to the right of your list. This column will contain regular formulas that indicate if the value to its
left is unique in the list or not. The first time a value appears, the formula returns the number 1.
On each subsequent appearance of the same value, the formula returns a 0. Start by sorting your
list, and then place the following formula in cell B1:

=IF(ISNA(VLOOKUP(A2,$A$1:A1,1,)),1,0)

Just copy the formula from B1 to the range B2:B100. With these results in place, you can easily
sum column B and have a count of the unique values in the list.

Counting Unique Values


Sometimes you need to know the number of unique values in a range of cells. For instance,
suppose that an instructor was teaching the following classes:

104-120
104-101
104-119

ExcelTips: Amazing Array Formulas Page 27


Using Array Formulas to Count

104-120

In this case there are three unique values. There is no intuitive worksheet function that will return
a count of unique values, which makes one think that a user-defined function would be the
logical approach. However, you can use an array formula to very easily derive the desired
information. Follow these steps:

1. Define a name that represents the range that contains your list. (This example assumes
the name you define is MyRange.)
2. In the cell where you want the number of unique values to appear type the following
formula, but don't press ENTER yet:
=SUM(1/COUNTIF(MyRange,MyRange))

3. Instead of pressing ENTER, press CTRL+SHIFT+ENTER. This informs Excel that you are
entering an array formula. The formula shown in the formula bar should now appear as
follows (notice the addition of the surrounding braces, indicative of array formulas):
{=SUM(1/COUNTIF(MyRange,MyRange))}

That's it! The cell now contains the number of unique name values in the specified range. This
approach is not case-sensitive, so if you have two values that differ only in their capitalization
(ThisName vs. THISNAME), they are both counted as a single unique value. In addition, there
can be no blank cells in the range. (Having a blank cell returns a #DIV/0 error from the formula.)

Counting Cells with Specific Characters


Let's say that you have a worksheet that contains all the people who have ever worked in your
department. Each name is prefaced by a single character that indicates the status of the person.
For instance, if Fred Davis were retired, his name might show up as "RFred Davis". With quite a
lot of these names in the worksheet, you may need a way to count those people with a specific
status character.

The easiest way to accomplish this is to use the COUNTIF function. If, for instance, the status
character is the letter R (for "retired"), and your range of names is in cells A5:A52, then you
could use the following to determine which cells begin with the letter R:

=COUNTIF(A5:A52,"R*")

The formula works because the comparison value is R*, which means "the letter R followed by
any other characters." Excel dutifully returns the count. To search for a different status character,
simply replace R with the desired status character.

Obviously, if the asterisk has a special meaning in this usage, you can't search directly for an
asterisk. Actually, there are three characters you cannot search for directly: the asterisk (*), the
question mark (?) and the tilde (~). If you want to search for any of these characters, you must

ExcelTips: Amazing Array Formulas Page 28


Using Array Formulas to Count

precede the character with the tilde. Thus, if you wanted to determine a count of names that had a
question mark as a status code, you could use the following:

=COUNTIF(A5:A52,"~?*")

An alternative to using COUNTIF is to create an array formula that is applied to every cell in the
range. The following will do the trick very nicely:

=SUM((LEFT(A5:A52,1)="R")*1)

This must, of course, be entered as an array formula. This means that instead of pressing ENTER
at the end of the formula, you would press SHIFT+CTRL+ENTER. The formula checks the left-
most character of a cell, returning the value TRUE if it is R or FALSE if it is not. The
multiplication is done to convert the TRUE/FALSE value to a number, either 1 for TRUE or 0
for FALSE. The SUM function returns the sum, or count, of all the cells that meet the criteria.

One final note: The formulas provided in this tip are a way to deal with the data as originally
presented at the first of the tip. If you have any control at all over your data, you really should
consider removing the "R" (or any other leading status character) from the names. Those sort of
characters really deserve their own column, instead of complicating the name field. Use the Text
to Columns tool to separate out the "R" and any other leading characters, and then you can more
easily work with them in your formulas.

Counting Groupings Below a Threshold


Ronald imports a number of signal-level measurements as a series of values into Excel. He needs
to count how many consecutive groups of values exist in this series that fall below a certain
threshold. For example, he may have the following measurements:

27, 22, 22, 30, 32, 18, 22, 23, 28, 39, 24, 27, 35, 25, 21

If he wants to know the number of groupings where the members of those groupings were under
26, the answer would be 4. Note that this is the groupings of consecutive values below 26, not
the number of individual values below 26. Thus, in this case, the four groupings would be shown
by the brackets in the following:

27, [22, 22], 30, 32, [18, 22, 23], 28, 39, [24], 27, 35, [25, 21]

Ronald is wondering what sort of formula he can use to figure out the number of groupings that
fall below some arbitrary threshold he might specify.

There are actually several different ways you can approach this. The first is to use a "results
column" that essentially notes changes in threshold and sequence grouping. For instance, if you
had the above values in column A of a worksheet (starting at cell A2) and the threshold value in

ExcelTips: Amazing Array Formulas Page 29


Using Array Formulas to Count

cell E1, then you could use the following formula in every cell to the right of a value in column
A:

=IF(A2>=$E$1,B1,IF(A1<$E$1,B1,B1+1))

The formula keeps a running sum of the groups below the threshold. The max (or last value) of
column B provides the total number of groups below the threshold. The formula checks to see
whether the value immediately to the left, in column A, is above or below the threshold. If it's
above, or if not and the previous value in column A was also below, then it doesn't increment the
running sum. Otherwise, it does increment because a new grouping is starting.

A related way of doing the count is to use this formula in column B, instead:

=IF(A2>=$E$1,0,IF(A1<$E$1,0,1))

This results in column B containing a series of 0 or 1 values. The only time that a 1 value occurs
is at the start of a series that is below the threshold. This makes it easy to sum all the values in
column B, which provides the count of groupings.

If you don't want to use the results column, you can use an array formula to figure out the count.
The following formula assumes, again, that the values to be analyzed are in column A, beginning
at A2, and that the threshold value is in cell E1. Remember, as well, that array formulas are
entered by pressing CTRL+SHIFT+ENTER.

=SUM(IF((A2:A16<$E$1)*((A2:A16<$E$1)*1<>((A1:A15<$E$1)*ISNUMBER(A1:A15))),1))

The formula basically does what the previous results-column formula did (determines a 0 or 1
based on whether a below-threshold grouping is starting) and then sums those values.

Counting Commas in a Selection


At work, Mark regularly needs to count the number of commas in a range of selected cells. He
can't find an Excel function to do this type of task and is wondering if a macro might be able to
do the trick.

A macro isn't really necessary. While there is no worksheet function that will produce the desired
count, there is a formula or two you can use. If you just want to know the number of cells that
have at least one comma in them, the following formula will work just fine:

=COUNTIF(A1:A10,"*,*")

If you, instead, need to figure out the number of commas in the range when there could be
multiple commas per cell, then you need to use a different formula:

=SUM(LEN(A1:A10))-SUM(LEN(SUBSTITUTE(A1:A10,",","")))

ExcelTips: Amazing Array Formulas Page 30


Using Array Formulas to Count

This formula should be entered as an array formula, which means that you should use
CTRL+SHIFT+ENTER to enter the formula. If you need to derive the count for a different range,
just change the range in two places in the formula.

Using a formula like this will count commas that are actually in the cell. It will not count
commas that appear to be in the cell because of formatting. For instance, if a number appears as
"1,234" in a cell, chances are good that the comma is there because of the way that the cell is
formatted; it is not really in the cell itself. Such commas are not counted.

Of course, if all you need to do is know the number of commas and you don't need the value in
your worksheet, you can bypass the formula all together. Follow these general steps:

1. Select the range of cells in which you want to count commas.


2. Press CTRL+H to display the Replace tab of the Find and Replace dialog box.

The Replace tab of the Find and Replace dialog box.

3. In the Find What box, enter a comma.


4. In the Replace With box, enter a comma.
5. Click Replace All.

Excel does the replacement and displays a dialog box that shows how many replacements were
made.

Counting Wins and Losses


Graham has, in Excel, created a matrix of player names for his league. Cells B2:H2 contain the
names of the players, as do cells A3:A9. At each intersection in the matrix, Graham places a "W"
or "L" to indicate whether the match-up resulted in a win or loss for the player in each row. If a
player plays another person more than once, then a cell contains a "W" or "L" for each game.
Graham was wondering what formula could be used, starting in column I, to indicate the number
or wins and losses for each player.

ExcelTips: Amazing Array Formulas Page 31


Using Array Formulas to Count

There are a number of ways you can get the desired information. One is to use this type of
formula:

=LEN(SUBSTITUTE(B3&C3&D3&E3&F3&G3&H3,"L",""))

This formula calculates the number of non-L characters in row 3—in other words, the number of
wins. It does this by concatenating the contents of B3:H3, and then using the SUBSTITUTE
function to remove all the Ls. This leaves the Ws, which are counted by the LEN function. You
could also use the CONCATENATE function, in the following manner, for the same result:

=LEN(SUBSTITUTE(CONCATENATE(B3,C3,D3,E3,F3,G3,H3),"L",""))

To calculate the number of losses, simply replace "L" in each formula with "W".

You can also use an array formula, which allows you to specify a range of cells to examine,
rather than needing to specify every single cell:

=SUM(LEN(SUBSTITUTE(B3:H3, "L","")))

This array formula, entered by pressing CTRL+SHIFT+ENTER, returns the number of wins (W
characters) in the range B3:H3.

Getting a Count of Unique Names


John has a worksheet that he uses for registration of attendees at a conference. Column A has a
list of each person attending, and column B has the company represented by each attendee. Each
company can have multiple people attend. John can easily figure out how many individuals are
coming to the conference; it is simply the number of rows in column A (minus any header rows).
The more difficult task is to determine how many companies are going to be represented at the
conference.

There are a couple of ways to determine the desired count. First, if there are no blank cells in
column B, you can use an array formula (entered by CTRL+SHIFT+ENTER) such as the following:

=SUM(1/COUNTIF(B2:B50,B2:B50))

If there are blanks in the range (B2:B50 in this case), then the array formula will return a
#DIV/0! error. If that case, the array formula needs to be changed to the following:

=SUM(IF(FREQUENCY(IF(LEN(B2:B50)>0,MATCH(B2:B50,B1:B50,0),
""),IF(LEN(B2:B50)>0,MATCH(B2:B50,B2:B50,0),""))>0,1))

If you prefer to not use an array formula, you can add regular formulas to column C to do the
count. First, sort the table of data by the contents of column B. That way the data will be in

ExcelTips: Amazing Array Formulas Page 32


Using Array Formulas to Count

company order. Then add a formula such as the following to cell C2 (assuming you have a
header in row 1):

=IF(B2<>B3,1,0)

Copy the formula down through all the rest of the cells in column C, and then do a sum on the
column. The sum represents the number of unique companies attending, since a 1 only appears in
column C when the companies change.

Of course, if you need to find the names of all the companies represented at the conference, you
can use Excel's filtering capabilities. Follow these steps:

1. Sort the data by column B, the company names.


2. Select all the cells containing data (including the header cell) in column B.
3. Display the Advanced Filter dialog box. (Display the Data tab of the ribbon and then
click the Advanced tool, in the Sort & Filter group. If you are using a version of Excel
before Excel 2007, choose Filter from the Data menu, then choose Advanced Filter.)

The Advanced Filter dialog box.

4. Make sure the Copy to Another Location radio button is selected.


5. Make sure the Unique Records Only check box is selected.
6. With the insertion point in the Copy To box, click on a blank cell, such as E1. (This is
where the list of companies will be copied to.)
7. Click OK. Excel copies the unique company names from the original list to column E.

You now can easily see how many companies are being represented, along with who those
companies are.

ExcelTips: Amazing Array Formulas Page 33


Using Array Formulas to Count

Counting Asterisks
David needs to count the number of asterisks that appear in a range of cells. He notes that
COUNTIF appears to assume that * is a wild card character, so it doesn't return the proper count.

There are a number of ways to get results, based upon what it is you actually want to get. Let's
assume that you have the following values in cells A3:A8:

• 1234
• abcd
• ab*cd
• ab*c*d
• *
• ***

In these six cells there are a total of seven asterisks. To determine the number of asterisks
appearing within the range, you'll need to rely upon an array formula, such as this one:

=SUM(LEN(A3:A8)-LEN(SUBSTITUTE(A3:A8,"*","")))

Remember to enter the formula with CTRL+SHIFT+ENTER.

Of course, you might want to count the number of cells in the range that contain a single asterisk
instead of the number of actual asterisks. In this case you can actually use the COUNTIF
function, provided you know how to put the formula together. First, try this formula:

=COUNTIF(A3:A8,"*")

With the data shown at the beginning of this tip, this formula returns the value 5. This, of course,
is wrong. The reason it returns this result is because COUNTIF uses * as a wildcard that means
"any text in the cell." Since there are five cells in the range that contain text (non-numeric
values), that is the answer returned by the formula.

You might think that if you searched for the ANSI character of the asterisk, instead of the
asterisk itself, you could get the correct result. This formula shows this approach:

=COUNTIF(A3:A8,CHAR(42))

This formula also returns the incorrect answer (5). It appears that Excel sees no difference, in
application, between searching for * and searching for CHAR(42). Both are still treated as a
wildcard.

The solution to this is to remember that you can force Excel to treat the asterisk as an actual
character by preceding it with a tilde, character, in this manner:

ExcelTips: Amazing Array Formulas Page 34


Using Array Formulas to Count

=COUNTIF(A3:A8,"~*")

This returns a result of 1, which may be surprising. Excel is very literal, however, and your
formula asked for a count of all the cells which contain a single asterisk. The correct answer is
that only one cell (A7) contains what you asked for. If you want to count all the cells that contain
an asterisk anywhere within the cell, then you need to surround the formula with wildcard
characters, in this manner:

=COUNTIF(A3:A8,"*~**")

This returns "any text" followed by a literal asterisk followed by "any text." The result is 4,
which is the number of cells that contain at least one asterisk.

The concept of using tildes to counteract wildcards is covered in this Knowledge Base article:

http://support.microsoft.com/?kbid=214138

Counting Asterisks in a Column


Steven uses Excel for a personalized movie database. In one column he has the rating he's given
to each movie on a one-to-four scale, defined by using one, two, three, or four asterisks. He
wants to add info showing how many '*' movies he has, how many '**' movies he has, etc.
Steven has figured out how to count the number of '*' movies, but when he tries to count how
many '**' movies are in the column the figure is wrong.

There are multiple ways that this can be handled, but first a comment about the choice of
asterisks for a purpose such as this: In the grand scheme of things that is Excel, the asterisk has
many purposes. It is used most often in formulas as the multiplication symbol and, almost as
often, as a wildcard symbol in many formula arguments. For this reason, it is not a particularly
good idea to use the asterisk for other purposes, particularly for items that you want to count, as
with the movie ratings. It may be better, in this case, to simply use a number 1 through 4 for the
ratings, because the numbers are very easy to work with and are unambiguous in their usage.

If you must use asterisks, then there are several ways you can put together a formula to do the
counts. The SUMPRODUCT function will do the work nicely. Assuming the asterisks are in
column C, you could use the following:

=SUMPRODUCT(--(C:C="*")*1)
=SUMPRODUCT(--(C:C="**")*1)
=SUMPRODUCT(--(C:C="***")*1)
=SUMPRODUCT(--(C:C="****")*1)

You could also use SUMPRODUCT a bit differently to simply check the length of whatever is in
column C. This approach works well if C contains just asterisks, but will also work if you use
something different than asterisks:

ExcelTips: Amazing Array Formulas Page 35


Using Array Formulas to Count

=SUMPRODUCT(--(LEN(C:C)=1))
=SUMPRODUCT(--(LEN(C:C)=2))
=SUMPRODUCT(--(LEN(C:C)=3))
=SUMPRODUCT(--(LEN(C:C)=4))

You can also use the straight SUM function, but the following formulas must be entered using
CTRL+SHIFT+ENTER. (They are array formulas.)

=SUM(IF(C:C="*",1,0))
=SUM(IF(C:C="**",1,0))
=SUM(IF(C:C="***",1,0))
=SUM(IF(C:C="****",1,0))

Note that the examples so far use summing functions, specifically SUMPRODUCT and SUM.
These functions don't have the problem mentioned earlier of misunderstanding the asterisk. You
do run into the problem when using counting functions, though. For instance, the following will
not give the desired results:

=COUNTIF(C:C,"*")

The asterisk functions as a wildcard character, matching anything in a cell. Thus, you end up
with a count of all cells in column C that contain anything. You can specify that you want the
asterisk treated as a literal character (instead of as a wildcard) by preceding it with a tilde, in this
manner:

=COUNTIF(C:C,"~*")

When it comes to two asterisks, you might think that this will work:

=COUNTIF(C:C,"~**")

It won't; Excel interprets this as "a single literal asterisk followed by anything." In other words,
the first asterisk is literal and the second is still a wildcard. It is each of the asterisks which must
be preceded by tildes, in this manner:

=COUNTIF(C:C,"~*")
=COUNTIF(C:C,"~*~*")
=COUNTIF(C:C,"~*~*~*")
=COUNTIF(C:C,"~*~*~*~*")

Finally, if you want to skip using formulas all together, you could create a PivotTable that
references the rating column for your movies. If you use that column as a row in the PivotTable
and change the aggregation method so that it does a count of contents of the cells in the column,
you can get a nice summary of how many of each type of rating you've awarded your movies.

ExcelTips: Amazing Array Formulas Page 36


Using Array Formulas to Count

Counting Odds and Evens


Pini has a range of whole numbers (let's say C2:J2) and some of the numbers can be odd and
some even. He would like a formula to count the number of odd values in the range and another
to count the number of even values in the range.

There are a couple of ways to derive the desired counts, without resorting to intermediate values.
One way is to use an array formula, such as the following, to determine a count of odd values:

=SUM((MOD(C2:J2,2)<>0)*1)

Enter the formula using CTRL+SHIFT+ENTER and you get the desired count. Only a small change
is required for the array formula to return the count of even numbers:

=SUM((MOD(C2:J2,2)=0)*1)

If you prefer to not use an array formula, you could use SUMPRODUCT to return the same
count, as a regular formula. For example, here is the formula to return a count of odd values:

=SUMPRODUCT(--(MOD(C2:J2,2)=1),--(C2:J2<>""))

This is the formula for even values:

=SUMPRODUCT(--(MOD(C2:J2,2)=0),--(C2:J2<>""))

Another advantage of using the SUMPRODUCT approach is that it compensates for possible
blank cells in your range. The earlier array formulas will always count blank cells as if they
contain an even value.

Finding Odd Values Greater Than 50


Amol has 1,000 values in an Excel worksheet, occupying 100 rows of 10 columns each. Each
value in this range is an integer value between 0 and 99. Amol needs a way to count and display
all the values which are odd and greater than 50.

There are a few ways you can go about counting and displaying, but it is important to understand
that these are different tasks. Perhaps the best way to display those values that fit the criteria is to
use conditional formatting. You can add a conditional formatting rule to each cell that will make
bold or otherwise highlight the desired values. Follow these steps:

1. Select the cells that contain your data.


2. Display the Home tab of the ribbon.
3. Click the Conditional Formatting tool in the Styles group. Excel displays a palette of
options related to conditional formatting.

ExcelTips: Amazing Array Formulas Page 37


Using Array Formulas to Count

4. Click New Rule. Excel displays the New Formatting Rule dialog box.
5. In the Select a Rule Type area at the top of the dialog box, choose Use a Formula To
Determine Which Cells to Format.

The New Formatting Rule dialog box.

6. In the formula box enter the formula =AND(MOD(A1,2),A1>50).


7. Click the Format button. Excel displays the Format Cells dialog box.

ExcelTips: Amazing Array Formulas Page 38


Using Array Formulas to Count

The Format Cells dialog box.

8. Use the controls in the dialog box to modify the formatting, as desired.
9. Click OK to close the Format Cells dialog box.
10. Click OK to close the New Formatting Rule dialog box. The formatting is applied to the
range of cells you selected in step 1.

If you prefer, you could also use the following formula in step 6:

=AND(ISODD(A1),A1>50)

If you are using a version of Excel earlier than Excel 2007, then the steps to create the
conditional format are different:

1. Select the cells that contain your data.


2. Choose Conditional Formatting from the Format menu. Excel displays the Conditional
Formatting dialog box.
3. Using the first drop-down list for Condition 1, choose Formula Is.

ExcelTips: Amazing Array Formulas Page 39


Using Array Formulas to Count

The Conditional Formatting dialog box.

4. In the formula box for Condition 1, enter the formula =AND(MOD(A1,2),A1>50).


5. Click the Format button for Condition 1. Excel displays the Format Cells dialog box.
6. Use the controls in the dialog box to modify the formatting, as desired.
7. Click OK to close the Format Cells dialog box.
8. Click OK to close the Conditional Formatting dialog box. The formatting is applied to
the range of cells you selected in step 1.

So far we've demonstrated how to display the target values using conditional formatting. To get
the count of cells that fit the criteria, you could use an array formula:

=SUM(--((MOD(MyCells,2))*(MyCells>50)))

This formula assumes that the range of cells you want to analyze are named MyCells. Don't
forget to enter the cell using CTRL+SHIFT+ENTER. If you don't want to use an array formula, you
could use the following:

=SUMPRODUCT(--(MOD(MyCells,2)*(MyCells>50)))

Getting a Conditional Count of Cells Containing


Values
The formula =SUMIF(B1:B100,"Current",D1:D100) provides the sum of the values in column
D, provided the corresponding cell in column B contains the text "Current". What Kenneth
actually needs, though, is a count of the values in column D when column B contains "Current".
(The count of values in D may well be different than the number of instances of "Current" in B.)
He wishes it was as simple as changing SUMIF to COUNTIF, but that produces an error.

The reason the error occurs is because SUMIF requires three parameters and COUNTIF requires
only two. Thus, if you simply replace SUMIF with COUNTIF, then you'll get an error. Instead,
you could use COUNTIF in this way:

=COUNTIF(B1:B100,"Current")

ExcelTips: Amazing Array Formulas Page 40


Using Array Formulas to Count

This will not give you an error, but it also won't give you a correct answer. Kenneth wants a
count of the number of cells containing values in the range D1:D100, but only if the
corresponding cell in column B contains the text "Current." The COUNTIF formula doesn't even
take column D into effect; it only counts the number of cells in the range B1:B100 that contain
the word "Current."

The solution is to use the COUNTIFS function, instead. This function allows you to check
multiple conditions in order to derive a count. In this case, the following version would work:

=COUNTIFS(B1:B100,"current",D1:D100,">0")

This formula counts the number of values in D1:D100 that are greater than zero. (Well, it also
only counts them if B1:B100 contains "current.") This works because empty cells or cells that
contain text are considered by COUNTIFS to be equivalent to 0. Note, as well, that the function
is case-insensitive when it comes to matching: "current" will also match "Current" or any
combination of uppercase and lowercase letters in the word.

If your cells might contain negative values, then you should try this variation, instead:

=COUNTIFS(B1:B100,"current",D1:D100,"<>")

The drawback is that this method also includes in the count any cells that contain text.

You could also use the following formula:

=SUMPRODUCT(--(B1:B100="current"),--(D1:D100<>""))

Again, this one includes text values in the count. If you want to ignore the text values, then this
variation works great:

=SUMPRODUCT(--(B1:B100="current"),--ISNUMBER(D1:D100))

The following formula can also be used, provided you enter it as an array formula (press
CTRL+SHIFT+ENTER):

=SUM((B1:B100="current")*ISNUMBER(D1:D100))

There are also other ways you could derive the total you need. One method would be to use the
DCOUNT function (which counts based upon multiple criteria), but that approach requires more
setup than the formulas already described.

ExcelTips: Amazing Array Formulas Page 41


Using Array Formulas with Numbers

Using Array Formulas with Numbers

Summing Digits in a Value


If you have a cell that contains a value, you may want to devise a way to add together all the
digits in the value. For instance, if a cell contains the value 554, you might want to determine the
sum of 5+5+4, which is 14.

There are several ways you can approach this task. (Doesn't that always seem the way in Excel?)
The first is to use a formula that relies on several functions:

=SUMPRODUCT(--MID(A1,ROW(INDIRECT("1:" & LEN(A1))),1))

This regular formula will sum the digits in any integer value (in cell A1) in a simple, elegant
manner. This is not the only possible formula, however. The following is an array formula
(terminated by pressing CTRL+SHIFT+ENTER) version of the same formula:

=SUM(1*MID(A1,ROW(INDIRECT("1:" & LEN(A1))),1))

Either of these formulas works fine if the value in A1 is a positive whole number. If there are any
non-digit characters in the number (such as a negative sign or a decimal point), then the formulas
return a #VALUE! error. If you suspect that the number will contain any non-digit characters,
you could use a more complex formula:

=SUM(IFERROR(1*MID(A1,ROW(INDIRECT("1:" & LEN(A1))),1),0))

This formula will only work in Excel 2007 or later versions, as it uses the IFERROR function.
(That function is not available in earlier versions of Excel.)

These are not the only formulas possible for this type of calculation. You can find some other
examples of formulas in the Microsoft Knowledge Base:

http://support.microsoft.com/?kbid=214053

Summing Absolute Values


Joseph has a worksheet that contains a list of values. Some of those values are above zero and
others are below. He can use the SUM function to calculate a sum of the values, but he really

ExcelTips: Amazing Array Formulas Page 42


Using Array Formulas with Numbers

wants to calculate a sum of the absolute value of each item in the list. So, the sum of the three
values -33, 14, -5 would be 52 instead of -24.

There is no intrinsic function you can use to create the desired sum, but you can create a formula
to perform the task. One method is to use the SUMIF function, in the following manner:

=SUMIF(A1:A10,">0")-SUMIF(A1:A10,"<0")

The first SUMIF sums all the values that are greater than zero, and the second sums all those less
than zero. Thus, with the four values -33, 14, -5, 42, the first SUMIF would result in a sum of 56
(14 + 42) and the second would result in a sum of -38 (-33 + -5). When you subtract the second
sum from the first (56 - -38) you get a final answer of 94, which is the sum of all the absolute
values.

Another approach is to use the SUMPRODUCT function. The following formula will produce
the desired result:

=SUMPRODUCT(ABS(A1:A10))

The function is typically used to multiply different elements of arrays by each other, and then
sum those products. Since only one array (A1:A10) is provided, there is no multiplication done,
but a sum of the desired absolute values is returned.

You can also get the desired result by using an array formula, a convenient but seldom used
feature of Excel. Assuming your values are in the range A1:A10, type this formula:

=SUM(ABS(A1:A10))

Don't press ENTER; instead press CTRL+SHIFT+ENTER, which signifies this is an array formula. If
the formula is entered correctly, you'll see braces around the formula in the Formula bar:

{=SUM(ABS(A1:A50))}

What the formula does is internally create the intermediate column (which is an array of values)
which are the individual absolute values of A1:A10. It then sums this array and displays the
result.

Summing Every Fourth Cell in a Row


Kevin needs to create a formula that sums every fourth cell in a row. He knows he can use a
formula such as =A6+E6+I6+M6, etc., but this becomes cumbersome if there are a lot of
columns in the worksheet.

There are several ways you can approach this problem. One way is to add some additional
information to the worksheet to designate which cells should be included in the sum. For

ExcelTips: Amazing Array Formulas Page 43


Using Array Formulas with Numbers

instance, in the example you are interested in summing cells in row 6 of the worksheet. If you
can add some indicators in row 5, these could then be used a "triggers" in a formula. Put the
number 1, for example, above each cell you want included in the sum (columns A, E, I, M, etc.).
Then, you can use a formula such as the following:

=SUMPRODUCT(A5:X5, A6:X6)

The formula basically multiples whatever is in row 5 against row 6, and then sums the results.
Since there are only 1s in the columns you want summed, these are all that are included in the
final sum.

If you don't want to add an indicator row to your worksheet, then you need to look at different
solutions. You could still use the SUMPRODUCT function in a formula such as the following:

=SUMPRODUCT((MOD(COLUMN(6:6),4)=1)*(6:6))

This formula relies on the MOD function to return the remainder of a division. In this case, what
is being divided is the column number of a cell by the value 4. This will result in a remainder of
either 0, 1, 2, or 3. Every fourth cell in a row will have the same remainder. Thus, column A
(also known as column 1) will have a MOD value of 1 (1 divided by 4 is 0, with 1 left over), as
will columns E, I, M, etc.

Note that the formula compares whether the MOD value is 1 or not. If it is, then the comparison
returns True (1); if it isn't, then it returns False (0). This is then multiplied against the cell in the
sixth row. Finally, SUMPRODUCT sums all these multiplications and gives the desired result.

While this formula provides the sum of every fourth cell in the sixth row, it could easily be
changed to provide the sum for every third cell, fifth cell, or whatever interval you want. Simply
change the 4 in the MOD function to the interval desired.

If you wanted to select a different cell in each "cluster" of four cells to be summed, then all you
need to do is change the value being compared in the MOD function. In this example, only the
first cell in each cluster of four will have a MOD of 1 (A, E, I, M, etc.). If you instead want to
sum every fourth cell starting with, say, cell C, then you would change the comparison value
from 1 to 3. Why? Because C is the third cell in the cluster and will have a MOD of 3, as will
each fourth cell thereafter (G, K, O, etc.).

The only "gotcha" to this general rule is if you want to sum the fourth cell in each four-cell
cluster. For instance, you might want to sum cells D, H, L, P, etc. In this case the comparison
value used wouldn't be 4 since there will never be a remainder of 4 when doing a MOD
operation that involves dividing by 4. Instead, the comparison value would be 0, as in the
following:

=SUMPRODUCT((MOD(COLUMN(6:6),4)=0)*(6:6))

If you prefer to work with array formulas, you can use a slightly shorter variation on the above
formula:

ExcelTips: Amazing Array Formulas Page 44


Using Array Formulas with Numbers

=SUM(IF(MOD(COLUMN(6:6),4)=1,6:6))

Note that the formula should be entered by pressing CTRL+SHIFT+ENTER. It will then appear in
the Formula bar with braces ({ }) around the formula. The same modification notes relative to
the MOD divisor and comparison value apply here as they did with the SUMPRODUCT
function.

Both of these formulaic approaches (SUMPRODUCT and the array formula) sum every fourth
cell in the entire row. If you instead want to limit the cells from which the sum is derived to a
portion of the row, then simply replace 6:6 (both instances) with the proper range. Thus, if you
wanted to only sum every fourth cell in the range of A6:Z6, you would use that range in the
formula.

If you do a lot of summing in this manner, and you apply it not only to ranges in a row but
ranges in a column, you may want to consider creating a user-defined function to do the
summing. The following simple function will do the trick:

Function SumEveryFourth(MyRange As Range)


Dim x As Integer
SumEveryFourth = 0
For x = 1 To MyRange.Cells.Count
If (x Mod 4) = 1 Then
SumEveryFourth = SumEveryFourth + MyRange.Cells(x).Value
End If
Next x
End Function

The function examines the range passed to it, and then sums every fourth cell starting with the
first cell in the range. If you prefer to have it sum every second cell in the range, then change the
comparison value in the If statement, as discussed earlier in this tip. (Since the Mod operation is
used in this function, and it operates the same as the MOD worksheet function, then the same
comparison values come into play for determining which cell in each cluster should be summed.)

The user-defined function will work just fine on either cells in a row or cells in a column. You
simply need to make sure that you pass it the range you want, as demonstrated here:

=SumEveryFourth(C3:C57)

Summing Based on Part of the Information in a Cell


Kathy has a worksheet that includes information for all the parts in her warehouse. In this sheet,
part numbers are shown in column A using the format 12345 XXX, where XXX represents a
location code. This means she could have multiple entries on the worksheet for the same part
numbers, but each entry representing a different location for that part. Kathy needs a formula that
sums the values associated with each part number, regardless of its location code. Thus, she
needs a way to sum the quantity column related to parts 12345 ABC, 12345 DEF, 123456 GHI,
etc. She needs a way to do this without splitting the location code to a different column.

ExcelTips: Amazing Array Formulas Page 45


Using Array Formulas with Numbers

There is more than one way to get the desired answer. For the sake of the examples in this tip,
assume that the part numbers are in column A (as Kathy indicated) and that the quantities for
each part are in column B. It is these quantities that need to be summed, based upon just a
portion of what is in each cell in column A. Further, you can put the part number (minus the
location code) desired in cell D2.

The first potential solution is to use the SUMPRODUCT function, in this manner:

=SUMPRODUCT(--(VALUE(LEFT(A2:A49,FIND(" ",A2:A49)))=D2),B2:B49)

This formula checks the values in the range A2:A49. You should make sure that this range
reflects the range of your actual data. If you generalize the formula so that it looks at all of
columns A and B (as in A:A and B:B), you'll get a #VALUE error, since it tries to apply the
formula to empty cells in the columns.

You can get a similar result by using an array formula such as this:

=SUM(B:B*(LEFT(A2:A49,5)=TEXT(D2,"@")))

Remember, again, that this is an array formula, so you need to enter it by pressing
SHIFT+CTRL+ENTER. Note, as well, that this formula converts the value in D2 to text for the
comparison. This wasn't done in the previous formula because there the substring picked out of
column A was converted to a numeric value using the VALUE function.

You can also use the DSUM function to construct a working formula. Let's assume that the part
numbers (column A) have a column header in cell A1. Copy this column header (such as "Part
Num") to another cell in the worksheet, such as cell D1. In cell D2, enter the part number,
without its location code, followed by an asterisk. For example, you could enter "12345*"
(without the quote marks) into cell D2. With that specification set up, you can then use this
formula:

=DSUM($A$1:$B$49,$B$1,D1:D2)

This formula uses the specification in cell D2 (the characters 12345 followed by anything) as a
key to which values from column B should be summed.

Finally, if you had the same specification in cell D2 as you used with the DSUM approach, you
could use a very simple SUMIF function, in this manner:

=SUMIF(A:A,D2,B:B)

Note that this approach allows you to use the full column ranges (A:A and B:B) in the formula.

ExcelTips: Amazing Array Formulas Page 46


Using Array Formulas with Numbers

Using a Numeric Portion of a Cell in a Formula


Rita described a problem where she is provided information, in an Excel worksheet, that
combines both numbers and alphabetic characters in a cell. In particular, a cell may contain "3.5
V", which means that 3.5 hours of vacation time was taken. (The character at the end of the cell
could change, depending on the type of hours the entry represented.) Rita wondered if it was
possible to still use the data in a formula in some way.

Yes, it is possible, and there are several ways to approach the issue. The easiest way (and
cleanest) would be to simply move the alphabetic characters to their own column. Assuming that
the entries will always consist of a number, followed by a space, followed by the characters, you
can do the "splitting" this way:

1. Make sure there is a blank column to the right of the entries.


2. Select the entries.
3. If you are using Excel 2007 or a later version, display the Data tab of the ribbon and
click the Text to Columns tool in the Data Tools group. If you are using an older
version of Excel, choose Text to Columns from the Data menu. Excel displays the first
step of the Convert Text to Columns Wizard.

The first step of the Convert Text to Columns Wizard.

ExcelTips: Amazing Array Formulas Page 47


Using Array Formulas with Numbers

4. The Delimited radio button should be selected. Click Next. Excel displays the second
step of the wizard.
5. Make sure the Space check box is selected, then click Next. Excel displays the third
step of the wizard.
6. Click Finish.

Word splits the entries into two columns, with the numbers in the leftmost column and the
alphabetic characters in the right. You can then any regular math functions on the numeric values
that you desire.

If it is not feasible to separate the data into columns (perhaps your company doesn't allow such a
division, or it may cause problems with those later using the worksheet), then you can approach
the problem in a couple of other ways.

First, you could use the following formula on individual cells:

=VALUE(LEFT(A3,LEN(A3)-2))

The LEFT function is used to strip off the two rightmost characters (the space and the letter) of
whatever is in cell A3, and then the VALUE function converts the result to a number. You can
then use this result as you would any other numeric value.

If you want to simply sum the column containing your entries, you could use an array formula.
Enter the following in a cell:

=SUM(VALUE(LEFT(A3:A21,LEN(A3:A21)-2)))

Make sure you actually enter the formula by pressing SHIFT+CTRL+ENTER. Because this is an
array formula, the LEFT and VALUE functions are applied to each cell in the range A3:A21
individually, and then summed using the SUM function.

Calculating the Median Age of a Group of People


Jan has a worksheet that has a list of ages in column A. The ages start at 1 and go through to 100.
In column B she has the number of people in each of those ages. Jan needs a formula that will
tell her the median age of this group of people.

At first blush you might think that you can use the MEDIAN function to calculate the median.
That function works great if you have a simple list of values. For instance, if you were
calculating the median for the ages alone, then MEDIAN would work fine. However, in Jan's
case she needs the median value for the ages of the people, not for the ages themselves. In other
words, the median needs to be weighted by the number of people that are each age. The
MEDIAN function cannot handle such a requirement.

ExcelTips: Amazing Array Formulas Page 48


Using Array Formulas with Numbers

It should be pointed out that the median age is going to be different than the average age for a
group of people. The average can be calculated most easily by multiplying the age by the number
of people that is each age. For instance, in column C you could place a formula such as =A1*B1
and then copy it down the column. Add up the values in columns B and C, and then divide the
sum in column C by the sum in column B. The result is the average age for the list of people.

The median age, on the other hand, is the age at which half of the people fall below that age and
half above that age. The median age can best be calculated by using an array formula, such as the
following:

=MATCH(SUM($B$1:$B$100)/2,SUMIF($A$1:$A$100,
"<="&$A$1:$A$100,$B$1:$B$100))

This is a single formula, entered by pressing CTRL+SHIFT+ENTER. The SUMIF function in the
formula is used to generate an array of the cumulative number of people who are less than or
equal to each age. The SUM portion of the formula gives the midpoint of the total frequency of
ages. The MATCH function is then used to look up the midpoint value in the array of cumulative
frequencies. This yields an "index number" in the initial array, and since the array consists of the
all ages 1 through 100, this index number matches is equivalent to the median age.

An Average that Excludes Zero Values


Veronica knows how to use the AVERAGE function to determine the average of a range of
values. She would like to have the average determined based on the non-zero values in the range,
however.

If you are using Excel 2007 or a later version, the worksheet function most suited to this purpose
is to use AVERAGEIF. You can use it in this manner:

=AVERAGEIF(A1:A50,">0")

This function only includes in the average those cells that contain values greater than zero. If you
want to also exclude blank cells, you should use the AVERAGEIFS function. This function
differs from AVERAGEIF in that it allows you to specify multiple criteria that indicate which
cells to average.

=AVERAGEIFS(A1:A50,A1:A50,">0",A1:A50,"<>""")

If you are using an older version of Excel (2003 or before), then AVERAGEIF and
AVERAGEIFS are unavailable to you. In that case, then there are several ways you can proceed.
The first is to remember how an average is calculated. It is defined as the sum of a range of
values divided by the number of items in the range. Thus, you could figure the exclusionary
average by simply making sure that the denominator (the number you are dividing by) does not
include any zero values. For instance:

ExcelTips: Amazing Array Formulas Page 49


Using Array Formulas with Numbers

=SUM(A1:A50)/COUNTIF(A1:A50,"<>0")

This approach uses the COUNTIF function to determine the number of cells in the range
(A1:A50) that don't contain zero. If this range contains not only zeros but also blank cells, and
you don't want the blank cells figured into the result, then you need to use a more complex
formula:

=SUM(A1:A50)/(COUNTIF(A1:A50,"<>0")-COUNTBLANK(A1:A50)-
(COUNTA(A1:A50)-COUNT(A1:A50)))

The COUNTIF function counts cells that do not explicitly evaluate to 0, but it will count blank
and text cells. The COUNTBLANK term adjusts for the blank cells and the difference between
COUNTA and COUNT adjusts the total count for cells that contain text.

Of course you can also use an array formula to do your calculation:

=AVERAGE(IF(A1:A50<>0,A1:A50))

Remember that array formulas need to be entered by using the combination CTRL+SHIFT+ENTER.
This array formula also excludes blanks or cells containing text.

All in all it is easier to use the AVERAGEIF or AVERAGEIFS functions. When would you not
want to use them? When you need to share your workbook with users of Excel versions prior to
Excel 2007. (This is when the functions were added to Excel.)

Deriving High and Low Non-Zero Values


There may be times when you need to derive the smallest (or largest) value from a range, unless
the smallest (or largest) value is zero. For instance, you might have a range of values such as {0,
3, 1, 4, 2}. In this case, the lowest value is zero, but the value you really want returned is 1.

There is no intrinsic function within Excel to return a value as stipulated here. However, you can
create a formula that will do the trick. Assuming that the range of values you want to analyze are
in C4:C8, the following formula will return the lowest non-zero value:

=IF(MIN(C4:C8)=0,SMALL(C4:C8,COUNTIF(C4:C8,"=0")+1),MIN(C4:C8))

This formula uses the MIN function to determine if the lowest value in the range is zero. If it is,
then the SMALL function is used to derive the lowest value, excluding the zeros. (The
COUNTIF function returns the number of zeros in the range, and therefore tells SMALL which
item from the range to pick.)

A small change to the formula allows it to be used to return the largest non-zero number in a
range:

ExcelTips: Amazing Array Formulas Page 50


Using Array Formulas with Numbers

=IF(MAX(C4:C8)=0,LARGE(C4:C8,COUNTIF(C4:C8,"=0")+1),MAX(C4:C8))

These formulas will work for any range, unless the range is made up entirely of zeros. In that
instance, a #NUM! error is returned.

If you prefer to use array formulas, then you can make the formula much shorter. This version
returns the lowest non-zero value:

=MIN(IF(C4:C8=0,9^9,C4:C8))

Remember to enter it using CTRL+SHIFT+ENTER. It also suffers from a problem if all the values
in the range are 0; in that case it returns 387420489, which is 9^9. (It would also return that value
if all the values in the range were greater than 387420489.)

Median of Selected Numbers


William is trying to find the median of about 3,000 numbers in a column that has a range from
0.9 to 5. However, he only wants the median of numbers that are greater than 1. He can't figure
out how to put that parameter into his formula and is looking for ideas.

There are a couple of ways you can go about determining the median. One method is to use an
intermediate column that contains only those values above 1 and then calculate the median based
on those values. You could also use an advanced filter to extract only the values above 1 and
then use those values to find the median.

A better approach, however, is to use a simple array formula:

=MEDIAN(IF($A$1:$A$3000>1,A1:A3000))

When you enter this as an array formula (by using CTRL+SHIFT+ENTER), the result is your
median value, with your criteria taken into account.

Calculating a Geometric Standard Deviation


Jim has a set of data on which he needs to calculate some statistical information. He uses built-in
Excel functions to calculate many of these, such as the geometric mean. He cannot seem to
figure out how to calculate the geometric standard deviation, however.

The place that a geometric mean is most often used (and, therefore, a geometric standard
deviation) is when calculating investment returns over time, especially when the returns involve
compound interest. How you calculate the geometric mean is rather easy—you use the
GEOMEAN function built into Excel. How you calculate a geometric standard deviation,
however, depends on which resource you are referencing.

ExcelTips: Amazing Array Formulas Page 51


Using Array Formulas with Numbers

One reference that explains the math behind a geometric standard deviation is found on
Wikipedia:

http://en.wikipedia.org/wiki/Geometric_standard_deviation

Let's assume that you have calculated the compound annual growth rate for an investment for
four years. Over those four years the rate is expressed as 1.15 (+15%), 0.9 (-10%), 1.22 (+22%),
and 1.3 (+30%). If you place these values in cells A1:A4, then apply the simplest form of
calculating geometric standard deviation found on the Wikipedia page, you would enter the
following as an array formula:

=EXP(STDEV(LN(A1:A4)))

This provides a result of 1.1745, rounded to four decimal places. However, there is some
muddiness, as evidenced in this mathematical treatise at the Motley Fool:

http://www.fool.com/workshop/2000/workshop000309.htm

Note that it references the results of the above formula as the "standard deviation of the log
values," insisting that you need to add the average of the log values to the standard deviation and
then use the EXP function, in this manner:

=EXP(STDEV(LN(A1:A4))+AVERAGE(LN(A1:A4)))

Again, this must be entered as an array formula. It provides a result of 1.3294, which is
significantly different from what is returned using the simpler formula from Wikipedia. Which is
the actual geometric standard deviation is apparently a matter of debate and, perhaps, dependent
on a definition of terms.

Using GEOMEAN With a Large List


Ken wrote about a problem he was having with the GEOMEAN function. When he attempts to
use the function on a large number of values (3,500 rows of data), he gets a #NUM error value
returned.

The GEOMEAN function is used to return the geometric mean of a series of values. The
GEOMEAN of n numbers is the n-th root of the product of the numbers. For example, if there
are four values in a series (A through D), then the product of those numbers is A * B * C * D,
and the GEOMEAN is the fourth root of that product.

The #NUM error would be return if any of three conditions were met: any of the values was
equal to zero, any of the values was negative, or the limits of Excel were exceeded. It is likely
that it is this last condition that Ken is running into, particularly if any of his 3,500 values are
large.

ExcelTips: Amazing Array Formulas Page 52


Using Array Formulas with Numbers

Since GEOMEAN finds the product of the 3,500 numbers (multiplies them all by each other) and
then takes the nth root, the product may easily be too large for Excel. The largest positive number
in Excel is 9.99999999999999 * 10^307 (in scientific notation this is written as
9.99999999999999E+307). If the product gets larger than this number you will get a #NUM
error for the function.

The solution is to use logs to do the calculation. This is easiest to understand when you look at a
transformation of the GEOMEAN function:

GEOMEAN = (X1*X2*X3*...*Xn)^ (1/n)


ln(GEOMEAN) = ln((X1*X2*X3*...*Xn)^ (1/n))
ln(GEOMEAN) = (1/n) * ln(X1*X2*X3*...*Xn)
ln(GEOMEAN) = (1/n) * (ln(X1)+ln(X2)+ln(X3)+...+ln(Xn))
ln(GEOMEAN) = average(ln(X1)+ln(X2)+ln(X3)+...+ln(Xn))
GEOMEAN = exp(average(ln(X1)+ln(X2)+ln(X3)+...+ln(Xn)))

If you follow through the above, you see that GEOMEAN is equivalent to the exponent of the
average of the logs of the values. You can calculate the desired result by using the following
array formula instead of the GEOMEAN function:

=EXP(AVERAGE(LN(A1:A3500)))

This assumes that the desired values are in the range A1:A3500. Since it is an array formula, you
must enter it into a cell by using CTRL+SHIFT+ENTER.

Excluding Values from Averaging


You've seen it on the Olympics and in other sporting events: The average score for an athlete is
determined by throwing out the highest score and the lowest score, and then averaging the rest.
You may have a need to do similar types of averages. For instance, you may be a teacher and
need to exclude the two lowest assignment scores before calculating an average.

To perform this type of averaging, all you need to remember is that an average is calculated by
summing all the values in a range and then dividing that sum by the number of items in that
range. The SUM function easily provides the sum, and the COUNT function can be used to find
out the number of items in the range. How to exclude the two lowest values in the range? You
can use the SMALL function.

Consider the following formula, which assumes you want to find an adjusted average of the
range A10:A14:

=(SUM(A10:A14)-SMALL(A10:A14,1)-SMALL(A10:A14,2))/(COUNT(A10:A14)-2)

The SMALL function is used to determine the two lowest values in the range, and these are
subtracted from the overall sum of the range. The resulting value is then divided by the COUNT
of values in the range. Note, as well, that the COUNT value is decreased by 2 to compensate for
the fact you are ignoring the two lowest values.

ExcelTips: Amazing Array Formulas Page 53


Using Array Formulas with Numbers

Another way to calculate the same average is to use an array formula. The following one does
the trick:

=AVERAGE(IF(A10:A14>SMALL(A10:A14,2),A10:A14))

Since this is an array formula, you need to enter it by pressing CTRL+SHIFT+ENTER instead of
just pressing ENTER. This formula still relies on the use of the SMALL function, but it also uses
the actual AVERAGE function to return a result. Since this is an array formula, it examines each
of the values in the array (the range) and only considers them for use in the average if they are
larger than the second smallest value in the array.

While the array formula is shorter than the longer regular formula, there is one caveat to keep in
mind: The array formula will produce an undesired result if there is a two-way "tie" in the
second-lowest value in the range, or a three-way tie in the lowest value. For instance, if the
values being averaged are 3, 2, 10, 3, and 7, then the array formula will produce an average of
8.5. Why? Because only the values 10 and 7 are above the second-lowest value, and the average
of those two is 8.5. If you use the longer formula, first presented above, then the average
returned is 6.666667, which is the average of 10, 3, and 7.

If you try these formulas and they don't work, you should check to make sure that you have the
Analysis ToolPak installed. The SMALL function is a part of that ToolPak in versions of Excel
before Excel 2007.

Finding the Smallest Even Value


Emin has a range of cells in which there can be either text or numbers. He needs a way to
determine the smallest even number in the range. Emin wonders if this can be done with a
formula, or if he needs a user-defined function.

There are a couple of ways you can approach this problem. One method you can try is to use the
DMIN function. All that you need is to make sure that you have a header on your data column
(such as "MyData") and then create a small criteria field in some out-of-the-way place. For
instance, you might want to create the criteria field by placing a header (such as "Min Even") in
cell F1 and place the formula =ISEVEN(MyData) in cell F2. Cell F2 evaluates to an #VALUE!
error, but that is fine in this case. You can then use the following formula in a different cell:

=DMIN(A1:A100, 1, F1:F2)

If you prefer, you can use an array formula to figure out the lowest even value. Because your
data range can contain text as well as numbers, not all array formulas will work, however. For
instance, the following will generate an error if there is anything but numbers in the data range:

=MIN(IF(MOD(A1:A100,2)=0,A1:A100))

To make sure you don't get the errors, you need to do some checking in the formula:

ExcelTips: Amazing Array Formulas Page 54


Using Array Formulas with Numbers

=MIN(IF(ISNUMBER(A1:A100),IF(NOT(MOD(A1:A100,2)=0),"",A1:A100)))

Again, remember that this is an array formula, so you need to enter it using SHIFT+CTRL+ENTER.

Returning the Smallest Non-Zero Value


Mark is hoping to find the smallest non-zero value in a set of values. For example, if he has the
values 0,0,3,0,6,8, he would want the value 3 (the smallest non-zero value) returned by a
formula. Mark knows he can use the SMALL function with the second argument calculated by
using a COUNTIF to count the number of zeroes in the range. However, he wants to use this
inside of an array formula, and Excel can't handle COUNTIFs inside of array formulas.

Since Mark is only interested in array formulas (entered by pressing CTRL+SHIFT+ENTER), then
there are a couple that could be used. The following array formula is worth looking at first:

=MIN(IF(A1:A5=0,MAX(A1:A5),A1:A5))

Assuming the values to be examined are in A1:A5, this formula puts together an array of non-
zero values from that range. If the value in one of the cells is 0, then the MAX function kicks in,
returning the largest value from the range. (This essentially kicks the value at that cell—
originally 0—out of consideration as the smallest value.) If the value in one of the cells is not 0,
then the actual value is returned. The MIN function then returns the lowest value from the array.

You can make the formula even shorter by turning it around in this manner:

=MIN(IF(A1:A5<>0,A1:A5))

Note that in this version, the value in each cell of the range is checked to see if it isn't 0. If it isn't,
then the value is returned. If it is 0, then nothing is returned. Again, the MIN function is used to
return the lowest value from the array.

Last Non-Zero Value in a Row


Brian has a row of numbers with 240 cells. In this row, the numbers are steadily declining and
will eventually, at some point in those 240 cells, become 0. The zeroes will continue to fill the
remaining cells in the row. Brian needs to write an equation that will return the last non-zero
value in the row.

There are a variety of ways that the desired value can be returned. (Doesn't that always seem to
be the case with Excel? You can come up with lots of ways to get a result.) In general, you could
use a regular formula or an array formula.

If you want to use a regular formula, here's one you can try:

ExcelTips: Amazing Array Formulas Page 55


Using Array Formulas with Numbers

=OFFSET(A6,0,(COUNT(A6:IF6)-COUNTIF(A6:IF6,0))-1)

The COUNTIF function counts the number of zero values and the COUNT function determines
the number of cells in the range. Subtracting one from the other and adjusting by 1 gives the
OFFSET value into the "array" of cells where the last non-zero value lies. This formula assumes
that the values begin in column A; if they begin in a different column then you'll need to adjust
the value provided by the COUNT/COUNTIF portion of the formula to represent the offset from
the first column.

Here's a shorter variation of the formula, based on doing an offset from the right side of the range
rather than the left side:

=OFFSET(IF6,0,-COUNTIF(A6:IF6,0))

In this instance it is important that IF6 be the actual right end of the range. The formula works by
counting the number of zero values in the range (all at the right side of the range) and then
computing the cell address of the last cell (IF6) minus the number of zeros.

Here is a version that uses the INDEX function, instead:

=INDEX(A6:IF6,,MATCH(0,A6:IF6,0)-1)

This version is even shorter, using the LOOKUP function:

=LOOKUP(1,1/(6:6>0),6:6)

Array formulas can also be used. (Array formulas are entered by pressing CTRL+SHIFT+ENTER.)
This one uses the INDIRECT function:

=INDIRECT("R6C" & MAX((A6:IF6>0)*COLUMN(A6:IF6)),FALSE)

This array formula uses an interesting implementation of the LOOKUP function to find the
correct result:

=LOOKUP(9.99999999999999E+307,IF(A6:IF6<>0,A6:IF6))

Here's another array formula that can be used, this time using the OFFSET function to find the
last non-zero value in row 6:

=OFFSET(A6,0,MIN(IF(6:6=0,COLUMN(6:6),300))-2)

Here's an even shorter variation:

=MIN(IF(A6:IF6>0,A6:IF6))

ExcelTips: Amazing Array Formulas Page 56


Using Array Formulas with Numbers

All of these formulas presented so far depend on the fact that the numbers in the row actually do
decline—they go from whatever the beginning number is and steadily go toward zero. If the
numbers don't decline, then you can use a different type of array formula to determine the last
non-zero value in the row:

=INDEX(6:6,MAX(IF(A6:IF6<>0,COLUMN(A6:IF6))))

The formula first determines the maximum column in the row (in this case row 6) that has a
value not equal to zero, then it uses the INDEX function to get the value from that column in that
row.

As you can tell, there are quite a few ways to find the last non-zero value in a row. Pick the one
that strikes your fancy; there is no right or wrong in this instance.

Extracting Numbers within a Range


Robert has a series of numbers in column A that range from 1 to 100. He would like to extract
only those values between 65 and 100, inclusive, and place them in column B. He wonders if
there is a way to do that easily.

The short answer is that there is a very easy way to do it, provided you don't mind sorting the list
of numbers. Follow these steps:

1. Select a cell in column A. (It doesn't really matter which cell you select, as long as it is
one of the cells that contains a number.)
2. Display the Data tab of the ribbon.
3. Click the Sort Smallest to Largest tool, in the Sort & Filter group. Excel sorts all the
numbers in the column.
4. Select the numbers that you want to put into column B.
5. Press CTRL+X to cut the cells to the Clipboard.
6. Select cell B1 (or the first cell in column B where you want the values to appear).
7. Press CTRL+V to paste the cells into the column.

That's it; you've now got the desired cells into column B. If you simply wanted to copy the cells,
then in step 5 you could have pressed CTRL+C instead.

If you need to keep the values in column A in their original order (minus the values you want to
move), you can do it by using column B as a "place retainer" column. To the right of the first
value in column A, put the value 1. Then, below that in column B put a 2, then a 3, and so on,
until each value in column A has a corresponding value in column B that indicates the numbers
location. Then, follow these steps:

1. Select a cell in column A. (It doesn't really matter which cell you select, as long as it is
one of the cells that contains a number.)

ExcelTips: Amazing Array Formulas Page 57


Using Array Formulas with Numbers

2. Display the Data tab of the ribbon.


3. Click the Sort Smallest to Largest tool, in the Sort & Filter group. Excel sorts all the
numbers in the column.
4. Select the numbers in column A that you want to move, along with the numbers to the
right of them in column B.
5. Press CTRL+X to cut the cells to the Clipboard.
6. Select cell D1. (It is important to select cell D1 because you need to leave column C
blank.)
7. Press CTRL+V to paste the cells into columns D and E.
8. Select a cell in column B.
9. Display the Data tab of the ribbon.
10. Click the Sort Smallest to Largest tool, in the Sort & Filter group. Excel sorts all the
numbers in the column based on the values in column B.
11. Select a cell in column E.
12. The Data tab of the ribbon should still be displayed.
13. Click the Sort Smallest to Largest tool, in the Sort & Filter group. Excel sorts all the
numbers in the column based on the values in column E.
14. Delete columns B, C, and E.

At this point the values in columns A and B reflect their original order, from when they were all
in column A.

Another way to move the cells is to use the filtering capabilities of Excel. Follow these steps:

1. Select a cell in column A. (It doesn't really matter which cell you select, as long as it is
one of the cells that contains a number.)
2. Display the Data tab of the ribbon.
3. Click the Filter tool. Excel adds a filter drop-down arrow at the right side of the column
A header.
4. Click the down-arrow and choose Number Filters | Between. Excel displays the Custom
AutoFilter dialog box.

ExcelTips: Amazing Array Formulas Page 58


Using Array Formulas with Numbers

The Custom AutoFilter dialog box.

5. In the Greater Than or Equal To box, enter 65.


6. In the Less Than or Equal To box, enter 100.
7. Click OK. Excel limits what is shown to only those rows that meet the criteria you
specified in steps 4 through 6.
8. Select the cells that are displayed.
9. Press CTRL+C to copy the cells to the Clipboard.
10. Select cell B1 (or the first cell in column B where you want the values to appear).
11. Press CTRL+V to paste the cells into the column.
12. Select one of the cells in column A.
13. Again click the Filter tool. (The Data tab of the ribbon should still be displayed.) Excel
removes the filter you previously applied.

You could also use formulas in column B to pull out the values that are within the desired range.
An easy way to do this is to place this formula in cell B1:

=IF(AND(A1>=65, A1<=100),A1,"")

Copy the formula down as far as necessary in column B and you end up with any values in the
range of 65 to 100, inclusive, being "copied" into column B. If the value is outside this range,
then the cell in column B is left empty.

Assuming that you don't want any empty cells in column B, then you could use an array formula
to grab the values. If your values are in the range A1:A500, place the following in cell B1:

=IFERROR(INDEX(A$1:A$500,SMALL(IF(A$1:A$500>=65,ROW($1:$500)),ROW())),"")

Enter it using CTRL+SHIFT+ENTER, and then copy the formula down as far as you'd like.

ExcelTips: Amazing Array Formulas Page 59


Using Array Formulas with Numbers

Picking Different Random Numbers from a Range


Let's say that you have a list of employees (or products, or widgets, or whatever) and that you
want to pick two items from this list, at random. There are a couple of different worksheet
functions you can use to pick items from the list, such as RANDOM and RANDBETWEEN.

Many people use the RANDBETWEEN function because it is very easy and provides a random
number within a range. Thus, if you have 25 items in your list, RANDBETWEEN can return a
number between 1 and 25, which can then correspond to items in the list.

For example, let's say that your list of items is in the range A2:A26, and that you give this range
the name Items. You could then put the following formula in cell C2 to return a number between
1 and the number of Items:

=RANDBETWEEN(1,ROWS(Items))

Copy this formula to cell C3, and you now have two random numbers that represent items from
the list. In cell D2 and D3 you could put formulas like this to get the actual names from the list:

=INDEX(Items,C2)

The only problem with this approach is that it is possible for both instances of
RANDBETWEEN (cells C2 and C3) to return the same value, and thus you end up with the
same item selected twice from your list.

One way to work around this potential problem is to actually select three items from the list
instead of two. If the first two items are the same, then the third can be used as a "fallback" item
to provide the unique second. The method is not foolproof, as it is possible—but quite
improbable—that all three will be the same.

A different approach to selecting items from the list would be to assign each item its own
random value, and then select based on the highest number in the series. The RANDOM function
returns a random value between 0 and 1. In each cell of column B, just to the right of each item
in column A, put this formula:

=RAND()

Select the range of cells (B2:B26) and name the range, using a name such as ItemNums. You can
then determine the first random name from the list by using the following array formula:

=OFFSET(A$1,SUM((LARGE(ItemNums,1)=(ItemNums))*ROW(ItemNums))-1,0)

In order to signify that this is an array formula, enter it by pressing SHIFT+CTRL+ENTER. The
formula should return a single name. You can then use the following array formula to return the
second name:

ExcelTips: Amazing Array Formulas Page 60


Using Array Formulas with Numbers

=OFFSET(A$1,SUM((LARGE(ItemNums,2)=(ItemNums))*ROW(ItemNums))-1,0)

The reason that using the RAND function approach works better than using RANDBETWEEN
is because the chance that RAND will return two identical values is infinitesimally small,
whereas the chances of RANDBETWEEN doing so is much higher.

ExcelTips: Amazing Array Formulas Page 61


Using Array Formulas with Text

Using Array Formulas with Text

Comparing Lists for Duplicates


Imagine for a moment that you have a worksheet that contains lists of part numbers. On one
worksheet you have a list of part numbers, and on another worksheet you have a similar list. The
lists are not identical, however, and you want to determine if a particular part number on one list
also appears on the other.

One solution is to somehow combine the lists, but add some sort of indicator as to which original
list the particular part number came from. This approach (or a variation thereon) is, in fact, the
approach taken by many Excel users.

What if you don't want to combine the lists, however? In this case, there is a very easy way to do
the comparison. Follow these steps:

1. Make sure there is a blank column just to the right of each list of part numbers on each
worksheet.
2. Select the part numbers on the first worksheet and give them a name such as
"PartList1". (In Excel 2007 or later versions display the Formulas tab of the ribbon and
then click on Define Name in the Defined Names group. In older versions of Excel use
Insert | Name | Define.)
3. Select the part numbers on the second worksheet and give them a name such as
"PartList2".
4. Assuming that the first part number on the first worksheet is in cell A2, enter the
following formula in cell B2:

=ISNUMBER(MATCH(A2,PartList2,0))

5. Copy the formula down so that a copy appears to the right of each part number on the
first worksheet.
6. Repeat steps 4 and 5 on the second worksheet, but use the following formula:

=ISNUMBER(MATCH(A2,PartList1,0))

When you are done, either TRUE or FALSE will appear to the right of each part number on each
worksheet. If TRUE appears, the associated part number appears on the other worksheet. If
FALSE appears, then the part number is unique and does not appear on the other worksheet.

ExcelTips: Amazing Array Formulas Page 62


Using Array Formulas with Text

Another approach is to use an array formula to do the comparisons. You could follow the same
steps shown above, but use the following formula in step 4 (and PartList1 variation in step 6):

=OR(EXACT(A2,PartList2))

Since this is an array formula, you would enter it by using CTRL+SHIFT+ENTER. The result is the
same TRUE and FALSE designation described above.

Regardless of which formula approach you use, you can use the AutoFilter capabilities of Excel
to limit what is shown on either worksheet. If you filter to show only the FALSEs, you will have
a list of all unique part numbers. If you filter to show TRUEs, then you will have a list of
duplicates.

Finding the First Non-Digit in a Text Value


Tony has a bunch of data in a worksheet that consists of digits and other characters. For instance,
he might have a cell that contains "1234567Blue." Tony wants to be able to figure out the
character position at which the first non-digit character occurs. In the example of the text
"1234567Blue" Tony wants some way to figure out that the first non-digit character is at position
8.

A great way to get the value you want is to use an array formula to calculate the position. The
following array formula (entered by using CTRL+SHIFT+ENTER) will work in the majority of
cases:

=MATCH(TRUE,ISERROR(VALUE(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1))),0)

The only instances where this array formula won't work is if cell A1 is either empty or contains a
strictly numeric value. If your list may contain this type of data (or no data at all), then you
should consider using a slightly longer array formula:

=IF(LEN(A1)=0,0,MIN(IF(1*ISNUMBER(1*MID(A1,ROW(INDIRECT("A1:A"&
LEN(A1))),1))=0,ROW(INDIRECT("A1:A"&LEN(A1))),LEN(A1)+1)))*
(ISNUMBER(A1)=FALSE)

Remember that that is a single array formula, entered by using CTRL+SHIFT+ENTER. It will
properly handle instances where A1 contains no non-digit characters (as in a blank cell or a value
such as "123").

Another possible array formula that should return the desired position is the following. This one
should handle empty cells and strictly numeric values just fine, but it is shorter than the
previously presented array formula:

=IFERROR(MATCH(1,ISERR(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)*1)*1,),)

ExcelTips: Amazing Array Formulas Page 63


Using Array Formulas with Text

Inserting Dashes between Letters and Numbers


Let's say you have a worksheet with lots of product codes in column A. These codes are in the
format A4, B12, AD4, etc. Due to a change in the way your company operates, you are directed
to change all the product codes so they contain a dash between the letters and the numbers.

There are several ways you can perform this task. If the structure of your product codes is
consistent, then inserting the dashes is a snap. For instance, if there will always be a single letter
followed by numbers, then you could use a formula such as this:

=LEFT(A1,1) & "-" & RIGHT(A1,LEN(A1)-1)

Chances are good that your data won't be structured, meaning you could have one or two letters
followed by up to three digits. Thus, both A4 and QD284 would both be valid product codes. In
this case, a solution formula takes a bit more creativity.

One way to handle it is with an array formula. Consider the following formula:

=REPLACE(A1,MATCH(FALSE,ISERROR(1*MID(A1,ROW(INDIRECT("1:100")),1)),0),0,"-")

If values are in A1-A10, you can put this formula into B1, and then copy it down the column.
Since it is an array formula, it must be entered by pressing CTRL+SHIFT+ENTER. The formula
finds the location of the first number in the cell and inserts a dash before it.

Assume, for the sake of example, that cell A1 contains BR27. The innermost part of the formula,
INDIRECT("1:100"), converts the text 1:100 to a range. This is used so that inserting or deleting
rows does not affect the formula. The next part of the formula, ROW(INDIRECT("1:100")),
essentially creates an array of the values 1-100: 1,2,3,...,99,100. This is used to act on each
character in the cell.

The next part, MID(A1,ROW(INDIRECT("1:100")),1), refers to each individual character in the


string. This results in the array: "B", "R", "2", and "7". Multiplying the array by 1 (the next part
of the formula) results in each of the individual characters being converted to a number. If the
character is not a number, this conversion yields an error. In the case of the string being
converted (BR27), this results in: #VALUE, #VALUE, 2, and 7.

The next step is to apply the ISERROR function to the results of the multiplication. This
converts the errors to TRUE and the non-errors to FALSE, yielding TRUE, TRUE, FALSE, and
FALSE. The MATCH function looks in the array of TRUE and FALSE values for an exact
match of FALSE. In this example, the MATCH function returns the number 3, since the first
FALSE value is in the third position of the array. At this point, we essentially know the location
of the first number in the cell.

The final function is REPLACE, which is used to actually insert the dash into the source string,
beginning at the third character.

ExcelTips: Amazing Array Formulas Page 64


Using Array Formulas with Text

Getting Rid of Everything Except Numbers


Linda has a column that contains alpha and numeric characters. She needs to retain the numeric
characters and delete the alpha ones. For example, a cell may contain 10003E111 and she wants
to end up with 10003111.

There are a few ways you can approach this problem. Before proceeding with any solution,
however, you should make sure that you aren't trying to change something that isn't really
broken. For instance, you'll want to make sure that the "E" that appears in the number isn't part of
the format of the number—in other words, a designation of exponentiation. If it is, then you don't
really want to remove the character because it will end up changing the nature of the underlying
number.

If you determine that the characters aren't part of the number's format, then you can first try
using formulas to remove the alpha characters. If the values you want to change are in column A,
you could enter the following (very long) formula in column B:

=MID(A1,MATCH(TRUE,ISERROR(1*MID(A1,ROW(INDIRECT
("1:"&LEN(A1))),1)),0),-MATCH(TRUE,ISERROR(1*MID
(A1,ABS(ROW(INDIRECT("1:"&LEN(A1)))-LEN(A1)-1),1))
,0)+LEN(A1)+2-MATCH(TRUE,ISERROR(1*MID(A1,ROW
(INDIRECT("1:"&LEN(A1))),1)),0))

Make sure you enter this as an array formula by pressing CTRL+SHIFT+ENTER. Then enter the
following into column C:

=SUBSTITUTE(A1,B1,"")

The result is that column C contains the values from column A, without the alpha characters.
You could use Paste Special to copy the information from column C to another column so that
you end up with actual values instead of formula results.

Breaking Up Variable-Length Part Numbers


Marty has a worksheet with a long series of part numbers in column A. These consist of letters
and numbers, such as A123BC, AB123C, etc. Marty wants to break the data into three columns,
so that text before the numbers will be in one column, numbers in the second column, and text
after the numbers in the third.

The factor that complicates dividing the part number into segments is that there is no set length
for each component of the combined part number. If the components were of standard lengths,
then you could use the Text to Columns function in Excel. Since they aren't, and there is no
delimiter between the components, then that potential avenue for solution is closed.

If you want to use formulas to pull apart the part numbers, then you will need three of them, one
for each component you want to extract. Assuming that the part numbers follow the pattern

ExcelTips: Amazing Array Formulas Page 65


Using Array Formulas with Text

indicated (text, digits, text) and that the first part number is in cell A1, you could use the
following in cell B1:

=LEFT(A1,MATCH(FALSE,ISERROR(1*MID(A1,ROW(INDIRECT("1:100")),1)),0)-1)

This needs to be entered as an array formula, meaning that you need to enter it using
CTRL+SHIFT+ENTER. The formula finds the first numeric digit in the part number, and then
returns everything before that digit. It will work on any part number that isn't over 100 characters
in length.

To extract the second component of the part number, you can put the following formula in cell
C1:

=MID(A1,MATCH(FALSE,ISERROR(1*MID(A1,ROW(INDIRECT("1:100")),
1)),0),COUNT(1*MID(A1,ROW(INDIRECT("1:100")),1)))

Again, this is a single formula, and it needs to be entered as an array formula


(CTRL+SHIFT+ENTER) so that it can work on each character in the original part number. It
examines the part number and determines the beginning point of the digits, and then extracts all
those digits. It returns a text string, even though that string is composed of digits. If you want it
to actually be treated as a number (which would get rid of any leading zeros, of course), then you
need to enclose the entire formula in a Value function, as shown here:

=VALUE(MID(A1,MATCH(FALSE,ISERROR(1*MID(A1,ROW(INDIRECT("1:100")),
1)),0),COUNT(1*MID(A1,ROW(INDIRECT("1:100")),1))))

To get the last component of the part number, you need to use the following formula, again
entered as an array formula:

=MID(A1,MATCH(FALSE,ISERROR(1*MID(A1,ROW(INDIRECT("1:100")),
1)),0)+COUNT(1*MID(A1,ROW(INDIRECT("1:100")),1)),100)

While this approach works very well, array formulas are notoriously calculation intensive,
especially when you have a lot of the formulas in your worksheet. If you need to pull apart a
thousand part numbers, that means that you end up with 3,000 array formulas, which can be
very, very slow in recalculating.

Returning Item Codes Instead of Item Names


Alan can use data validation to create a drop-down list of valid choices for a cell. However, what
he actually needs is more complex. He has a large number of item names with associated item
codes. In cell B2 he can create a data validation list that shows all the item names (agitator,
motor, pump, tank, etc.). The user can then choose one of these. When he references cell B2
elsewhere, however, he wants the item code—not the item name—returned by the reference.
Thus, the reference would return A, M, P, TK, etc. instead of agitator, motor, pump, tank, etc.

ExcelTips: Amazing Array Formulas Page 66


Using Array Formulas with Text

There is no direct way to do this in Excel. The reason is because data validation lists are set up to
include only a single-dimensional list of items. This makes it easy for the list to contain your
item names. However, you can expand how you use the data validation list a bit to get what you
want. Follow these steps:

1. Someplace to the right of your worksheet data, set up a data table. This table will
contain your item names and, to the right of each item name, the item code associated
with that name.
2. Select the cells that contain your item names. (Don't select the item codes, just the
names.)
3. Display the New Name dialog box (Excel 2007 and later) or the Define Name dialog
box (Excel 2003 and earlier). (Display the Formulas tab of the ribbon and then click the
Define Name tool in the Defined Names group. If you are using a version of Excel
before Excel 2007, choose Insert | Name | Define.)

The New Name dialog box.

4. Replace whatever is in the Name box with a descriptive name, such as ItemNames.
5. Click OK to add the name and close the dialog box.
6. Select cell B2 (the cell where you want your validation list).
7. Display the Data Validation dialog box. (Display the Data tab of the ribbon and then
click the Data Validation tool in the Data Tools group. If you are using a version of
Excel before Excel 2007, choose Validation from the Data menu.)

ExcelTips: Amazing Array Formulas Page 67


Using Array Formulas with Text

The Data Validation dialog box.

8. Using the Allow drop-down list, choose List.


9. In the Source box, enter an equal sign followed by the name you defined in step 4 (such
as =ItemNames).
10. Click OK.

With these steps done, people can still use the data validation drop-down list to select valid item
names. What you now need to do is reference the item code from the data table you set up in step
1. You can do that with a formula such as this:

=VLOOKUP(B2,OFFSET(Itemlist,0,0,,2),2,FALSE)

This formula can be used on its own (to put the desired item code into a cell) or it could be used
within a larger formula, anyplace you would have originally referenced B2.

If, for some reason, you cannot create a data table for your item names and codes, you could
approach the problem by creating an array formula:

=INDEX({"A","M","P","TK"},MATCH(B2,{"agitator","motor","pump","tank"},0))

As with all array formulas, you enter this one by pressing CTRL+SHIFT+ENTER. The biggest
drawback to it is that it can quickly become unwieldy to keep the formula updated and there is a
"viability limit" on how many pairs of codes and items you can include in the formula. (The limit
is defined by formula length, so it depends on the length of your item names.) Also, this
approach is good to only return the item code in another cell, rather than including it as part of a
larger formula.

ExcelTips: Amazing Array Formulas Page 68


Using Array Formulas with Text

Compiling a List of Students in a Course


Keval has a table of over 5,000 students distributed across 22 courses of study. Against each
student's name in each row of this table is indicated the course in which the student is registered.
Keval would like, on a different worksheet, to put a course name in cell A1 and then have Excel
display, beginning in row 3, all the students in that course, as pulled from the list. He wonders if
there is a way to do this with a formula.

The best solution to this problem is going to be dependent, in large part, on how your source data
is organized. If you only have two columns (student name and course name), then it is possible
that you don't even need to use a second worksheet. Instead, you can get by using Excel's
filtering capabilities. Just filter on the contents of the column that contains the course name, and
you can easily limit what is displayed to only those students in the course.

If you must have the information appear on the second worksheet, it may be best to use an array
formula to extract the students. Put the desired course in cell A1 and then the following formula
in cell A3:

=IF(COUNTIF(Sheet1!$B$1:$B$5000,$A$1)<ROW()-2,"",
INDEX(Sheet1!$A$1:$A$5000,SMALL(IF(Sheet1!$B$1:$B$5000=$A$1,
ROW(Sheet1!$B$1:$B$5000)),ROW()-2)))

Remember—this is a single formula and you need to enter it in the cell by pressing
CTRL+SHIFT+ENTER. Copy the formula downward into enough cells that it should accommodate
your largest class. The formula also assumes that the source data in in rows 1 through 5000; if
this is not the case, you'll want to modify the formula to reflect the appropriate range.

Another approach is to create a PivotTable based upon the student/course list. All you need to do
is make sure that both the course name and the student name fields are in the "row" area of the
PivotTable. Put the course name field first and the student name field second, and you'll end up
with a list of all courses with the students under each course name.

If your source data has additional information associated with it (such as grades, addresses, book
assignments, etc.), then you may want to consider working with an actual database program,
such as Access. You'll be able to manipulate and extract data using tools that are superior to
those in Excel.

Splitting Cells by Case


Manik has a worksheet that, in column A, has text values in the format "mikeDAVIS", where the
person's first name is in lowercase and the last name is in uppercase. He would like to split the
names to two separate columns, according to the case of the text.

The key to accomplishing this is to figure out where the text switches from lower- to uppercase.
This can only be done by examining each character in the string. So, if you want to use a

ExcelTips: Amazing Array Formulas Page 69


Using Array Formulas with Text

formulaic approach, then you'll need to use an array formula. The following array formula
returns the last name of whatever is in cell A1:

=MID(A1,MATCH(1,(CODE(MID(A1,ROW($1:$255),1))>=65)
*(CODE(MID(A1,ROW($2:$255),1))<90),)+1,255)

Remember, since this is an array formula, you should enter it by pressing CTRL+SHIFT+ENTER. It
returns everything in the cell starting with the first uppercase letter it finds. Thus, in
"mikeDAVIS" it would return "DAVIS" and in "mikeDavis" it would return "Davis". Assuming
that you use the array formula in cell B1, you could then determine the first name by using the
following:

=SUBSTITUTE(A1,B1,"")

This is a regular formula, not an array formula.

There are many similar array formulas that can accomplish much the same task. For example,
this array formula will return the first name (all the characters up to the first uppercase character)
of whatever is in cell A1:

=LEFT(A1,MAX((CODE(MID(A$1,ROW(INDIRECT("1:"&
LEN(A1))),1))>96)*ROW(INDIRECT("1:"&LEN(A1)))))

You can then use the same regular formula (the one that uses the SUBSTITUTE function) to
derive the last name.

Checking for Messages in Cells


Blair has a worksheet divided into two areas: data entry and data verification. The data
verification area consists of formulas that check entries using IF statements. If a problem is
located, a text message is displayed in a cell in the verification area, otherwise the formula
returns a blank. The following is a typical verification formula:

=IF(A1<>5,"Value in A1 is not 5")

The problem is that the data verification area can be quite large, which means it is easy to miss
one of the text messages. Blair wondered if there was a way to create a formula that examined
the data verification area and returned a single message if there were any other messages in the
area.

There are a number of different ways that this problem can be approached. If the data
verification area is contiguous, then a simple array formula will do the trick. Enter the following
in any empty cell on the worksheet:

=SUM((LEN(ValRange)>0)*1)

ExcelTips: Amazing Array Formulas Page 70


Using Array Formulas with Text

Make sure that ValRange is replaced with the range of cells in the data validation area. Also,
make sure you enter the formula by pressing CTRL+SHIFT+ENTER (to denote it is an array
formula). The formula returns a value that indicates how many cells in the range have a length
that is greater than 0. In other words, it counts the number of cells that have messages visible.

If you prefer to not use an array formula, you can accomplish the same result by using the
following regular formula:

=SUMPRODUCT((LEN(ValRange)>0)*1)

The result, again, is the number of cells that have a length greater than 0. Another approach is to
use some of the COUNT functions provided by Excel:

=COUNTA(ValRange) - COUNTBLANK(ValRange)

This formula counts the number of cells in the range, and then subtracts the number of blank
cells in the range. The result is the number of cells that are non-blank, or those that are
displaying messages. A different formulaic approach can be used to determine a simple yes/no
response:

=IF(COUNTIF(ValRange,"?*"),"","No ") & "Verification Messages"

If there are no messages in the ValRange, the formula returns "No Verification Messages." If
there are messages, it strips the "No" and simply returns "Verification Messages."

It would also be a good idea to apply conditional formatting to your data verification area. While
the formulas discussed so far will tell you if there are messages, it won't highlight where those
messages are—conditional formatting can pinpoint each message. Select all the cells in the area
that contain formulas, and then use conditional formatting to check the length of those cells. If
the length is greater than 0, the cell could be formatted to show a red background. This will make
any messages in the data verification area much harder to miss when scrolling through the
worksheet.

If you are in the mood to completely redesign your worksheet, a more powerful approach would
be to do away with the data verification area. You can achieve the same results (checking what is
in the data entry area) by using data validation for each of the entry cells. Set up properly, data
validation would make sure that the user entered acceptable values into each cell, removing the
need for much of the data validation area.

Deleting Duplicate Columns


Dror has a worksheet that contains quite a bit of data. It is possible that the data in one column
will be exactly the same as the data in another column, so he wonders if there is an easy way to
delete any duplicate columns within the worksheet.

ExcelTips: Amazing Array Formulas Page 71


Using Array Formulas with Text

The first step, of course, is to figure out if two columns are identical or not. This can be
determined rather easily with an array formula such as the following:

=AND(A1:A100=B1:B100)

(Remember that an array formula is entered by using CTRL+SHIFT+ENTER.) The formula


compares all the values in the first 100 rows of columns A and B. If they are all the same, then
the formula returns TRUE. If any of the cells don't match, then the formula returns FALSE. If
the result is TRUE you could then delete one of the columns because they are the same.

Checking for Either of Two Text Values


Chris wants to count cells that contain text value A or text value B, anywhere in the cell's text. If
the cell contains both A and B, she wants to count it, but only once. For instance, Chris has three
cells containing "apple seed", "apple tree", and "peach seed" and she wants to know the number
of cells containing either "apple" or "seed". (The proper answer that should be returned is 3.)

There are many ways that this can be approached. In considering solutions, I examined only
those solutions that avoid intermediate answers, which occupy additional columns. The first
solution involves using the COUNTIF function in this manner:

=COUNTIF(A1:A9,"*apple*")+COUNTIF(A1:A9,"*seed*")
-COUNTIF(A1:A9,"*seed*apple*")-COUNTIF(A1:A9,"*apple*seed*")

The formula counts all the cells that contain either "apple" or "seed" and then subtracts all the
cells that contain "seed" followed by "apple" (both words are in the cell) or "apple" followed by
"seed" (the same words in reverse order).

Another solution, this one a bit shorter, relies on the COUNTA and FIND functions, as shown
here:

=COUNTA(A1:A9)-SUMPRODUCT(--(ISERROR(FIND("apple",A1:A9)))
*--ISERROR(FIND("seed",A1:A9)))

The formula counts the cells containing values and then subtracts all those cells that don't contain
either "apple" or "seed".

You can also, if you prefer, use one of Excel's database functions. Provided you have a column
heading for your original phrases, this is not that difficult to do and it results in the shortest
formula. All you need to do is set up a corresponding criteria table. For instance, let's say your
data is in A1:A9, and the first cell in the column contains a header such as "My Phrases". In
another column you should put the same header and then, in the two cells directly under it, place
these two formulas:

*apple*
*seed*

ExcelTips: Amazing Array Formulas Page 72


Using Array Formulas with Text

The criteria specify that you want to match any cells that contain "apple" or "seed" within the
cell. With this set up (I'm assuming you placed the criteria table in D1:D3), you can use the
following formula:

=DCOUNTA(A1:A9,1,D1:D3)

Of course, you could also use an array formula (entered by pressing CTRL+SHIFT+ENTER) to get
your answer. The following is one such formula that relies, again, on the phrases being checked
to be in A1:A9:

=SUM(--((ISNUMBER(FIND("apple",A1:A9))+ISNUMBER(FIND("seed",A1:A9)))>0))

Extracting Street Numbers from an Address


Allan has a list of several hundred names and addresses. The street addresses range from Main
Street, 123 Main Street, US RT 2, or 187 South Elm St. He would like to break out the street
number from the addresses. So the address 123 Main Street would end up with "123" in one cell
and "Main Street" in another. If there is no street number, then nothing ends up in the street
number column. The Text to Columns tool will not work and he wonders how he can do this.

In a perfect world, Excel would allow you to easily split the numbers from the street names.
Since this option doesn't exist, you have a couple of choices. The most time-consuming option
involves adding an additional column and retyping the data. If, however, you would like to save
some time, you can use a variety of formulas to accomplish the task.

Assuming the list of addresses is in column A (beginning in cell A1), you could use a formula
similar to the following to pull out the numeric portion of the address:

=IF(ISERROR(VALUE(LEFT(A1,1))),"",LEFT(A1,FIND(" ",A1)-1))

Assuming you put the formula in cell B1, you could then use a different formula to derive the
non-numeric portion of the address:

=TRIM(RIGHT(A1,LEN(A1)-LEN(B1)))

Note that this approach does have a limitation. Some addresses, especially in major metropolitan
areas, use a format such as 152-33 Bell Blvd. The formulas above will work for these addresses,
but if the alternative, 152 33 Bell Blvd., is used, the formula will parse incorrectly. Unless you
want to buy a professionally developed address-parsing program, the formulas above and a quick
eyeball scan of the results should be adequate.

Another formula works in this case. Assuming your address is in cell A2, enter the following
formula into cell B2:

=IF(ISNUMBER(VALUE(LEFT(A2,1))),VALUE(LEFT(A2,FIND(" ",A2)-1)),"")

ExcelTips: Amazing Array Formulas Page 73


Using Array Formulas with Text

This formula is saying, "If the first character is not a number, leave the cell blank. Otherwise,
give me all of the characters on the left out to, but not including, the first space." You can then
use the result of this formula to pull out the non-numeric portion of the address:

=IF(B2="",A2,MID(A2,FIND(" ",A2)+1,99))

Another approach is to use an array formula. Here again, assuming your address is in cell A2,
you can use the following:

=IF(ISNUMBER(1*MID(A2,ROW($1:$1),1)) = TRUE,
LEFT(A2,FIND(" ",A2,1)),"")

Since this is an array formula you need to enter it by using CTRL+SHIFT+ENTER. The result is
that the formula returns the leading numeric portion of the address. You can then determine the
non-numeric portion by using the following array formula:

=IF(ISNUMBER(1*MID(A2,ROW($1:$1),1)) = TRUE,
RIGHT(A2,LEN(A2)-FIND(" ",A2,1)),A2)

ExcelTips: Amazing Array Formulas Page 74


Using Array Formulas with Dates

Using Array Formulas with Dates

Weekdays in a Month
Ever wonder how many of a particular weekday occur within a given month? For some people, it
is important to know, for example, how many Tuesdays there are in a month. And who doesn't
want to know whether a particular month will have four or five Saturdays?

Excel does not include an intrinsic function that you can use to determine the number of times a
particular weekday occurs within a given month. You can, however, create your own formulas
and functions to accomplish the task.

First, consider the following formula.

=4+N((WEEKDAY(DATE(YEAR($A$1),MONTH($A$1),1)))+
(DAY(DATE(YEAR($A$1),MONTH($A$1)+1,0))-28)>(7*((
WEEKDAY(DATE(YEAR($A$1),MONTH($A$1),1)))>(1+ROW()-
ROW($A$2)))+(1+ROW()-ROW($A$2))))

The formula relies on a date in A1. This date should be from the month you want "tested." The
formula is meant to be copied into a cell in row 2, and then copied to the six cells directly
beneath that. For instance, you could copy this formula to the range of cells B2:B8. The first
response (B2) is the number of Sundays in the month, the second (B3) is the number of
Mondays, and so on.

The drawback to this formula is that it uses the position of the cell containing the formula as part
of the formula. This means that the formula must be placed somewhere beginning in the second
row.

Another drawback is that the formula is quite long and complex. If you want a shorter formula,
then you need to turn to an array formula. One handy formula you can use assumes that you
provide three arguments: the year (cell C2), the month (cell D2), and a weekday (cell E2). With
these three items, the following formula works great:

=SUM(IF(WEEKDAY(DATE(C2, D2, ROW(INDIRECT("1:" &


DAY(DATE(C2, D2+1, 0))))))=E2, 1, 0))

Remember that this is an array formula, which means that you must enter it by pressing
SHIFT+CTRL+ENTER. In addition, the weekday value you enter in cell E2 must be in the range of
1 through 7, where 1 is Sunday, 2 is Monday, etc.

ExcelTips: Amazing Array Formulas Page 75


Using Array Formulas with Dates

Finding the Date Associated with a Negative Value


Stuart has a series of readings in a worksheet. In the first column he has dates associated with the
readings and in the second column he has the actual readings. Stuart would like to have a
formula that will return the first date at which a reading became negative. In other words, the
formula should look for the first value that is negative in the second column and then return the
date associated with that value. There can be multiple negative values in the second column, but
he needs only the date associated with the first negative value.

There are a number of ways that this problem can be approached. All of the methods presume
that the dates in column A are in ascending order and that the readings in column B are not in
any type of discernable order. (In other words, the readings could bounce above and below 0 on
any given date.)

Provided that you have some control over the layout of the worksheet, you could add an
intermediate work column in column C, used to indicate when a value is negative. Simply place
a formula like this in column C, to the right of each reading:

=IF(B1<0,A1,"")

This formula returns the date in column A if the value in B is below 0 (negative), otherwise it
returns nothing. All you then need to do is look for the minimum value in column C:

=MIN(C:C)

Format the result as a date, and it represents the date at which the readings first became negative.

Another approach is to forego the use of the intermediate column and use an array formula to
determine the date. Assuming the data is in the range A1:B42, you can use any of the following
formulas:

=MIN(IF(B1:B42<0,A1:A42,""))
=OFFSET($A$1,MATCH(TRUE,$B$1:$B$42<0,0)-1,,,)
=INDEX(A:A,MIN(IF(B1:B42<0,ROW(B1:B42))))
=INDEX(A1:A42,MATCH(TRUE,B1:B42<0,0))
=INDIRECT("A"&MIN(IF(B1:B42<0,ROW(B1:B42))),TRUE)

Remember that these are all array formulas, so you need to enter whichever one you choose by
pressing CTRL+SHIFT+ENTER. Format the result as a date, and it is the answer you seek.

Determining a Name for a Week Number


Theo uses an Excel worksheet to keep track of reservations in his company. The data consists of
only three columns. The first is a person's name, the second the first week number (1-52) of the
reservation, and the third the last week number of the reservation. People can be reserved for
multiple weeks (i.e., start week is 15 and end week is 19). Theo needs a way to enter a week

ExcelTips: Amazing Array Formulas Page 76


Using Array Formulas with Dates

number and then have a formula determine what name (column A) is associated with that week
number. The data is not sorted in any particular order, and the company won't let Theo use a
macro to get the result (it has to be a formula).

Theo's situation sounds simple enough, but it is filled with pitfalls when devising a solution.
Looking at the potential data (as shown in the following figure) quickly illustrates why this is the
case.

Potential data for Theo's problem.

Notice that the data (as Theo said) is not in any particular order. Note, as well, that there are
some weeks where there are no reservations (such as week 5 or 6), weeks where there are
multiple people (such as week 11 or 16), and weeks where there is someone reserved, but the
week number doesn't show up in column B or C (such as week 12 or 17).

Before starting to look at potential solutions, let's assume that the week you want to know about
is cell E1. You should name this range as Query. Further, name the range that contains people's
names (in this example, cells A2:A10) as ResNames, the starting weeks (B2:B10) as StartWeeks,
and the ending weeks (C2:C10) as EndWeeks. Finally, define a name for the entire table
(A2:C10), such as MyData. This naming, while not strictly necessary, will make understanding
the formulas much easier.

One potential solution is to add what is commonly referred to as a "helper column." Add the
following to cell D2:

=IF(AND(Query>=B2,Query<=C2),"RESERVED","")

Copy the formula down, for as many cells as there are names in the table. (For example, copy it
down through cell D10.) When you place a week number in cell E1, then the word
"RESERVED" appears to the right of any reservation that involves that week number. It is also
easy to see if there are multiple people reserved for that week or if there are no people reserved
for that week. You could even apply an AutoFilter and select to only show those records with the
word "RESERVED" in column D.

ExcelTips: Amazing Array Formulas Page 77


Using Array Formulas with Dates

You can, if desired, forego the helper column and consider using conditional formatting to
display who is reserved for a desired week. Simply select the names in column A and add a
conditional formatting rule that uses the following formula:

=AND(Query>=B2,Query<=C2)

(How you enter conditional formatting rules has been described extensively in other issues of
ExcelTips.) Set the rule so that it changes the shading (pattern) applied to the cell, and you'll
easily be able to see which reservations apply to the week you are interested in.

Another approach is to use an array formula. Select a few more cells than the number of
overlapping reservations you expect, and then enter the following into those cells by pressing
CTRL+SHIFT+ENTER:

=IFERROR(INDEX(ResNames,LARGE((StartWeeks<=Query)*(EndWeeks>=Query)*(ROW(ResNames
)),ROW()-1)-1),"")

When picking the number of cells you want this array formula to occupy, look at, for instance,
the number of people that may be reserved over week 11. In the example shown in this tip, it is 2
people. Select more than that number of cells and then put the array formula in those cells. If you
expect that you might have 20 people potentially booked for the same week, then you'll want to
pick a larger number of cells, such as 20 or 30. Just select the cells, put the formula in the
Formula bar, and then press CTRL+SHIFT+ENTER.

Finally, you really should consider revising how your data is laid out. You could create a
worksheet that has week numbers in column A (1 through 52 or 53) and then place names in
column B. If a person was reserved for two weeks, their name would appear in column B twice,
once beside each of the two weeks that they reserved.

With your data in this format, you could easily scan the data to see which weeks are available,
which are taken, and who they are taken by. If you want to do some sort of lookup, it is easy to
use the VLOOKUP function based on the week number, since it is the first column of the data, in
sorted order.

Calculating Averages by Date


Suppose that you have a huge worksheet that contains all the rainfall readings for a given locale
for the past hundred years or so. In cells A2:A37987 you have the dates, 1 January 1903 through
31 December 2006. In cells B2:B37987 you have the measurements for each date. Further, some
of the measurements can be zero (if there is no rainfall for the day) or blank (if no reading was
taken that particular day). With all this information, you want to calculate the average historic
rainfall for any given day of the year.

One solution involves the use of array formulas, as detailed here:

1. Select all the cells in column A that contain dates, and assign this range the name Dates.

ExcelTips: Amazing Array Formulas Page 78


Using Array Formulas with Dates

2. Select all the cells in column B that contain rainfall data, and assign this range the name
Rainfall.
3. In column D, starting in cell D2, place all the days of the year. You should end up with
D2 through D366 filled with dates.
4. In cell E2, enter the following array formula (terminate the formula by pressing
SHIFT+CTRL+ENTER). The result of the formula is the sum of all the cells in the Rainfall
range, for the date specified in cell D2.

=SUM((MONTH(Dates)=MONTH(D2))*(DAY(Dates)=DAY(D2))*Rainfall)

5. In cell F2, enter the following array formula (terminate the formula by pressing
SHIFT+CTRL+ENTER). The result of the formula is the number of cells in the Rainfall
range, for the date in cell D2, that have a value in them.

=SUM((MONTH(Dates)=MONTH(D2))*(DAY(Dates)=DAY(D2))*(Rainfall<>""))

6. In cell G2, enter the following regular formula. This is your average for the date in cell
D2.

=IF(F2<>0,E2/F2,"")

7. Select the range E2:G2 and copy down for all the dates shown in column D.

This approach works, but it takes quite a while to calculate. This is because you effectively
entered 730 array formulas, each checking over 37,000 cells. This is a lot of work, and
consequently it may appear like your machine has "hung" after you complete step 7. It has not
hung; it will just take it a while to complete the calculations.

To decrease the number of calculations that must be performed, you can use a variation on the
above steps. Follow steps 1 through 3, as noted, and then place the following array formula into
cell E2:

=AVERAGE(IF(ISNUMBER(Dates)*ISNUMBER(Rainfall)*(MONTH(Dates)=MONTH(D2))*(DAY(Date
s)=DAY(D2)),Rainfall))

You can then copy the formula down for all the dates shown in column D. The result of this
formula is the actual average rainfall, the same as had been shown in column G in the previous
approach.

This formula works because of the way that Boolean arithmetic works in Excel. The
ISNUMBER function returns either True or False, and the comparisons (MONTH and DAY)
return either True or False. These results are all multiplied against each other, resulting in True
only if all the individual tests are True. Only if they are all True will the average of the Rainfall
for that particular date be calculated.

ExcelTips: Amazing Array Formulas Page 79


Using Array Formulas with Dates

You can reduce the calculation overhead even further by simply getting rid of all the table that
calculates the averages for every day of the year. With your dates and rainfall in columns A and
B, follow these steps:

1. Select all the cells in column A that contain dates and assign this range the name Dates.
2. Select all the cells in column B that contain rainfall data and assign this range the name
Rainfall.
3. In cell D2, place the date for which you want to check the average rainfall. (The year
isn't important; only the month and day are used in the calculation.)
4. Enter the following formula into cell E2:

=AVERAGE(IF(ISNUMBER(Dates)*ISNUMBER(Rainfall)*(MONTH(Dates)=MONTH(D2))*(DAY(Date
s)=DAY(D2)),Rainfall))

That's it. Now, you can change the date in cell D2 as desired, and cell E2 will always indicate the
average rainfall for that date. The formula in cell E2 is the same as the formula used in the last
approach; the difference is that you aren't calculating it for all the days in a year, and thus the
calculation is done much quicker.

Another approach involves the use of Excel's filtering capabilities. Before you can use them
properly, however, you must create a column that shows only the month and day for each date in
your data. Use this formula in cell C2:

=MONTH(A2) & "-" & DAY(A2)

Now, turn on AutoFiltering and use the drop-down list at the top of the new column to select the
date for which you want an average. You then use the following formula, placed in any cell
desired, to show the average rainfall for the selected date:

=SUBTOTAL(1,B:B)

Averaging Values for a Given Month and Year


George has a worksheet that includes dates (in column A) and values associated with those dates
(in column B). The worksheet includes values for the last several years. He would like to
calculate the average of all the values for a given month in a given year. For instance, George
would like to calculate the average of all the values for May 2011.

There are several different ways to approach this problem. One way is to create a PivotTable
based on your data. (PivotTables are great for aggregating and analyzing huge amounts of data.)
You can easily set the value field to Average (instead of the default Sum) and group the Dates
column by whatever you want.

ExcelTips: Amazing Array Formulas Page 80


Using Array Formulas with Dates

If you'd rather not use a PivotTable, there are any number of formulas you can add to your
worksheet. For instance, the following formula uses the SUMPRODUCT function to calculate
the average:

=SUMPRODUCT((MONTH(A2:A1000)=5)*(YEAR(A2:A1000)=2011)*(B2:B1000)) /
(SUMPRODUCT((MONTH(A2:A1000)=5)*(YEAR(A2:A1000)=2011)*1))

The formula assumes your dates and values begin in row 2 (to allow for headings) and don't go
past row 1000. If there are no dates in the data that are in the month of May 2011, then the
formula returns a #DIV/0! error.

Another approach is to use an array formula, such as the following:

=AVERAGE(IF((MONTH(A2:A1000)=5)*(YEAR(A2:A1000)=2011),B2:B1000))

This approach is shorter than the SUMPRODUCT formula, but you've got to remember to hold
down CTRL+SHIFT+ENTER as you enter the formula. You'll also get the division by zero error if
there is no data for the desired month and year.

Still another approach is to use one of the database functions of Excel, DAVERAGE. All you
need to do is set up a criteria table that defines what you are looking for. Assume, for example,
that the headings on the columns are something original, like Date (cell A1) and Value (cell B1).
You could set up a criteria table in another place, such as D1:E2. The table could look like this:

Date Date
>4/30/11 <6/1/11

The criteria table says that you want DAVERAGE to use anything in which the Date column
contains a date greater than 4/30/11 and a date less than 6/1/11. Here's the formula:

=DAVERAGE(A1:B1000,"Value",D1:E2)

The first parameter defines your database, the second parameter indicates that you want to
average the information in the Value column (column B), and the third parameter tells
DAVERAGE where your criteria table is located.

One quite easy way is to apply filtering of dates and use the SUBTOTAL function. Enter the
following formula into a cell:

=SUBTOTAL(101,B2:B1000)

Select a cell in your data range and turn on the AutoFilter. Click the filtering arrow at the top of
column A and then choose Custom Filter from the drop-down list. (You may need to click the
Number Filters option or the Text Filters option before you see the Custom Filter option.) Excel
displays the Custom AutoFilter dialog box.

ExcelTips: Amazing Array Formulas Page 81


Using Array Formulas with Dates

The Custom AutoFilter dialog box.

Use the controls in the dialog box to specify that you want records greater than 4/30/11 and less
than 6/1/11. When you click on OK, only those records within May 2011 are displayed, and the
subtotal formula shows the average of those visible records.

If you are using Excel 2007 or a later version, perhaps the simplest approach is to use the
AVERAGEIFS function. It allows you to calculate an average of some values depending on
whether an associated value (the date, in this case) meets the criteria you specify. Here's how the
formula looks:

=AVERAGEIFS(B2:B1000,A2:A1000,">30 Apr 2011",A2:A1000,"<01 Jun 2011")

Notice that this is not an array formula and you don't need to define a criteria table for your
criteria—they are built directly into the formula.

Deriving Monthly Median Values


John has a huge worksheet that includes daily data covering about sixty years. He wants to come
up with a formula that will calculate the median values for each month in the time period
covered by the data.

Before proposing a solution, it is best to lay out some assumptions. For the purposes of this tip,
let's assume that the daily data is in columns A and B. In column A are the dates associated with
the data and in column B are the actual data values for each of those dates. Further, cells A1 and
B1 contain headings for each column. This means that the actual data is approximately in the
range of A2:B22000.

To make your formulas easier to use, you should define names for the data in both columns A
and B. Select the range of dates (for example, A2:A22000) and assign it a name such as
AllDates. Select the range of corresponding data (for example, B2:B22000) and use the same
process to create a name such as AllData.

ExcelTips: Amazing Array Formulas Page 82


Using Array Formulas with Dates

You can use array formulas to calculate the actual median values. This involves setting up
another data table to contain the medians. Place headings such as "Month" in cell E1 and
"Median" in cell F1. In cell E2 place the first day of the first month of your data set, such as
1/1/1940. In cell E3 put a date that is one month later, such as 2/1/1940. Select these two cells
(E2:E3) and drag the fill handle downward for the number of months you want in the data table.

If there are no blanks in your sixty years of data, then enter the following formula into cell F2:

=MEDIAN(IF(DATE(YEAR(AllDates),MONTH(AllDates),1)=E2,AllData))

Finalize the formula by pressing CTRL+SHIFT+ENTER, which tells Excel that this is an array
formula. You can then copy the formula in F2 into each of the cells in column F that has a
corresponding month in column E. The formula analyzes the dates in column B, and if the year
and month are equal to whatever date you put in cell E2, then the median is calculated from all
corresponding data points.

If there are blanks in your sixty years of data (a date in column A with no corresponding value in
column B), then the formula treats the blank as if it is a zero value. If you do have blanks, this
may result in skewed medians. To get around this, you could use a different array formula that
checks for and ignores any blank values:

=MEDIAN(IF((DATE(YEAR(AllDates),MONTH(AllDates),1)=E2)*ISNUMBER(AllData),AllData)
)

There is one caveat to using array formulas in this manner. If you have sixty years of data, with
approximately 22,000 individual values, then that is still a lot of months: about 720 of them. That
means that you are creating 720 array formulas, each analyzing 22,000 data values to arrive at an
answer. That is a lot of calculating going on, so you will notice a slow-down in the
responsiveness of Excel whenever it recalculates the worksheet.

If the sluggishness becomes an issue, then you could look at reorganizing your original data so
that each row of the worksheet represents an individual month. Column A could contain the
month for the row (1/1/1940, 2/1/1940, 3/1/1940, etc.) and the columns B:AF would be days 1
through 31 for each month. The intersecting cells in the table could then contain the data point
for each day in the month, and you could use the MEDIAN function in column AG to calculate
the median for each month. This still results in 720 formulas, but these are regular formulas that
each only need to process a month's worth of data in stead of the array formulas that need to each
process sixty years of data. The result is much faster calculations.

Of course, for most people the idea of reorganizing such a huge amount of data is enough to keep
you awake at night. Instead, you can take an entirely different approach to analyzing the data.
This approach is possible because a median is a very easy statistical function to manually
calculate. You simply sort your data set and, if the number of items in the data set is odd, select
the middle item. If the number of items is even, then you take the average of the two middle
items.

ExcelTips: Amazing Array Formulas Page 83


Using Array Formulas with Dates

To get ready to analyze the data, there are a couple of things to do. First, it will be handy to have
some way to uniquely identify the month of each data point. In cell C2 place the following
formula:

=100*Year(A2)+Month(A2)

This results in a values such as 194001, 194002, 194003, etc. being stored in column C. This is
the unique month value. Now, you should sort the data by column C and then by column B. Both
sorts should be in ascending order, so that you end up with your data first sorted by year/month
and then by value within the year/month.

Next you need to add subtotals to your data. Choose Subtotals from the Data menu, which
displays the Subtotal dialog box. You want to add a subtotal at each change in column C. The
function to use is Count, and you want to add the subtotal to column B. When you click OK, you
end up with 720 subtotals, one for each month in the data range, each one showing a count of
how many data items there were in that month.

To get the medians, add a formula to cell D2:

IF(RIGHT(B2,5)="Count", IF(MOD(C2,2)=1, INDIRECT("B"&(ROW()-1)-C2/2+1/2),


(INDIRECT("B"&(ROW()-1)-C2/2)+INDIRECT("B"&(ROW()-1)-C2/2+1))/2), "")

The formula examines what is in cell B2, and if it contains the word "Count," then it knows that
this is a subtotal row. In that case, it checks to see whether the count in cell C2 is odd or even. If
it is odd, then the INDIRECT function is used to grab whatever the median value is for the
month. If it is even, then the two middle values for the month are added together and divided in
half.

The result is that you now have median values for each month, in the same row as your subtotals.
Thus, you can collapse the outline for the data (click the plus signs in the outline area at the left
of your data) so that you only show those subtotal rows.

Counting Jobs Completed On a Date


Rob keeps statistics on each job he completes each day. For instance, he tracks the exact number
of hours and minutes spent on each job by entering a start time (column B) and finish time
(column C) for each job in the format: 05/16/11 11:25 am. In a separate column (column G) he
has the date for each day of the month. To the right of each date (in column H) he wants to show
how many jobs he completed on each of the calendar dates. Rob wonders what formula he would
use to figure out this count.

This task is not as easy as it might seem at first. The reason is because of the way in which the
start and end times are being stored. Column B contains both the start date and time in the same
column, in the format "05/16/11 11:25 am" and column C contains both the ending date and time
in the same format. You might think you can use the COUNTIF function in column G, in this
manner:

ExcelTips: Amazing Array Formulas Page 84


Using Array Formulas with Dates

=COUNTIF(C$2:C$1000,G2)

This won't work, however. The reason is simple—column G (cell G2, in this instance) contains a
date, with no time. For instance, it might contain the date 05/16/11. When this is compared to
cell C2, which might contain 05/16/11 11:25 am, they are not the same. Remember that Excel is
doing the comparison based on the date and time serial number stored in each cell. Given the
example values just mentioned, cell G2 would contain a serial number of 39217 and cell C2
would contain a serial number of 39217.47569. Since these two values are not the same,
COUNTIF doesn't count them as equal.

Obviously one solution would be to add another column that contained only the ending dates for
each job, without a time. Then you could use the COUNTIF function in your formula since you'd
be comparing "apples to apples," so to speak. But there is a solution that doesn't require using an
intermediate result in a new column. It involves making sure that the integer value of whatever is
in column C is compared with the dates stored in column G. This can be done by using the
SUMPRODUCT function, in this manner:

=SUMPRODUCT((INT(C$2:C$1000)=G2)*1)

The integer of each of the values in the range C2:C1000 is compared to the date in G2, giving an
array of True and False values. Multiplying each of these by 1 turns the True and False values
into 1 and 0 values, respectively. The formula then sums these products, giving the desired
count.

If you prefer, you could also use the following formula, which performs essentially the same task
using SUMPRODUCT:

=SUMPRODUCT(--(INT(C$2:C$1000)=G2))

One of the benefits of using the SUMPRODUCT function to determine a result is that you don't
need to use an array formula. Some people, however, prefer to use array formulas. If you like to
use them, then you can use either of the following:

=COUNT(IF(INT(C$2:C$1000)=INT(G2),1,FALSE))
=SUM(IF(ROUND(C$2:C$1000,0)=G2,1,0))

Remember that array formulas must be entered into a cell using CTRL+SHIFT+ENTER.

Getting Rid of All Rows Except the One for the


Latest Date
Gary is using an Excel worksheet to maintain of list of facilities that his company inspects, along
with the dates of all the prior inspections of those facilities. This results in multiple rows for each
facility, one row per inspection. Gary needs to delete all the rows for each facility with the

ExcelTips: Amazing Array Formulas Page 85


Using Array Formulas with Dates

exception of the latest inspection date. The result would be one row per facility, showing the
latest inspection date.

Perhaps the easiest way to do this is to use Excel's remove duplicate tool. To use the tool for this
particular purpose, follow these steps:

1. Select a cell within your data.


2. Display the Data tab of the ribbon.
3. Click the Sort tool. Excel displays the Sort dialog box.
4. Using the controls in the dialog box, indicate that you want to sort first by facility (A to
Z or Smallest to Largest, whichever is appropriate) and then by inspection date (Newest
to Oldest).

The Sort dialog box.

5. Click OK to actually sort the data.


6. With the Data tab of the ribbon still visible, click the Remove Duplicates tool in the
Data Tools group. Excel displays the Remove Duplicates dialog box.
7. Make sure that the only field selected in the dialog box is the one that contains the
facility.

ExcelTips: Amazing Array Formulas Page 86


Using Array Formulas with Dates

The Remove Duplicates dialog box.

8. Click OK. Excel removes the duplicates and leaves only those records that contain the
latest (most recent) inspection date.

Understand that if you follow these steps it is destructive to your data—when completed, the
older data is completely removed from your worksheet. Thus, if you want to maintain the older
information for historical purposes, you may want to perform the steps on a duplicate of your
data.

Of course, you could also use a different approach that maintains the original data and simply
extracts the information that represents the latest inspection dates. Assume, for the purposes of
this example, that your data is in columns A:C, with A containing the facility, B containing the
inspection date, and C containing the rating achieved on that date. Further, the first row of your
data contains headings (Facility, Inspected, and Rating). Somewhere to the right of your data—
separated by at least one empty column—place another set of identical headings. (For this
example I'll assume that these appear columns E:G.)

In the first column place a unique list of your facilities. In cell F2 place the following formula:

=MAX(($A$2:$A$123=E2)*$B$2:$B$123)

You can replace the two lower range references ($A$123 and $B$123) with whatever lower
range is appropriate for your data. Also, you need to enter this as an array formula, meaning you
press CTRL+SHIFT+ENTER to add it to cell F2.

The result in cell F2 will be a number, which is actually a date. (Excel maintains dates internally
as numbers.) To get F2 to look like a date, simply apply a date format to the cell.

In cell G2 place the following formula:

ExcelTips: Amazing Array Formulas Page 87


Using Array Formulas with Dates

=SUMIFS($C$2:$C$123,$A$2:$A$123,E2,$B$2:$B$123,F2)

Again, the lower range references can be replaced with whatever reference is appropriate for
your data. This is not an array formula, so you can simply press ENTER to put it in cell G2.

Now copy cells F2:G2 down as many rows as appropriate for your facilities. What you end up
with is a dynamic list of the most recent inspection results for each facility.

A dynamic list of the latest inspecition results.

As you add more data to your inspection list, your "result table" is updated to always show the
latest inspection results.

Creating a Year-to-Date Comparison Chart


Nora has kept a weather log for years. Each sheet of the workbook is a separate year, with
column A on each sheet being the dates in the year and column B being the amount of
precipitation on that day, if any. Nora would like to create a chart showing this year's
precipitation vs. last year's precipitation, to date. She wonders if there is a way to automatically
have this chart reference the correct precipitation values for both years based on today's date.

There are a couple of ways that this need can be filled, depending on exactly what you want to
achieve. If you just want to compare rainfall this year to rainfall last year, by date, then you can
easily do it by setting up some dynamic named ranges that define the data you want to use.

To begin with, let's assume that your data for 2015 is in a worksheet named 2015 and your data
for 2016 (so far) is in a worksheet named 2016. On each worksheet, row 1 contains headings,
which means that your dates actually start in cell A2 and your precipitation readings in cell B2.
Follow these steps to set up the ranges:

ExcelTips: Amazing Array Formulas Page 88


Using Array Formulas with Dates

1. Display the Formulas tab of the ribbon.


2. In the Defined Names group, click the Name Manager tool. Excel displays the Name
Manager dialog box.
3. Click the New button. Excel displays the New Name dialog box.

The New Name dialog box.

4. Replace whatever is in the Name field with the name CurrentYear (note that this is a
single word, with no spaces).
5. In the Refers To field, enter the following formula:
=OFFSET(INDIRECT(YEAR(NOW())&"!A1"),1,1,TODAY()-DATE(YEAR(NOW()),1,1)+1,1)

6. Click the OK button to finish creating the named range. The new range should appear in
the Name Manager dialog box.
7. Click the New button again. Excel (again) displays the New Name dialog box.
8. In the Name field, enter the name PreviousYear (note that this is, again, a single word,
with no spaces).
9. In the Refers To field, enter the following formula:
=OFFSET(INDIRECT(YEAR(NOW())-1&"!A1"),1,1,TODAY()-DATE(YEAR(NOW()),1,1)+1,1)

10. Click the OK button. The new range should appear in the Name Manager dialog box.
11. Click the New button a third time. Excel displays the New Name dialog box.
12. In the Name field, enter the name Dates.
13. In the Refers To field, enter the following formula:
=OFFSET(INDIRECT(YEAR(NOW())&"!A1"),1,0,TODAY()-DATE(YEAR(NOW()),1,1)+1,1)

14. Click the Close button to dismiss the Name Manager dialog box.

With the ranges defined, you can now create the chart using those ranges:

ExcelTips: Amazing Array Formulas Page 89


Using Array Formulas with Dates

1. Display the Insert tab of the ribbon.


2. In the Charts group click the Column option. Excel displays a drop-down list.
3. Click once on the Clustered Column format. (This is the first option under the 2-D
Column heading.) Excel creates a blank chart object in your worksheet.
4. Right-click the chart object. Excel displays a Context menu.
5. Choose Select Data from the Context menu. Excel displays the Select Data Source
dialog box, which should be completely empty because you've not added any data
ranges to the chart.
6. At the left side of the dialog box, under the Legend Entries (Series) heading, click the
Add button. Excel displays the Edit Series dialog box.

The Edit Series dialog box.

7. In the Series Name field, enter 2015.


8. In the Series Values field, enter this: '2015'!PreviousYear.
9. Click the OK button. The data series now appears in the Select Data Source dialog box.
10. Click, again, the same Add button you clicked in step 6. Excel again displays the Edit
Data Series dialog box.
11. In the Series Name field, enter 2016.
12. In the Series Values field, enter this: '2016'!CurrentYear.
13. Click the OK button. This second data series now appears in the Select Data Source
dialog box.
14. At the right side of the dialog box, under the Horizontal (Category) Axis Labels
heading, click the Edit button. Excel displays the Axis Labels dialog box.

The Axis Labels dialog box.

15. In the Axis Label Range, enter this: '2016'!Dates.

ExcelTips: Amazing Array Formulas Page 90


Using Array Formulas with Dates

16. Click the OK button. The Select Data Source dialog box should be fully filled out with
the necessary information.

The filled-in Select Data Source dialog box.

17. Click OK to dismiss the Select Data Source dialog box.

Your updated chart, showing only the dates up through today's date, should now be visible. You
can continue to format the chart, as desired. (For instance, you'll probably want to format the
dates in the chart so they don't include a year.) Further, the chart is dynamic, so that when you
open the workbook tomorrow it will reflect one more day than it did today.

Another way to handle it is to reconsider how you are storing your data. Instead of storing all
your precipitation readings on separate worksheets (by year), put them all on a single worksheet.
Since Excel can handle over a million rows of data in a worksheet, you won't run up against any
practical limitations. (A million rows represents well over 2,700 years.)

Now, on a different worksheet, you can use two array formulas to calculate the cumulative
rainfall for both years, to date. The following array formula will provide the rainfall for the
previous year:

=SUM(Data!B2:B1000*IF(Data!A2:A1000>=DATE(YEAR(NOW())-
1,1,1),IF(Data!A2:A1000<=EDATE(NOW(),-12),1,0)))

This assumes that the original precipitation readings are on a worksheet named Data and that
they don't extend beyond 1000 rows. (You can modify either of these, as necessary.) To get the
to-date rainfall for this year, you can use this array formula:

=SUM(Data!B2:B1000*IF(Data!A2:A1000>=DATE(YEAR(NOW()),1,1),IF(Data!A2:A1000<=NOW(
),1,0)))

ExcelTips: Amazing Array Formulas Page 91


Using Array Formulas with Dates

Remember: These are both array formulas, so they should be entered using CTRL+SHIFT+ENTER.
The single value returned by each formula represents the cumulative rainfall from each year, to
date. These two values can then be used in any chart that you want.

ExcelTips: Amazing Array Formulas Page 92


Using Array Formulas for Lookups

Using Array Formulas for Lookups

Complex Lookup Formulas


Eddie has a series of part numbers in the format 123/45678 or 011/00345. He needs to look up
the name of the corresponding part number from a different table. This table consists of three
columns. The first column contains the portion of the part number before the slash (such as 123
or 011) and the second column contains the portion after the slash (such as 45678 or 00345). The
third column contains the name that Eddie wants to look up.

Part of the problem Eddie is having is that in the table, the two columns that each contains a
portion of the part number are numeric values. Thus the full part number isn't 011 and 00345, but
11 and 345. Eddie is wondering how to put together a lookup formula for the part numbers
(123/45678 or 011/00345) to return the proper part name from the table.

To start with, you need to make some changes to the table that contains the part numbers. The
first two columns should be formatted as text, not as numbers. This will treat the values in those
columns as text, so that when you enter "011" in the first column, it retains the leading zero. If
you have already entered the value as 011 before formatting the column as text, it will still be
displayed as 11 (without the leading zero). You will need to not only change the format of the
column, but also re-enter any part-number fragments that contain leading zeros.

Next you need to make sure that your part number table includes column headers. For this
example, make sure the three column headers are Left, Right, and Name. (You can make these
headers bold and underlined, which helps set them off from the values in each column.) Then,
create names for the individual columns by following these steps:

1. Select the cells in the part number table. Make sure you also select the newly created
headers for each column of the table.

The selected cells of the part-number table.

ExcelTips: Amazing Array Formulas Page 93


Using Array Formulas for Lookups

2. Display the Create Names from Selection dialog box (Excel 2007 and later) or the
Create Names dialog box (Excel 2003 and earlier). In Excel 2007 or later versions
display the Formulas tab of the ribbon and click the Create from Selection tool in the
Defined Names group. In older versions of Excel choose Insert | Name | Create.

The Create Names from Selection dialog box.

3. Make sure that only the Top Row check box is selected.
4. Click OK.

With your part-number table prepared, you are now ready to look up part numbers. In the cells
just to the right of your lookup values (123/45678 and 011/00345) you are going to enter an
array formula. Assuming that the first part number is in cell A1, you would enter the following
array formula in cell B1:

=INDEX(Name,MATCH(A1,Left&"/"&Right,0))

Remember that to denote this as an array formula you enter it by pressing CTRL+SHIFT+ENTER.
The formula then appears in the Formula bar with {braces} around it. You can copy the formula
down to the other cells in column B, as necessary.

The formula works by pulling the value from the Name column of the table when the
corresponding Left-column value concatenated with a slash and the Right-column value matches
whatever is in cell A1. If there is no match, the formula returns a #N/A error value, otherwise it
returns the desired part number.

Note that this approach will work only if you format the first two columns of your part-number
table as text and make sure that the various cells contain any leading zeros. If, for some reason,
you cannot format the first two columns of the table in this way (perhaps there are too many of
them), then you need to change the lookup formula you are using:

=INDEX(Name,MATCH(A1,TEXT(Left,"000")&"/"&TEXT(Right,"00000"),0))

Note that this version of the formula (which should still be entered as an array formula) uses the
TEXT function in two places to convert the values from the Left and Right columns so they have
leading zeros.

ExcelTips: Amazing Array Formulas Page 94


Using Array Formulas for Lookups

Making VLOOKUP Case Sensitive


Robin asked if there is a way to do a VLOOKUP that is case sensitive. Her lookup table/range
has entries that are similar (AbC and aBC) with the only difference being the case of the letters.
She can't change the values (make them all upper or lower case) since the unique values are vital.

The VLOOKUP function doesn't have a way to check for the case of information; it is case
insensitive. There are several ways you can work around this shortcoming, however. One way is
to use the CODE function to create an intermediate column that can be searched by VLOOKUP.
Assuming that your original data is in column B, you could put the following formula in cell A1
and copy it down the column:

=CODE(LEFT(B1,1))&"."&CODE(MID(B1,2,1))&"."&CODE(RIGHT(B1,1))

This formula looks at the first three characters of whatever is in cell B1 and converts those
characters to decimal character codes separated by periods. Thus, if A1 contained "ABC" then
B1 would contain "65.66.67". Assuming that the value you want to locate is in cell C1, you
could use the following as your VLOOKUP formula:

=VLOOKUP(CODE(LEFT(C1,1))&"."&CODE(MID(C1,2,1))&"."&
CODE(MID(C1,3,1)), A:B,2,)

Another approach is to use the EXACT function to determine the location of what you are
looking for. This approach doesn't use VLOOKUP at all, instead relying on the INDEX function.
The formula assumes that the cells you want to compare are in column A and what you want to
return is the corresponding cell in column B.

=IF(MIN(IF(EXACT(C1,$A$1:$A$100),ROW($A$1:$A$100)))=0,NA(),
INDEX($B$1:$B$100,MIN(IF(EXACT(C1,$A$1:$A$100),ROW($A$1:$A$100)))))

This formula needs to be entered as an array formula (CTRL+SHIFT+ENTER). The first part of the
formula (the first instance of EXACT) compares C1 (what you are looking for) to each value in
the range A1:A100. Since this is an array formula, you end up with, in this case, 100 True/False
values depending on whether there is an exact match or not. If there is a match, then the first
ROW function returns the row of the match and the INDEX function is used to grab the value
from column B in that row.

A few additional approaches can be found at the following Knowledge Base article:

http://support.microsoft.com/?kbid=214264

Looking Backward through a Data Table


Kirk has a large data table in Excel. Each row has a vehicle number, date (the table is sorted by
this column), beginning mileage, and ending mileage. He would like to search backwards

ExcelTips: Amazing Array Formulas Page 95


Using Array Formulas for Lookups

through the data table to find the ending mileage for the same vehicle number to use as the
beginning mileage in the current row—similar to VLOOKUP, but looking bottom to top rather
that top to bottom.

There are several ways you can approach this with a formula. Assume, for this example, that the
vehicle number is in column A, the date in column B, the starting mileage in column C, and the
ending mileage in column D. What you need is a formula you can put in column C that looks up
the most recent ending mileage for current vehicle. The following formula provides one
approach; you should place it in cell C3:

=LOOKUP(2,1/FIND(A3,A$2:A2,1),D$2:D2)

You can copy the formula down the column as far as you need. If the vehicle number, in column
A, has not appeared earlier in the data table, then the formula will return an error such as
#VALUE! or #N/A. In that case, you can easily type over the formula with the starting mileage
that you want to use for the vehicle.

Here's another formulaic approach, but this one should be entered as an array formula (by
pressing CTRL+SHIFT+ENTER):

=IF(A3="","",MAX(IF(($A$2:A2=A3)*($D$2:D2),$D$2:D2)))

Again, place the formula in cell C3 and copy it down as far as needed. This one doesn't return an
error value if the vehicle hasn't appeared earlier in the data table; it returns a value of 0. You can
then type over the formula with the real starting mileage for that vehicle. The following array
formula could also be used:

=IF(A3="","",INDIRECT("D"&LARGE(($A$2:A3=A3)*ROW($2:3),2)))

The difference with this array formula is that if the vehicle hasn't appeared earlier in the data
table, it returns a #REF! error.

Here are two array formulas that are even shorter that you can use in C3 (and, again, copy down
as needed):

=MAX((D$2:D2)*(--(A$2:A2=A3)))
=MAX(IF(A$2:A2=A3,D$2:D2))

Pulling a Phone Number with a Known First and


Last Name
Kimm has a worksheet that has three columns in it. Column A contains the last name of a person,
column B contains the first name, and column C contains the phone number for the individual. If
Kimm knows the person's first and last name (say they are in cells F1 and F2, respectively), she

ExcelTips: Amazing Array Formulas Page 96


Using Array Formulas for Lookups

wonders what sort of lookup formula she would use to return the phone number for the first
person matching that first and last name.

There are actually several different formulas you could use to figure out the phone number. Most
of the approaches involve using array formulas, which are always entered in a cell by using
CTRL+SHIFT+ENTER. To make the formulas easier to understand, it is best to work with named
ranges. For instance, set up the following names:

• Select all the last names, in column A, and give them a name such as LNames.
• Select all the first names, in column B, and give them a name such as FNames.
• Select all the phone numbers, in column C, and give them a name such as Phones.

Here are a variety of array formulas you could use to find the phone number:

=SUMPRODUCT(--(LNames=F2),--(FNames=F1),Phones)
=INDEX(Phones,MATCH(F1&F2,FNames&LNames,))
=INDEX(Phones,INDEX(MATCH(F1&F2,FNames&LNames,0),))
=OFFSET(C1,MATCH(F1&F2,FNames&LNames,),)

In addition, you can construct an array formula that relies upon the ROW function, as shown
here:

=INDEX(Phones,SUMPRODUCT((F1&F2=FNames&LNames)*(ROW(FNames)-1)))
=INDEX(Phones,MIN(IF((FNames=F1)*(LNames=F2),(ROW(Phones)-1))))

If you use this approach (relying upon the ROW function), you may need to adjust the "-1"
portion of the formulas to reflect the number of rows that appear before your actual data. In this
instance, the row is decremented by one because the first row of the data table consists of the
headers for each column; the data itself begins in row 2.

Note that all of the formulas used thus far rely upon combining the first name with the last name
in order to make a comparison. This has the chance of running into "false positives" in some
instances. For example, let's say that two of the names you have in your data are Thom
Astonfield and Thomas Tonfield. Since the case of the letters in the names doesn't matter in these
formulas, when you combine first and last names of these individuals, they are exactly the same.
Thus, if you are looking for the phone number for Thomas Tonfield and his name appears in the
list after Thom Astonfield, then you'll always get Thom's phone number instead of Thomas'.

To get around this potential problem, you may want to include some sort of separator between
the first and last name. Using one of the formulas from earlier in the tip, all you would need to do
is make an adjustment, as shown here:

=INDEX(Phones,SUMPRODUCT((F1&":"&F2=FNames&":"&LNames)*(ROW(FNames)-1)))

The addition of the colon between the first and last names acts as a separator, eliminating the
likelihood of false positives.

ExcelTips: Amazing Array Formulas Page 97


Using Array Formulas for Lookups

You should also realize that if cells F1 and F2 contain a name that doesn't appear in the data at
all, the information you get back from the formula will be erroneous. Depending on the variation
of the function used, you may get back an actual error condition (such as #N/A or #REF) or you
may get back plain wrong data. In those instances where an error condition is returned, you may
want to adjust your formula to account for the possibility of not finding a match, in this manner:

=IF(ISERROR(INDEX(Phones,MATCH(F1&":"&F2,FNames&":"&LNames,0))),
"no phone",INDEX(Phones,MATCH(F1&":"&F2,FNames&":"&LNames,0)))

There are some things that you should keep in mind when using these types of formulas. First
and foremost, the reliability of the information you get back is going to depend largely on the
quality of the information in your data list. If your data has misspellings in it, contains blanks, is
sorted in some strange order, or there are multiple entries for the same person, it can affect what
the formula returns.

If you aren't too sure about the quality of your data, you may want to simply use Excel's filtering
capabilities rather than a formula. Apply an AutoFilter, and you can use the first two columns of
your data to pick first and last name. This will then return any phone numbers for the individual
you select. It is very simple to do and makes selecting the data you need easy.

Finally, you should realize that there are other approaches you can use to addressing the
problem. For instance, you could create a formula that uses the DGET function, but doing so
would require the addition of a small criteria table to your worksheet or workbook. Since Kimm
specified that she couldn't add intermediate results in a worksheet, then an editorial decision was
made to not include the DGET function as a solution since it would require the addition of the
criteria table.

Searching for a Value Using a Function


Thor wonders if there is a way to perform a lookup without having to specify a specific column
or row and having the result be the address of the cell at which the value is found. For instance,
he wants to look up a value (such as 345 or "my text") and have the function search all the cells
in all the worksheets in the workbook and return the full address of the cell in which the value
was found.

The approach you use will be dictated by the range you want to search. If you want to search on
the same worksheet on which you want the answer displayed, then you can use a formula, such
as the following:

=ADDRESS(MAX(ROW(1:5)*(A1:E5="my text")),
MAX(COLUMN(A1:E1)*(A1:E5="my text")),4)

This should be entered as an array formula (press CTRL+SHIFT+ENTER), and it only searches in
the range A1:E5. You can, if desired, change the range by adjusting the formula appropriately.

ExcelTips: Amazing Array Formulas Page 98


Using Array Formulas for Lookups

A larger search area would be to look at an entire worksheet. This can still be done using a array
formula, such as the following:

=ADDRESS(MAX(ROW(Sheet1!1:65000)*(IF(Sheet1!1:65000=$A$1,1,0))),
MAX(COLUMN(Sheet1!$1:$65000)*IF(Sheet1!1:65000=$A$1,1,0)))

The formula assumes that what you are looking for is stored in cell A1. You should change the
Sheet1 designation to the name of whatever worksheet you want searched.

Transposing and Linking


You may already know that you can transpose information by using the Paste Special abilities of
Excel. The information that is pasted is not dynamic, however. In some instances it might be nice
to have the transposed information always reflect what is in the original, un-transposed
information.

Here's a cool way to transpose information and have it linked to the original information:

1. Figure out how many rows and columns are in your original information. For instance,
if the original data is in the range A4:E10, then it has 7 rows and 5 columns.
2. Select another place in the workbook (perhaps even on the same worksheet) that has the
transposed number of rows and columns. For instance, if you are transposing A4:E10,
you should select blank cells that consist of 5 rows and 7 columns.
3. With the range still selected, click once in the Formula bar and type the following:
=transpose(A4:E10). Make sure the address reflects the original data range you are
transposing. If you are on a different worksheet, make sure you use an address that
contains the worksheet name of your data, as in =transpose(Sheet1!A4:E10).
4. Press CTRL+SHIFT+ENTER to signify to Excel that you are entering an array formula.

That's it. You've now got a dynamic transposed table, and any changes you make in the original
are automatically reflected in the transposed version.

ExcelTips: Amazing Array Formulas Page 99


I'd Like Your Feedback
Do you like ExcelTips: Amazing Array Formulas? Is it helpful? Is it a good value? Did you
expect more (or less) than what you got?

I'd like your feedback about this book, as feedback is helpful in planning the future of ExcelTips.
If you have something you'd like to share with me, just drop me a line. Here's my contact
information:

Allen Wyatt
Sharon Parq Associates, Inc.
PO Box 794
Orem, UT 84059

allen@sharonparq.com

Drop me a line—I'm anxious to hear from you!

PS: If you have a formal "testimonial" about ExcelTips: Amazing Array Formulas, send it my
way. I have an uneven and inconsistent habit of rewarding concise, pithy, and glowing
testimonials. (I've even been known to reward negative feedback. All of it is important to me.)
ExcelTips Archives
Your best source of usable Excel information!

For more ExcelTips products, visit our Web site:


http://store.tips.net/

You might also like