Sei sulla pagina 1di 74

Is A Particular Word Contained In A Text String?

Category: Formulas / General VBA | [Item URL] Here's a VBA function that might be useful in some situations. The ExactWordInString functions returns True if a specified word is contained in a text string. You might think that this function is just a variation on Excel's FIND function or VBA's Instr function. There's a subtle difference. The ExactWordInString function looks for a complete word -- not text that might be part of a different word. The examples in the accompanying figure should clarify how this function works. Cell C2 contains this formula, which was copied to the cells below:
=ExactWordInString(A2,B2)

The function identifies the complete word trapped, but not the word trap, which is part of trapped. Also, note that a space is not required after a word in order to identify it as a word. For example, the word can be followed by a punctuation mark. The function, listed below, modified the first argument (Text) and replaces all non-alpha characters with a space character. It then adds a leading and trailing space to both arguments. Finally, it uses the Instr function to determine if the modified Word argument is present in the modified Text argument. To use this function in a formula, just copy and paste it to a VBA module in your workbook.
Function ExactWordInString(Text As String, Word As String) As Boolean ' Returns TRUE if Word is contained in Text as an exact word match Dim i As Long Const Space As String = " " Text = UCase(Text) ' Replace non-text characters with a space For i = 0 To 64 Text = Replace(Text, Chr(i), Space) Next i For i = 91 To 255 Text = Replace(Text, Chr(i), Space) Next i

'

Add initial and final space to Text & Word Text = Space & Text & Space Word = UCase(Space & Word & Space) ExactWordInString = InStr(Text, Word) <> 0

End Function

* Update * Excel MVP Rick Rothstein sent me a much simpler function that produces the same result. In fact, it uses just one statement:
Function ExactWordInString(Text As String, Word As String) As Boolean ExactWordInString = " " & UCase(Text) & " " Like "*[!A-Z]" & UCase(Word) & "[!A-Z]*" End Function

Formulas To Perform Day Of Month Calculations


Category: Formulas | [Item URL] Many events are scheduled for a particular occurrence of the day within a month. For example, payday might be the last Friday of every month. Or, a meeting might be scheduled for every second Monday of the month. Excel doesn't have a function that can calculate these types of dates, but it's possible to create a formula. In the figure below, the formula in cell D4 calculates the date based on the parameters in column C. The formula in D4 is:
=DATE(C3,C4,1+((C6-(C5>=WEEKDAY(DATE(C3,C4,1))))*7)+(C5-WEEKDAY(DATE(C3,C4,1))))

This formula is not always accurate, however. If you specify a day number that doesn't exist (for example, the 6th Friday), it returns a date in the following month. Cell D6 contains a modified formula that displays "(none)" if the date isn't in the month specified. This formula is much longer:
=IF(MONTH(DATE(C3,C4,1+((C6-(C5>=WEEKDAY(DATE(C3,C4,1))))*7)+ (C5-WEEKDAY(DATE(C3,C4,1)))))<>C4,"(none)",DATE(C3,C4,1+ ((C6-(C5>=WEEKDAY(DATE(C3,C4,1))))*7)+(C5-WEEKDAY(DATE(C3,C4,1)))))

In some cases, you might need to determine the last occurrence of a day in a particular month. This calculation requires a different formula (refer to the figure below):

=DATE(C9,C10+1,1)-1+IF(C11>WEEKDAY(DATE(C9,C10+1,1)-1), C11-WEEKDAY(DATE(C9,C10+1,1)-1)-7,C11-WEEKDAY(DATE(C9,C10+1,1)-1))

In this figure, the formula in cell D10 displays the date of the last Friday in March, 2008. The download file for this tip contains another example that has an easy-to-use interface. The user can select the parameters from drop-down lists. The megaformula in the Calculated Date column is very complex because it needs to covert words into values.

Making An Exact Copy Of A Range Of Formulas, Take 2


Category: General / Formulas | [Item URL] When you copy a range of formulas and paste them to a new location, Excel adjusts the cell references automatically. Most of the time, this is exactly what you want. Consider this simple formula:
=SUM(A2:A13)

If you copy this formula and paste it to the next column, the references are adjusted and the pasted formula is:
=SUM(B2:B13)

Making an exact copy of a single formula is easy: Press F2, highlight the formula, and press Ctrl+C to copy it as text. Then paste it to another cell. In some situations, however, you might need to make an exact copy of a range of formulas. In an older tip, I described a rather complicated way to do this. See Making An Exact Copy Of A Range Of Formulas. Matthew D. Healy saw that tip and shared another method, which uses Notepad. Here's how it works:

1. Put Excel in formula view mode. The easiest way to do this is to press Ctrl+` (that character is a "backwards apostrophe," and is usually on the same key that has the ~ (tilde).

2. Select the range to copy. 3. Press Ctrl+C 4. Start Windows Notepad 5. Press Ctrl+V to past the copied data into Notepad 6. In Notepad, press Ctrl+A followed by Ctrl+C to copy the text 7. Activate Excel and activate the upper left cell where you want to paste the formulas. And, make sure that the sheet you are copying to is in formula view mode. 8. Press Ctrl+V to paste. 9. Press Ctrl+` to toggle out of formula view mode. Note: If the paste operation back to Excel doesn't work correctly, chances are that you've used Excel's Text-to-Columns feature recently, and Excel is trying to be helpful by remembering how you last parsed your data. You need to fire up the Convert Text to Columns Wizard. Choose the Delimited option and click Next. Clear all of the Delimiter option checkmarks except Tab.

Calculating Easter
Category: Formulas | [Item URL]

Easter is one of the most difficult holidays to calculate. Several years ago, a Web site had a contest to see who could come up with the best formula to calculate the date of Easter for any year. Here's one of the formulas submitted (it assumes that cell A1 contains a year):
=DOLLAR(("4/"&A1)/7+MOD(19*MOD(A1,19)-7,30)*14%,)*7-6

Just for fun, I calculated the date of Easter for 300 years from 1900 through 2199. Then I created a pivot table, and grouped the dates by day. And then, a pivot chart:

During this 300-year period, the most common date for Easter is March 31 (it occurs 13 times on that data). The least common is March 24 (only one occurrence). I also learned that the next time Easter falls on April Fool's Day will be in 2018.

Converting Unix Timestamps


Category: Formulas | [Item URL] If you import data you might encounter time values stored as Unix timestamps. Unix time is defined as the number of seconds since midnight (GMT time) on January 1, 1970 -- also known as the Unix epoch. For example, here's the Unix timestamp for August 4, 2008 at 10:19:08 pm (GMT):
1217888348

To create an Excel formula to convert a Unix timestamp to a readable data and time, start by converting the seconds to days. This formula assumes that the Unix timestamp is in cell A1:
=(((A1/60)/60)/24)

Then, you need to add the result to the date value for January 1, 1970. The modified formula is:
=(((A1/60)/60)/24)+DATE(1970,1,1)

Finally, you need to adjust the formula for the GMT offset. For example, if you're in New York the GMT offset is -5. Therefore, the final formula is:
=(((A1/60)/60)/24)+DATE(1970,1,1)+(-5/24)

A simpler (but much less clear) formula that returns the same result is:
=(A1/86400)+25569+(-5/24)

Both of these formulas return a date/time serial number, so you need to apply a number format to make it readable as a date and time.

Naming Techniques
Most Excel users know how to name cells and ranges. Using named cells and ranges can make your formulas more readable, and less prone to errors. Most users, however, don't realize that Excel lets you provide names for other types of items. This document describes some useful naming techniques that you may not be aware of.

Naming a constant
If formulas in your worksheet use a constant value (such as an interest rate), the common procedure is to insert the value for the constant into a cell. Then, if you give a name to the cell (such as InterestRate), you can use the name in your formulas. Here's how create a named constant that doesn't appear in a cell:

1. Select the Insert Name Define command to display the Define Name dialog box.
2. Enter the name (such as InterestRate) in the field labeled Names in workbook. 3. Enter the value for the name in the Refers to field (this field normally holds a formula). For example, you can enter =.075. 4. Click OK

Try it out by entering the name into a cell (preceded by an equal sign). For example, if you defined a name called InterestRate, enter the following into a cell:
=InterestRate

This formula will return the constant value that you defined for the InterestRate name. And this value does not appear in any cell.

Names are actually named formulas


Here's another way of looking at names. Whenever you create a name, Excel actually creates a name for a formula. For example, if you give a name (such as Amount) to cell D4, Excel creates a name for this formula:
=$D$4

You can use the Define Name dialog box and edit the formula for a name. And you can use all of the standard operators and worksheet functions. Try this: 1. Create a name for cell D4. Call it Amount. 2. Enter =Amount into any cell. The cell will display the value in cell D4. 3. Use the Insert Name Define command and edit the refers to field so it appears as =$D$4*2 You'll find that entering =Amount now displays the value in cell D4 multiplied by 2.

Using relative references


When you create a name for a cell or range, Excel always uses absolute cell references for the range. For example, if you give the name Months to range A1:A12, Excel associates $A$1:$A$12 (an absolute reference) with the name Months. You can override the absolute references for a name and enter relative references. To see how this works, follow the steps below to create a relative name called CellBelow. 1. Select cell A1. 2. Select the Insert Name Define command to display the Define Name dialog box. 3. Enter the name CellBelow in the field labeled Names in workbook. 4. Replace the value in the Refers to field with =A2 (this is a relative reference) 5. Click OK Try it out by entering the following formula into any cell:
=CellBelow

You'll find that this formula always returns the contents of the cell directly below. NOTE: It's important to understand that the formula you enter in Step 4 above depends on the active cell. Since cell A1 was the active cell, =A2 is the formula that returns the cell below. If, for example, cell C6 was the active cell when you created the name, you would enter =C7 in step 4.

Using mixed references


You can also used "mixed" references for you names. Here's a practical example of how to create a name that uses mixed references. This name, SumAbove, is a formula that returns the sum of all values above the cell. 1. Activate cell A3. 2. Select the Insert Name Define command to display the Define Name dialog box.

3. In the Names in workbook field, enter SumAbove. 4. In the Refers to field, enter =SUM(A$1:A2) Notice that the formula in Step 3 is a mixed reference (the row part is absolute, but the column part is relative). Try it out by entering =SumAbove into any cell. You'll find that this formula returns the sum of all cells in the column from Row 1 to the row directly above the cell.

Creating A List Of Formulas


Most users have discovered that Excel has an option that lets you display formulas directly in their cells: Choose Tools Options, click the View tab, and select the Formulas checkbox. However, Excel doesn't provide a way to generate a concise list of all formulas in a worksheet. The VBA macro below inserts a new worksheet, then creates a list of all formulas and their current values. NOTE: My Power Utility Pak add-in includes a more sophisticated version of this subroutine, plus several other auditing tools. To use this subroutine: 1. Copy the code below to a VBA module. You can also store it in your Personal Macro Workbook, or create an add-in. 2. Activate the worksheet that contains the formulas you want to list. 3. Execute the ListFormulas subroutine. The subroutine will insert a new worksheet that contains a list of the formulas and their values.

The ListFormulas Subroutine


Sub ListFormulas() Dim FormulaCells As Range, Cell As Range Dim FormulaSheet As Worksheet Dim Row As Integer ' Create a Range object for all formula cells On Error Resume Next Set FormulaCells = Range("A1").SpecialCells(xlFormulas, 23) ' Exit if no formulas are found If FormulaCells Is Nothing Then MsgBox "No Formulas." Exit Sub End If ' Add a new worksheet Application.ScreenUpdating = False Set FormulaSheet = ActiveWorkbook.Worksheets.Add FormulaSheet.Name = "Formulas in " & FormulaCells.Parent.Name

'

Set up the column headings With FormulaSheet

Range("A1") = "Address" Range("B1") = "Formula" Range("C1") = "Value" Range("A1:C1").Font.Bold = True End With ' Process each formula Row = 2 For Each Cell In FormulaCells Application.StatusBar = Format((Row - 1) / FormulaCells.Count, "0%") With FormulaSheet Cells(Row, 1) = Cell.Address _ (RowAbsolute:=False, ColumnAbsolute:=False) Cells(Row, 2) = " " & Cell.Formula Cells(Row, 3) = Cell.Value Row = Row + 1 End With Next Cell ' Adjust column widths FormulaSheet.Columns("A:C").AutoFit Application.StatusBar = False End Sub

Cell Counting Techniques


Excel provides many ways to count cells in a range that meet various criteria:

The DCOUNT function. The data must be set up in a table, and a separate criterion range is required. The COUNT function. Simply counts the number of cells in a range that contain a number. The COUNTA function. Counts the number of non-empty cells in a range. The COUNTBLANK function. Counts the number of empty cells in a range. The COUNTIF function. Very flexible, but often not quite flexible enough. An array formula. Useful when the other techniques won't work.

Formula Examples
Listed below are some formula examples that demonstrate various counting techniques. These formula all use a range named data. To count the number of cells that contain a negative number:
=COUNTIF(data,"<0")

To count the number of cells that contain the word "yes" (not case sensitive):
=COUNTIF(data,"yes")

To count the number of cells that contain any text:


=COUNTIF(data,"*")

To count the number of cells that contain text that begins with the letter "s" (not case-sensitive):
=COUNTIF(data,"s*")

To count the number of cells that contain the letter "s" (not case-sensitive):
=COUNTIF(data,"*s*")

To count the number of cells that contain either "yes" or "no" (not case-sensitive):
=COUNTIF(data,"yes")+COUNTIF(data,"no")

To count the number of three-letter words:


=COUNTIF(data,"???")

To count the number of cells that contain a value between 1 and 10:
=COUNTIF(data,">=1")-COUNTIF(data,">10")

To count the number of unique numeric values (ignores text entries):


=SUM(IF(FREQUENCY(data,data)>0,1,0))

To count the number of cells that contain an error value (this is an array formula, entered with Ctrl+Shift+Enter):
=SUM(IF(ISERR(data),1,0))

Using the formulas in VBA


You can also use these techniques in your VBA code. For example the VBA statement below calculates the number of three-letter words in a range named data, and assigns the value to the NumWords variable:
NumWords = Application.COUNTIF(Sheets("Sheet1").Range("data"), "???")

The other formula examples listed above can also be converted to VBA.

Summing And Counting Using Multiple Criteria


If you peruse the Excel newsgroups, you've probably realized that one of the most common questions involves summing or counting using multiple criteria. If your data is set up as a database table you can use database functions such as DCOUNT or DSUM. These functions, however, require the use of a separate criteria range on your worksheet. This tip provides a number of examples that should solve most of your counting and summing problems. Unlike DCOUNT and DSUM, these formulas don't require a criteria range. The example formulas presented in this tip use the simple database table shown below. You will need to adjust the formulas to account for your own data.

Sum of Sales, where Month="Jan"


This is a straightforward use of the SUMIF function (it uses a single criterion):
=SUMIF(A2:A10,"Jan",C2:C10)

Count of Sales, where Month="Jan"


This is a straightforward use of the COUNTIF function (single criterion):
=COUNTIF(A2:A10,"Jan")

Sum of Sales, where Month<>"Jan"


Another simple use of SUMIF (single criterion):
=SUMIF(A2:A10,"<>Jan",C2:C10)

Sum of Sales where Month="Jan" or "Feb"


For multiple OR criteria in the same field, use multiple SUMIF functions:
=SUMIF(A2:A10,"Jan",C2:C10)+SUMIF(A2:A10,"Feb",C2:C10)

Sum of Sales where Month="Jan" AND Region="North"


For multiple criteria in different fields, the SUMIF function doesn't work. However, you can use an array formula. When you enter this formula, use Ctrl+Shift+Enter:
=SUM((A2:A10="Jan")*(B2:B10="North")*C2:C10)

Sum of Sales where Month="Jan" AND Region<>"North"


Requires an array formula similar to the previous formula. When you enter this formula, use Ctrl+Shift+Enter:
=SUM((A2:A10="Jan")*(B2:B10<>"North")*C2:C10)

Count of Sales where Month="Jan" AND Region="North"


For multiple criteria in different fields, the COUNTIF function doesn't work. However, you can use an array formula. When you enter this formula, use Ctrl+Shift+Enter:
=SUM((A2:A10="Jan")*(B2:B10="North"))

Sum of Sales where Month="Jan" AND Sales>= 200


Requires an array formula similar to the previous example. When you enter this formula, use Ctrl+Shift+Enter:
=SUM((A2:A10="Jan")*(C2:C10>=200)*(C2:C10))

Sum of Sales between 300 and 400


This also requires an array formula. When you enter this formula, use Ctrl+Shift+Enter:
=SUM((C2:C10>=300)*(C2:C10<=400)*(C2:C10))

Count of Sales between 300 and 400


This also requires an array formula. When you enter this formula, use Ctrl+Shift+Enter:
=SUM((C2:C10>=300)*(C2:C10<=400))

Chart Trendline Formulas


When you add a trendline to a chart, Excel provides an option to display the trendline equation in the chart. This tip describes how to create formulas that generate the trendline coefficients. You can then use these formulas to calculate predicted y values for give values of x. These equations assume that your sheet has two named ranges: x and y.

Linear Trendline
Equation: y = m * x + b m: =SLOPE(y,x) b: =INTERCEPT(y,x)

Logarithmic Trendline
Equation: y = (c * LN(x)) + b c: =INDEX(LINEST(y,LN(x)),1) b: =INDEX(LINEST(y,LN(x)),1,2)

Power Trendline
Equation: y=c*x^b c: =EXP(INDEX(LINEST(LN(y),LN(x),,),1,2)) b: =INDEX(LINEST(LN(y),LN(x),,),1)

Exponential Trendline
Equation: y = c *e ^(b * x) c: =EXP(INDEX(LINEST(LN(y),x),1,2)) b: =INDEX(LINEST(LN(y),x),1)

2nd Order Polynomial Trendline


Equation: y = (c2 * x^2) + (c1 * x ^1) + b c2: =INDEX(LINEST(y,x^{1,2}),1) C1: =INDEX(LINEST(y,x^{1,2}),1,2)

b = =INDEX(LINEST(y,x^{1,2}),1,3)

3rd Order Polynomial Trendline


Equation: y = (c3 * x^3) + (c2 * x^2) + (c1 * x^1) + b c3: =INDEX(LINEST(y,x^{1,2,3}),1) c2: =INDEX(LINEST(y,x^{1,2,3}),1,2) C1: =INDEX(LINEST(y,x^{1,2,3}),1,3) b: =INDEX(LINEST(y,x^{1,2,3}),1,4)

Higher Order Polynomial Trendline


Notice the pattern in the two preceding sets of formulas.

Making An Exact Copy Of A Range Of Formulas


Assume that A1:D10 on Sheet1 has a range of cells that contain formulas. Furthermore, assume that you want to make an exact copy of these formulas, beginning in cell A11 on Sheet1. By "exact," I mean a perfect replica -- the original cell references should not change. If the formulas contain only absolute cell references, it's a piece of cake. Just use the standard copy/paste commands. But if the formulas contain relative or mixed references, the standard copy/paste technique won't work because the relative and mixed references will be adjusted when the range is pasted. If you're a VBA programmer, you can simply execute the following code:
With Sheets("Sheet1") .Range("A11:D20").Formula = .Range("A1:D10").Formula End With

Following are step-by-step instructions to accomplish this task without using VBA (contributed by Bob Umlas): 1. Select the source range (A1:D10 in this example). 2. Group the source sheet with another empty sheet (say Sheet2). To do this, press Ctrl while you click the sheet tab for Sheet2 3. Select Edit - Fill - Across worksheets (choose the All option in the dialog box). 4. Ungroup the sheets (click the sheet tab for Sheet2) 5. In Sheet2, the copied range will be selected. Choose Edit - Cut. 6. Activate cell A11 (in Sheet2) and press Enter to paste the cut cells. A11.D20 will be selected. 7. Re-group the sheets. Press Ctl and click the sheet tab for Sheet1 8. Once again, use Edit - Fill - Across worksheets. 9. Activate Sheet1, and you'll find that A11:D20 contains an exact replica of the formulas in A1:D10. Note: For another method of performing this task, see Making An Exact Copy Of A Range Of Formulas, Take 2.

Comparing Two Lists With Conditional Formatting


Category: Formatting / Formulas | [Item URL]

Excel's Conditional Formatting feature has many uses. Suppose you need to compare two lists, and identify the items that are different. The figure below shows an example. These lists happen to contain text, but this technique also works with numeric data.

The first list is in A2:B19, and this range is named OldList. The second list is in D2:E19, and the range is named NewList. The ranges were named using the Insert - Name - Define command. Naming the ranges is not necessary, but it makes them easier to work with. As you can see, items in OldList that do not appear in NewList are highlighted with a yellow background. Items in NewList that do not appear in OldList are highlighted with a green background. These colors are the result of Conditional Formatting.

How to do it
1. 2. 3. 4. Start by selecting the OldList range. Choose Format - Conditional Formatting In the Conditional Formatting dialog box, use the drop-down list to choose Formula is. Enter this formula:
=COUNTIF(NewList,A2)=0

5. Click the Format button and specify the formatting to apply when the condition is true (a yellow background in this example). 6. Click OK

The cells in the NewList range will use a similar conditional formatting formula. 1. 2. 3. 4. Select the NewList range. Choose Format - Conditional Formatting In the Conditional Formatting dialog box, use the drop-down list to choose Formula is. Enter this formula:
=COUNTIF(OldList,D2)=0

5. Click the Format button and specify the formatting to apply when the condition is true (a green background in this example). 6. Click OK Both of these conditional formatting formulas use the COUNTIF function. This function counts the number of times a particular value appears in a range. If the formula returns 0, it means that the item does not appear in the range. Therefore, the conditional formatting kicks in and the cell's background color is changed. The cell reference in the COUNTIF function should always be the upper left cell of the selected range.

Locate Phantom Links In A Workbook


Category: Formulas | [Item URL]

Q. Whenever I open a particular Excel workbook, I get a message asking if I want to update the links. I've examined every formula in the workbook, and I am absolutely certain that the workbook contains no links to any other file. What can I do to convince Excel that the workbook has no links? You've encountered the infamous "phantom link" phenomenon. I've never known Excel to be wrong about identifying links, so there's an excellent chance that your workbook does contain one or more links -- but they are probably not formula links. Follow these steps to identify and eradicate any links in a workbook. 1. Select Edit, Links. In many cases, this command may not be available. If it is available, the Links dialog
box will tell you the name of the source file for the link. Click the Change Source button and change the link so it refers to the active file. 2. Select Insert, Name, Define. Scroll through the list of names in the Define Name dialog box and examine the Refers to box (see the figure below). If a name refers to another workbook or contains an erroneous reference such as #REF!, delete the name. This is, by far, the most common cause of phantom links 3. If you have a chart in your workbook, click on each data series in the chart and examine the SERIES formula displayed in the formula bar. If the SERIES formula refers to another workbook, you've identified

your link. To eliminate the link move or copy the chart's data into the current workbook and recreate your chart. 4. If your workbook contains any custom dialog sheets, select each object in each dialog sheet and examine the formula bar. If any object contains a reference to another workbook, edit or delete the reference.

Next, save your workbook and then re-open it. It should open up without asking you to update the links.

Dealing With Negative Time Values


Category: Formulas | [Item URL]

Because Excel stores dates and times as numeric values, it's possible to add or subtract one from the other. However, if you have a workbook containing only times (no dates), you may have discovered that subtracting one time from another doesn't always work. Negative time values appear as a series of hash marks (########), even though you've assigned the [h]:mm format to the cells. By default, Excel uses a date system that begins with January 1, 1900. A negative time value generates a date/time combination that falls before this date, which is invalid. The solution is to use the optional 1904 date system. Select Tools, Options, click the Calculation tab, and check the 1904 date system box to change the starting date to January 2, 1904. Your negative times will now be displayed correctly, as shown below. Be careful if you workbook contains links to other files that don't use the 1904 date system. In such a case, the mismatch of date systems could cause erroneous results.

Converting Non-numbers To Actual Values


Category: Formulas | [Item URL]

Q. I often import data into Excel from various applications, including Access. I've found that values are sometimes imported as text, which means I can't use them in calculations or with commands that require values. I've tried formatting the cells as values, with no success. The only way I've found to convert the text into values is to edit the cell and then press Enter. Is there an easier way to make these conversions?
This is a common problem in Excel. The good news is the Excel 2002 is able to identify such cells and you can easily correct them If you're using an older version of Excel, you can use this method: 1. Select any empty cell 2. Enter the value 1 into that cell 3. Choose Edit, Copy 4. Select all the cells that need to be converted 5. Choose Edit, Paste Special 6. In the Paste Special dialog box, select the Multiply option, then click OK. This operation multiplies each cell by 1, and in the process converts the cell's contents to a value.

Compare Ranges By Using An Array Formula


In Excel, you can compare the cells in two ranges with an array formula. For instance, to see if all of the values in A1:A100 are identical to those in B1:B100, type this array formula: =SUM(IF(A1:A100=B1:B100,0,1)) Note: This is an array formula and it must be entered using Ctrl-Shift-Enter. The formula will return the number of corresponding cells that are different. If the formula returns 0, it means that the two ranges are identical.

Calculate The Number Of Days In A Month


Excel lacks a function for calculating the number of days in a particular month, so you'll need to construct your own formula. If cell A1 contains a date, this formula will return the number of days in the month:
=DAY(DATE(YEAR(A1),MONTH(A1)+1,1)-1)

Identify Formulas By Using Conditional Formatting


How many times have you accidentally deleted or overwritten cells containing formulas only to discover the mistake after it's too late? One solution is to write-protect important cells. Another approach is to give those cells a visual flag. This clever technique was submitted by David Hager. It uses Conditional Formatting (available in Excel 97 or later) to apply special formatting to cells that contain formulas--something that's not normally possible. With this technique you can set up your worksheet so that all formula cells get a yellow background, for example, or so that negative values are in boldface. Follow these steps:

1. Select Insert, Name, Define.


2. In the Define Name dialog box, enter the following in the 'Names in workbook' box CellHasFormula 3. Then enter the following formula in the "Refers to" box =GET.CELL(48,INDIRECT("rc",FALSE))

4. Click Add, and then OK.


5. Select all the cells to which you want to apply the conditional formatting. 6. Select Format, Conditional Formatting 7. In the Conditional Formatting dialog box, select Formula Is from the drop-down list, and then enter this formula in the adjacent box (see the figure below): =CellHasFormula

8. Click the Format button and select the type of formatting you want for the cells that contain a formula.
9. Click OK. After you've completed these steps, every cell that contains a formula and is within the range you selected in Step 4 will display the formatting of your choice.

How does it work? The key component is creating a named formula in Steps 2 and 3. This formula, unlike standard formulas, doesn't reside in a cell, but it still acts like a formula by returning a value -- in this case either 'True' or 'False'. The formula uses the GET.CELL function, which is part of the XLM macro language (VBA's predecessor) and cannot be used directly in a worksheet. Using a value of 48 as the first argument for GET.CELL causes the function to return 'True' if the cell contains a formula. The INDIRECT function essentially creates a reference to each cell in the selected range.

Displaying Autofilter Criteria


Excel's AutoFilter feature definitely ranks right up there when it comes to handy tools. This feature, which you access with the Data, Filter, AutoFilter command, works with a range of cells set up as a database or list. When AutoFiltering is turned on, the row headers display drop-down arrows that let you specify criteria (such as "Age greater than 30"). Rows that don't match your criteria are hidden, but they are redisplayed when you turn off AutoFiltering. One problem with AutoFiltering is that you can't tell which criteria are in effect. Stephen Bullen developed a custom VBA worksheet function that displays the current AutoFilter criteria in a cell. The instructions that follow are for Excel 97 or later. Press Alt+F11 and insert a new module for the active workbook. Then enter the VBA code for the FilterCriteria shown below.
Function FilterCriteria(Rng As Range) As String 'By Stephen Bullen Dim Filter As String Filter = "" On Error GoTo Finish With Rng.Parent.AutoFilter If Intersect(Rng, .Range) Is Nothing Then GoTo Finish With .Filters(Rng.Column - .Range.Column + 1) If Not .On Then GoTo Finish Filter = .Criteria1

Select Case .Operator Case xlAnd Filter = Filter & " AND " & .Criteria2 Case xlOr Filter = Filter & " OR " & .Criteria2 End Select End With End With Finish: FilterCriteria = Filter End Function

After you've entered the VBA code, you can use the function in your formulas. The single-cell argument for the FilterCriteria function can refer to any cell within the column of interest. The formula will return the current AutoFilter criteria (if any) for the specified column. When you turn AutoFiltering off, the formulas don't display anything. The figure below shows the FilterCriteria in action. The function is used in the cells in row 1. For example, cell A1 contains this formula:
=FilterCriteria(A3)

As you can see, the list is currently filtered to show rows in which column A contains January, column C contains a code of A or B, and column D contains a value greater than 125 (column B is not filtered, so the formula in cell B1 displays nothing). The rows that don't match these criteria are hidden.

Calculating A Conditional Average


Category: Formulas | [Item URL]

In the real world, a simple average often isn't adequate for your needs. For example, an instructor might calculate student grades by averaging a series of test scores but omitting the two lowest scores. Or you might want to compute an average that ignores both the highest and lowest values. In cases such as these, the AVERAGE function won't do, so you must create a more complex formula. The following Excel formula computes the average of the values contained in a range named "scores," but excludes the highest and lowest values:
=(SUM(scores)-MIN(scores)-MAX(scores))/(COUNT(scores)-2)

Here's an example that calculates an average excluding the two lowest scores:
=(SUM(scores)-MIN(scores)-SMALL(scores,2))/(COUNT(scores)-2)

Display Text And A Value In One Cell


Did you know that you could combine text and values in a single cell? For example, assume cell A12 contains the value 1435. Enter the following formula into another cell: ="Total: "&A12 The formula cell will display: "Total: 1435." The ampersand is a concatenation operator that joins the text with the contents of cell A12. Applying a number format to the cell containing the formula has no effect, because the cell contains text, not a value. As a work-around, modify the formula to use the TEXT function (the second argument for the TEXT function consists of a standard Excel number-format string). ="Total: "&TEXT(A12,"$#,##0.00") This formula will display "Total: $1,435.00." Here's another example formula that uses the NOW function to display some text along with the current date and time: ="Report printed on "&TEXT(NOW(),"mmmm d, yyyy at h:mm AM/PM")

Automatic List Numbering


It's fairly easy to create a formula that generates consecutively number items in nonconsecutive cells. Refer to the figure below.

Column A consists of formulas that refer to column B. The formula in cell A1 is: =IF(B1<>"",COUNTA($B$1:B1)&".","") This formula, which is copied down to the other cells in column A, displays the next consecutive item number if the corresponding cell in column B is not empty. If the cell in column B is empty, the formula displays nothing. As items are added or deleted from column B, the numbering updates automatically.

Calculate The Day Of The Year And Days Remaining


If you've ever had to figure out which of the year's 365 days a particular date falls on, or how many days remain in the year, you've probably found that Excel lacks functions to perform the calculation. But you can create formulas to do the job. The formula below returns the day of the year for a date in cell A1: =A1-DATE(YEAR(A1),1,0) Note: Excel automatically formats the cell as a date, so change the number format to another option (like General). To calculate the number of days remaining in the year (assuming that the date is in cell A1), use the following formula: =DATE(YEAR(A1),12,31)-A1

Rounding To n Significant Digits


Excel includes three functions (ROUND, ROUNDUP, and ROUNDDOWN) that round values to a specified number of digits. In some cases, however, you may need to round a value to a specified number of significant digits. For example, you might want to express the value 1,432,187 in terms of two significant digits (that is, as 1,400,000). Here's an elegant solution. The formula below rounds the value in cell A1 to the number of significant digits specified in cell A2: =ROUND(A1,A2-1-INT(LOG10(ABS(A1))))

Working With Pre-1900 Dates


In the eyes of Excel, the world began on January 1, 1900. Excel is not capable of working with dates earlier than that. People who use Excel to store historical information often need to work with pre-1900 dates. The only way to create a date such as July 4, 1776, in Excel is to enter it into a cell and have the program interpret it as text. Unfortunately, you can't manipulate dates stored as text -- if you want to alter their formatting, for example, or if you need to calculate the day of the week they fell on. To address this problem, I created an add-in (for Excel 97 or later versions) called Extended Date Functions. With this add-in installed, you'll have access to eight new worksheet functions that let you work with dates in any year from 0100 through 9999. Note: Be careful if you plan to insert dates that occurred before 1752. Differences between the historical American, British, Gregorian, and Julian calendars can result in inaccurate computations.

Using Data Validation To Check For Repeated Values


Sometimes you just don't want data to repeat itself. On an order form or an inventory sheet, for instance, you may not want a part number entered in one cell to repeat in another cell. You can use Excel's Data Validation feature to to prevent a value from appearing more than once in a range. In the example below, the range A2:A20 requires unique part numbers. If the user enters a number that already exists, a message box pops up and asks for a different one.

To create this type of message box for your worksheet: 1. Select the cells for which you need to punch in unique entries (here, the correct range to select is A2:A20).

2. Choose Data, Validation and click the Settings tab.


3. Choose Custom from the Allow drop-down list. The Custom option requires a logical formula that will return either "True" or "False." This example requires a formula that will return "True" only if the content of the cell does not match one that already exists in the range. The COUNTIF function will do the job. Type the following formula into the Formula field: =COUNTIF($A$2:$A$20,A2)=1 This formula counts the number of cells in range A2:A20 that contain the same value that appears in cell A2. If the count is 1, the formula returns "True"; otherwise, it returns "False." Notice that the first argument for COUNTIF is an absolute reference to the entire validation range. In contrast, the second argument is a relative reference to the upper left cell in the validation range (it will be adjusted for each of the other cells in the range). 4. Next, to create the warning that appears in a pop-up message box when a duplicate value is entered into the selected range, click the Error Alert tab in the Data Validation dialog box. For the Style, select Stop (this option rejects existing values). Enter a title for the message box (such as Duplicate Data) and type your error message. 5. Click OK and try it out. You'll find that you can enter any data into the validation range, but if you type an entry that already exists, you'll get the warning message. Click Retry to edit the cell's contents or choose Cancel to clear the cell. While Data Validation is a useful feature, it contains a potentially serious design flaw. If you copy a cell and paste it to a cell that uses the feature, the Data Validation rules are wiped out. This problem also applies to cells that use

Conditional Formatting. You'll need to keep this in mind when you're cutting and pasting in mission-critical applications.

Sum The Largest Values In A Range


Q. I need to calculate the sum of the three largest values in a range of 100 cells. The range isnt sorted, so I cant use a SUM function. Do you have any suggestions about how I could handle this problem? Excels LARGE function returns the nth-largest value in a range, in which n is the functions second argument. You need a formula that calls the LARGE function three times and then sums the results. The following formula, which assumes the numbers are located in the range A1:A100, will do the job: =LARGE(A1:A100,1)+ LARGE(A1:A100,2)+ LARGE(A1:A100,3) Another approach is to use an array formula like this one: =SUM(LARGE(A1:A100,{1,2,3})) The formula first passes an array of three values to the LARGE function, and then uses the SUM function to add the values returned by the LARGE function. Notice that the values 1 through 3 are enclosed in brackets rather than parentheses. After typing an array formula, press Ctrl-Shift-Enter instead of Enter. Formulas of this type can become unwieldy as n gets larger. For example, to sum the top 30 values in a range, a formula must contain a list of integers from 1 to 30. Here is a more general version of the array formula: =SUM(LARGE(A1:A100,ROW(INDIRECT ("1:30")))) This formula uses the ROW function to generate a series of integers between 1 and 30, and uses this array as the second argument for the LARGE function. To sum a different quantity of numbers, just change the 30 to the desired number.

Count Autofiltered Rows


Q. When I use Excel's AutoFiltering, the status bar displays the number of qualifying rows. But for no apparent reason, that number often vanishes. How do I keep this number visible while I work? AutoFiltering a list hides rows that don't meet your filter criteria. After applying filtering criteria, Excel shows the record count on the status bar--but this value disappears when the sheet is calculated. To display a permanent count of the visible rows in an AutoFiltered list, create a formula using Excel's SUBTOTAL function. The first argument for SUBTOTAL specifies the type of operation (an argument of 2 displays a count of the visible cells in a range). The figure below shows a list in rows 6 through 3006. The formula in cell D3 is: =SUBTOTAL(2,A6:A3006) The formula counts the number of visible cells in the range (minus the header row). Apply different filtering criteria, and the formula updates to show the new count. The SUBTOTAL function only works for AutoFiltering and outlining. If you hide rows manually, it won't return the correct result.

Perform Two-Way Table Lookups


All spreadsheets support lookup functions, tools that return a value from a table by looking up another value in the table. An income tax table is a good example. You can write a formula that uses the VLOOKUP function to determine the tax rate for a given income amount. The lookup functions in Excel are only appropriate for one-way lookups, however. If you need to perform a two-way lookup, you'll need more than the standard functions. The figure below shows a simple example.

The formula in cell H4 looks up the entries in cells H2 and H3 and then returns the corresponding value from the table. The formula in H4 is: =INDEX(A1:E14, MATCH(H2,A1:A14,0), MATCH(H3,A1:E1,0)).

The formula uses the INDEX function, with three arguments. The first is the entire table range (A1:A14). The second uses the MATCH function to return the offset of the desired month in column A. The third argument uses the MATCH function to return the offset of the desired product in row 1. You may prefer to take advantage of Excel's natural-language formulas. For example, enter the following formula to return Sprocket sales for June: =June Sprockets If natural-language formulas aren't working, select Tools, Options, click the Calculation tab, and place a check mark next to "Accept labels in formulas." Be aware that using natural language formulas is not 100% reliable!

Referencing A Sheet Indirectly


Q. My Excel workbook has a sheet for each month, named January, February, and so on. I also have a summary sheet that displays key calculations for a particular month. For example, one of my formulas is: =SUM(February!F1:F10) Is there any way that I can enter the month name into a cell on my summary sheet, and then have my formulas use the data for the specified sheet? Yes. Excel's INDIRECT function was designed specifically for this sort of thing. This function accepts a text string as an argument, and then evaluates the text string to arrive at a cell or range reference. In your case, assume that cell B1 on your summary worksheet holds the month name. The following formula utilizes the INDIRECT function to create the range reference used by the SUM function: =SUM(INDIRECT(B1&"!F1:F10")) Note that I use the ampersand operator to join the month name with the cell reference (expressed as text). Refer to the figure below. If cell B1 contains the text March, the SUM function returns the sum of the range March! F1:F10.

Delete All Input Cells, But Keep The Formulas


Your worksheet may be set up with formulas that operate on a number of input cells. Here's an easy way to clear all input values while keeping the formulas intact.

Press F5 to display the Go To dialog box, and click the Special button. In the Go To Special dialog, choose the Constants button and select Numbers. When you click OK, the nonformula numeric cells will be selected. Press Delete to delete the values. The Go To Special dialog box has many other options for selecting cells of a particular type.

Round Values To The Nearest Fraction


Q. Is it possible to round a dollar amount to the nearest 25 cents? For example, if a number appears as $1.65, I would like to convert it to $1.75. Excel's ROUND() function seems to work only with whole numbers. Yes, you can use Excel's ROUND() function to achieve the rounding you want. The following formula, which assumes that your value is in cell A1, will do the job for you. =ROUND(A1/.25,0)*.25 The formula divides the original value by .25 and then multiplies the result by .25. You can, of course, use a similar formula to round values to other fractions. For example, to round a dollar amount to the nearest nickel, simply substitute .05 for each of the two occurrences of ".25" in the preceding formula.

Avoid Error Displays In Formulas


Sometimes a formula may return an error message. Usually, you'll want to know when a formula error occurs. But now and then you may prefer to avoid the messages. You can do so by using an IF() function to check for an error. For example, the formula below displays a blank if the division results in an error. =IF(ISERROR(A1/B1),"",A1/B1) You can adapt this technique to any operation. The original formula serves as the argument for the ISERROR() function, and it repeats as the last argument of the IF() function. Like this: =IF(ISERROR(OriginalFormula),"",OriginalFormula)

Change Cell Values Using Paste Special


Q. I have a price list stored in a worksheet, and I need to increase all prices by 5 percent. Can I do this without reentering all the prices? Excel provides two ways to accomplish this. The "traditional" technique goes something like this: 1. 2. 3. 4. Insert or find a blank column near the prices. In that column's first cell, enter a formula to multiply the price in that row by 1.05. Copy the formula down the column. Select and copy the entire column of formulas 5. Select the original prices, and choose Edit, Paste Special. 6. In the Paste Special dialog box, select Values to overwrite the original prices with the formulas' results. 7. And finally, delete the column of formulas.

The other, more efficient approach also uses the Paste Special dialog box. To increase a range of values (prices, in this example) by 5 percent: 1. Enter 1.05 into any blank cell. 2. Select the cell and choose Edit, Copy. 3. Select the range of values and choose Edit, Paste Special. 4. Choose the Multiply option and click OK. 5. Delete the cell that contains the 1.05.

Hiding Your Formulas


Q. I've created some clever formulas, and I don't want anyone else to see them. Is it possible to hide the formulas but display the results? Every cell has two key properties: locked and hidden. A locked cell can't be changed, and the contents of a hidden cell don't appear in the formula bar when the cell is selected. By default, every cell is locked and not hidden. But it's important to remember that these attributes have no effect unless the worksheet itself is protected. First, to change the attributes, select the appropriate cell or range and then choose Format, Cells. In the Format Cells dialog box, click the Protection tab and select Locked or Hidden (or both). Unlock cells that accept user input, and lock formula and other cells that should stay unchanged (such as titles). To prevent others from seeing your formulas, lock and hide the formula cells: The results of the formulas will be visible, but the formulas will not. Now, to protect the worksheet, choose Tools, Protection, Protect Sheet to bring up the Protect Sheet dialog box. Make sure the Contents box is checked. You can enter a password to prevent others from unprotecting the sheet. Locked cells in a protected sheet cannot be edited, and other worksheet changes are disabled. For example, no one can insert rows or columns, change column width, or create embedded charts. NOTE: Keep in mind that it is very easy to break the password for a protected sheet. If you are looking for real security, this is not the solution.

Counting Distinct Entries In A Range


Q. Can I write a formula that returns the number of distinct entries in a range? First, let's clarify the question. We're hunting for a formula that, given the range that contains the values 100, 99, 98, 100, 98, 100, 98, would return 3. In other words, this range contains three different values, some of them repeated. This type of counting requires an array formula. The formula below, for example, counts the number of distinct entries in the range A1:D100. =SUM(1/COUNTIF(A1:D100, A1:D100))

When you enter this formula, you must press Ctrl-Shift-Enter. Pressing only Enter will give you the wrong result. Excel will place brackets around the formula to remind you that you've created an array formula.
The preceding formula works fine in many cases, but it will return an error if the range contains any blank cells. The formula below (also an array formula, so input it with Ctrl-Shift-Enter) is more complex, but it will handle a range that contains a blank cell.

=SUM(IF(COUNTIF(A1:D100,A1:D100)=0, "", 1/COUNTIF(A1:D100,A1:D100)))

Force A Global Recalculation


Q. I find that sometimes my formulas do not get fully calculated. This often happens when I use custom functions created with VBA. Microsoft has acknowledged some problems with the Excel calculation engine in some version of Excel. In order to be assured that all of your formulas have been calculated, press Ctrl-Alt-F9 to force a complete recalculation. This key combination will also update formulas that use custom VBA functions.

Summing Times That Exceed 24 Hours


Q. I have a range of time values, but when I try to sum them, the total is never greater than 24 hours. When you add a range that contains time values, Excel ignores the hours that exceed 24. The solution is to use a custom number format. 1. Activate the cell that contains your total time 2. Choose Format, Cells. 3. In the Format Cells dialog box, click the Number tab. 4. Choose Custom from the Category list 5. Type [h]:mm into the box labeled Type. Using brackets around the hour portion of the format string tells Excel to display hours that exceed 24 hours.

Transforming Data With Formulas


This tip describes a technique that should be in the arsenal of every Excel user. It describes how to use formulas to transform data. The figure below shows a simple example. The text in column A consists of lower case letters. The goal is to transform these cells so they display "proper" case. This will be done by creating formulas that use Excel's PROPER function.

The steps below are specific to this example. But they can easily be adapted to other types of data transformations.

Creating the formulas


In this case, the formulas will go in column D. As you'll see, this is just a temporary location. The formula results will eventually replace the names in column A. 1. Enter the following formula in cell D2: =PROPER(A2) 2. Copy the formula down the column to accommodate the data. In this case, the formula is copied down to cell D11. The worksheet now looks like this (the formula cells are selected, so they appear highlighted).

Copying and pasting the formula cells


In this step, the formula cells are copied, and pasted as values -- overwriting the original data in column A. 1. Select the formula cells. In this case, D2:D11.

2. Choose Edit - Copy 3. Select the first cell in the original data column (in this case, cell A2). 4. Choose Edit - Paste Special. This displays the Paste Special dialog box. 5. In the Paste Special dialog box, click the Value option button. This step is critical. It pastes the results of the formulas -- not the formulas. 6. Click OK. At this point, the worksheet looks like this:

Deleting the temporary formulas


The formulas in column D are no longer necessary, so you can delete them.

Creating A Megaformula
This tip describes how to create what I call a "megaformula" -- a single formula that does the work of several intermediate formulas.

An Example
The goal is to create a formula that returns the string of characters following the final occurrence of a specified character. For example, consider the text string below (which happens to be a URL): http://spreadsheetpage.com/index.php/tips Excel does not provide a straightforward way to extract the characters following the final slash character (i.e., "tips") from this string. It is possible, however, do do so by using a number of intermediate formulas. The figure below shows a multi-formula solution. The original text is in cell A1. Formulas in A2:A6 are used to produce the desired result. The formulas are displayed in column B.

Following is a description of the intermediate formulas (which will eventually be combined into a single formula).

1. Count the number of slash characters (Cell A2)


The formula in cell A2 returns the number of slash characters in cell A1. Excel doesn't provide a direct way to count specific characters in a cell, so this formula is relatively complex. 2. Replace the last slash character with an arbitrary character (Cell A3) The formula in A3 uses the SUBSTITUTE function to replace the last slash character (calculated in A2) with a new character. I chose CHAR(1) because there is little chance of this character actually appearing in the original text string. 3. Get the position of the new character (Cell A4) The formula in A4 uses the FIND function to determine the position of the new character. 4. Count the number of characters after the new character (Cell A5) The formula in A5 subtracts the position of the new character from the length of the original string. The result is the number of characters after the new character. 5. Get the text after the new character (Cell A6) The formula in A6 uses the RIGHT function to extract the characters -- the end result.

Combining the Five Formulas Into One


Next, these five formulas will be combined into a single formula. 1. Activate the cell that displays the final result (in this case, cell A6). Notice that it contains a reference to cell A5. 2. Activate cell A5. Press F2 and select the formula text (but omit the initial equal sign), and press Ctrl+C to copy the text. Press Esc. 3. Re-activate cell A6 and paste the copied text to replace the reference to cell A5. The formula in A6 is now: =RIGHT(A1,LEN(A1)-A4) 4. The formula contains a reference to cell A4, so activate A4 and copy the formula as text. Then replace the reference to cell A4 with the copied formula text. The formula now looks like this: RIGHT(A1,LEN(A1)-FIND(CHAR(1),A3)) 5. Replace the reference to cell A3 with the formula text from cell A3. The formula now looks like this: =RIGHT(A1,LEN(A1)-FIND(CHAR(1),SUBSTITUTE(A1,"/",CHAR(1),A2))) 6. Replace the reference to cell A2 with the formula text from cell A2. The formula now looks like this:

=RIGHT(A1,LEN(A1)-FIND(CHAR(1),SUBSTITUTE(A1,"/",CHAR(1),LEN(A1)LEN(SUBSTITUTE(A1,"/",""))))) The formula now refers only to cell A1, and the intermediate formula are no longer necessary. This single formula does the work of five other formulas. This general technique can be applied to other situations in which a final result uses several intermediate formulas. NOTE: You may think that using such a complex formula would cause the worksheet to calculate more slowly. In fact, you may find just the opposite: Using a single formula in place of multiple formulas may speed up recalculation. Any calculation speed differences, however, will probably not be noticeable unless you have thousands of copies of the formula.

Alternatives To Nested IF Functions


Excel's IF function provides some simple decision-making capability to a worksheet. The IF function accepts three arguments:
The condition being evaluated (should result in either TRUE or FALSE) The value to display if the condition is TRUE The value to display if the condition is FALSE

The formula below, for example, returns 1 if cell A1 contains "A". If cell A1 does not contain "A", the formula returns an empty string. =IF(A1="A",1,"") For more decision-making power, you can "nest" IF functions within a formula. In other words, you can use an IF function as the second argument for an IF function. Here's an example: =IF(A1="A",1,IF(A1="B",2,IF(A1="C",3,""))) This formula checks cell A1. If it contains "A", the formula returns 1. If it doesn't contain "A", then the second argument is evaluated. The second argument contains another IF function that determines if A1 contains a "B". If so, the formula returns 2; if not, the formula evaluates the IF function contained in the second argument and checks to see if A1 contains "C". If so, it returns 3; otherwise, it returns an empty string. Excel allows up to seven levels of nested IF functions. The formula below works correctly, but Excel will not allow you to nest the IF functions any deeper than this. =IF(A1="A",1,IF(A1="B",2,IF(A1="C",3,IF(A1="D",4, IF(A1="E",5,IF(A1="F",6,IF(A1="G",7,IF(A1="H",8,"")))))))) The sections that follow present various ways to get around the limit of seven nested IF functions. Be aware that these techniques may not be appropriate for all situations.
Note:

Excel 2007 and later allows up to 64 nesting levels

Using a VLOOKUP formula

In many cases, you can avoid using IF functions and use a VLOOKUP function. This will require a separate table in your worksheet. In the figure below, the lookup table is in B1:C10. The formula in A2 is: =VLOOKUP(A1,B1:C10,2)

Using the CHOOSE function


In some cases, you can use the CHOOSE function. The first argument is an integer, and the value determines which of the subsequent arguments is evaluated and returned.

Using defined names


Another way to overcome the nested IF function limit is to use named formulas. Chip Pearson describes this technique at his web site, so I won't repeat it here.

Using the CONCATENATE function


Yet another option was suggested to me by B. Ganesh: Use the CONCATENATE function. In this case, each argument for CONCATENATE consists of an IF function. Here's an example: =CONCATENATE(IF(A1="A",1,""),IF(A1="B",2,""),IF(A1="C",3,""), IF(A1="D",4,""),IF(A1="E",5,""),IF(A1="F",6,""),IF(A1="G",7,""), IF(A1="H",8,""),IF(A1="I",9,""),IF(A1="J",10,"")) The CONCATENATE function can handle as many as 30 arguments -- which equates to testing 30 different conditions. And, as Alan Williams pointed out, you can avoid using the CONCATENATE function and use the concatenation operator (&): =IF(A1="A",1,"")&IF(A1="B",2,"")&IF(A1="C",3,"") &IF(A1="D",4,"")&IF(A1="E",5,"")&IF(A1="F",6,"") &IF(A1="G",7,"")&IF(A1="H",8,"")&IF(A1="I",9,"") &IF(A1="J",10,"") This method is not limited to 30 comparisons.

Use Boolean multiplication

Another alternative, suggest by Daniel Filer is to use Boolean multiplication. This technique takes advantage of the fact that, when multiplying, TRUE is treated as 1 and FALSE is treated as 0. Here's an example: =(A1="A")*1+(A1="B")*2+(A1="C")*3+(A1="D")*4+(A1="E")*5 +(A1="F")*6+(A1="G")*7+(A1="H")*8+(A1="I")*9+(A1="J")*10

Creating a custom VBA function


The final alternative is to create a custom worksheet function, using VBA. The advantage is that you can customize the function to meet your requirements, and your formulas can be simplified quite a bit.

A Formula To Calculate A Ratio


Excel provides no direct way to display the ratio between two values. For example, assume cell A1 contains 3, and cell B1 contains 24. The ratio between these two values is 1:8. Following is a formula, contributed by Douglas J. Roach, that displays the ratio between the values in cells A1 and B1:
=(LEFT(TEXT(A1/B1,"####/####"),FIND("/",TEXT(A1/B1,"####/####"))-1)&":" &RIGHT(TEXT(A1/B1,"####/####"),LEN(TEXT(A1/B1,"####/####")) -FIND("/",TEXT(A1/B1,"####/####"))))

The formula automatically reduces the "fraction" to the simplest form, and it allows up to four characters on either side of the colon. Jerry Meng pointed out a much simpler formula that produces the same result, but does not have the four-character limit:
=A1/GCD(A1,B1)&":"&B1/GCD(A1,B1)

Jerry's formula uses the GCD function, which is available only when the Analysis Toolpak Add-In is installed. Note: Be aware that the result of these formulas is a text string, not a fractional value. For example, the ratio of 1:8 is not the same as 1/8.

Multiple Condition Tests


One of the most basic functions in any spreadsheet is to return an answer based upon some condition. This becomes especially useful when counting or summing based upon that condition. One condition is useful, and is easily achieved using COUNTIF or SUMIF. These are incredibly useful and flexible functions, but limited as they are to single conditions, they can be lacking. Multiple conditions, such as counting the number of items sold by part number AND by month, greatly extends the functionality of our solution. There are a number of ways that this can be achieved within Excel, but this paper is focusing on one particular function, the SUMPRODUCT function, which by creative use has evolved to a flexibility undreamt of by its originators in Microsoft. Because this usage has been driven outside of Microsoft, by real-world Excel users, you will not see it documented within Excel help, or in MSDN.
SUMPRODUCT is one of the most versatile functions provided in Excel. In its most basic form, SUMPRODUCT multiplies corresponding members in given arrays, and returns the sum of those products. This page discusses the classic use of SUMPRODUCT, how creativity and inbuilt flexibility

has enabled it to evolve into a far more useful function, and explains some of the techniques being deployed. This article comes in two parts. This first part discusses SUMPRODUCT, how it has evolved, how it works, whilst Part 2 provides a number of real world problems and the solutions,

Standard Use of SUMPRODUCT


In it's classic form, SUMPRODUCT multiplies each value in one array by the corresponding value in another array, and returns the summed result. As an example, if cells A9:A11 contain the values 1,2,3 and B9:B11 contain 10,20,30, then
=SUMPRODUCT(A9:A11,B9:B11)

returns 140, or (1*10)+(2*20)+(3*30)=10+40+90=140. This is a useful function, but nothing more than that. A further, more 'creative' use of SUMPRODUCT has evolved, and is still evolving, driven as far as I can see mainly by the regular contributors of the Microsoft Excel newsgroups. This has been a creative and productive process

that has significantly increased the useability of SUMPRODUCT, but in a way that you will not find documented in Excel's Help.

Evolving Use of SUMPRODUCT


Within Excel, there are two very useful functions that support conditional counting and summing, namely COUNTIF and SUMIF. Very useful functions, but limited in that they can only evaluate a single test range. In certain instances, a very simple double conditional test between two values can be emulated by testing for the lower condition and subtracting anything that is beyond the upper condition. For instance, the formula =COUNTIF(A1:A10,>=10) COUNTIF(A1:A10,>20) calculates how many items in A1:A10 that fall between 10 and 20, by counting all items greater than 10, which also includes those items greater than 20, and then subtracting the count of those items in A1:A10 that are greater than 20. Whilst this emulates a double conditional test, it is very limited, it cannot work on different ranges, or more conditions. Multiple conditions are so useful to test ranges (say between two dates), and double tests (one array = A and another = B), and whilst this can be managed using array functions =SUM(IF(test_A,IF(test_B, etc., this is somewhat unwieldy, and is an array formula. And there is a better way, using SUMPRODUCT. Note that in this section, all formulae given are using the '*' (multiply) operator format, but this in itself is one of the biggest discussion points around the SUMPRODUCT function, one which is discussed below. To understand how SUMPRODUCT can be used, first consider the following data. A 1 2 3 4 5 6 7 8 9 10 Make Ford Ford Ford Ford Renault Renault BMW BMW BMW B Month June June May June June June June May June Table 1. We can easily count the number of Fords with
=COUNTIF(A1:A10,"Ford")

C Price 7,500 8,300 6,873 11,200 13,200 14,999 17,500 23,500 18,000

which returns 4.

Similalrly, it is straight-forward to get the value of Fords sold, using


=SUMIF(A1:A10,"Ford",C1:C10)

which gives 33,873.

But supposing that we want a count of how many Fords are sold in June, or the value of them? The number can be calculated with
=SUM(IF(A1:A10="Ford",IF(B1:B10="June",1,0),0))

which is an array formula so is committed with Ctrl-Shift-Enter, not just Enter. Similarly, the value is obtained with
=SUM(IF(A1:A10="Ford",IF(B1:B10="June",C1:C10,0),0))

which is also an array formula. But as this page is about SUMPRODUCT, you would expect that we could use that function in this case, and we can. The solution for the number of Fords sold in June using this function is
=SUMPRODUCT((A1:A10="Ford")*(B1:B10="June")).

The value is obtained with


=SUMPRODUCT((A1:A10="Ford")*(B1:B10="June")*(C1:C10))
The * is being used as the AND operator, the formula is saying, where A1:A10 = Ford AND B1:B10 = June, and where A1:A10 = Ford AND B1:B10= June, multiplied by C1:C10. In my view, this formula more readily shows what the author's objective is, and of course, as it is not an array formula it is simply committed with Enter. We can see that the * is equivalent to AND in the formula, how this works is explained later, but supposing we want an OR condition. As a further extension of its use, we use the '+' (plus) operator to count OR conditions, such as how many cars sold were either Fords or Renaults. The formula for this is

=SUMPRODUCT((A1:A10="Ford")+(A1:A10="Renault"))
which returns the result 6 as expected[1]. So far, so good, in that we have a versatile function that can do any number of conditional tests, and has an inbuilt flexibility that provides extensibility. Its power is augmented when combined with other functions, such as can be found in the examples page[2].

Advantages of SUMPRODUCT
Multiple conditional tests are a major advantage of the SUMPRODUCT function as descibed above, but it has two other considerable advantages. The first is that it can function with closed workbooks, and the second is that the handling of text values can be tailored to the requirement. In the case of another workbook, the SUMIF function can be used to calculate a value, such as in
=SUMIF('[Nowfal Rates.xls]RATES'!$K$11:$K$13,"gt;1")

This is fine in itself, and the value remains if the other workbook is closed, but as soon as the sheet is re-calculated, the formula returns #VALUE. Similarly, if the formula is entered with the other workbook already closed, a #VALUE is immediately returned.
SUMPRODUCT, however, overcomes this problem. The formula =SUMPRODUCT(--('[Nowfal Rates.xls]RATES'!$K$11:$K$13>1),--('[Nowfal Rates.xls]RATES'! $K$11:$K$13))

returns the same result, but it will still work when the other workbook is closed and the sheet is recalculated, and can be initially entered referencing the closed workbook, without a #VALUE error. The second major advantage is being able to handle text in numeric columns differently. Consider the follwoing dataset, as shown in Table 2. A 1 2 3 4 Item x y x Table 2. If we are looking at rows 1:4. we can see that we have a text value in B1 In this case it is simply a heading row, but the principle applies to a text value in any row. Using SUMPRODUCT, we can either return an error, or ignore the text. This can be useful if we want to ignore errors, or if we want to trap the error (and presumably correct it later). Errors will be returned if we use this version
=SUMPRODUCT((A1:A4="x")*(B1:B4))

B Number 1 2 3

To ignore errors, use this amended version which uses the double unary operator (see SUMPRODUCT Explained below for details)
=SUMPRODUCT(--(A1:A4="x"),(B1:B4))

And a third, most significant advantage, is that the conditional test range or the condition can be constructed in a huge number of ways to facilitate the requirement, such as
LEFT(A1:A10), ISNUMBER(MATCH(A1:A10,{"apples","pears"},0),or ISNUMBER(MATCH(K2:K30,ROW(INDIRECT(TODAY()&":"&TODAY()+10)),0))

But how does it work?

SUMPRODUCT Explained
Understanding how SUMPRODUCT works helps to determine where to use it, how to can construct thus formula, and thus how it can be extended. Table 3. below shows an example data set that we will use. A 9 10 11 12 13 14 `5 Ford Vauxhall Ford Ford Ford Ford Ford B B C A A D A A C 3 4 2 1 4 3 2

16 17 18 19 20

Renault Ford Ford Ford Ford

A A A A A

8 6 8 7 6

Table 3. In this example, the problem is to find how many Fords with a category of "A" were sold. A9:A20 holds the make, B9:B20 has the category, and C9:C20 has the number sold. The formula to get this result is =SUMPRODUCT((A9:A20="Ford")*(B9:B20="A")*(C9:C20)). The first part of the formula (A9:A20="Ford") checks the array of makes for a value of Ford. This returns an array of TRUE/FALSE, in this case it is {TRUE,FALSE,TRUE,TRUE,TRUE,TRUE,TRUE,FALSE,TRUE,TRUE,TRUE,TRUE} Similarly, the categories are checked for the vale A with (B9:B20="A"). Again, this returns an array of TRUE/FALSE, or {FALSE,FALSE,TRUE,TRUE,FALSE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE} And finally, the numbers are not checked but taken as is, that is (C9:C20), which returns an array of numbers {3,4,2,1,4,3,2,8,6,8,7,6} So now we have three arrays, two of TRUE/FALSE values, one of numbers. This is showm in Table 4. A 9 11 12 13 14 15 17 18 19 20 TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE B * FALSE * * * * * * * * * TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE * * * * * * * * * C 3 4 2 1 4 3 2 8 6 8 7 6

10 FALSE * FALSE *

* FALSE *

16 FALSE *

Table 4. And this is where it gets interesting.


SUMPRODUCT usually works on arrays of numbers, but we have arrays of TRUE/FALSE values as

well as an array of numbers. By using the '*' (multiply) operator, we can get numeric values that can be summed. '*' has the effect of coercing these two arrays into a single array of 1/0 values. Multiplying TRUE by TRUE returns 1 (try it, enter =TRUE*TRUE in a cell and see the result), any

other combination returns 0. Therefore, when both conditions are satisfied, we get a 1, whereas if any or both conditions are not satisfied, we get a 0. Multiplying the first array of TRUE/FALSE values by the second array of TRUE/FALSE values returns a composite array of 1/0 values, or {0,0,1,1,0,1,1,0,1,1,1,1}. This subsequent array of 1/0 values is then multiplied by the array of numbers sold to give a further array, an array of numbers sold that satisfy the two test conditions. SUMPRODUCT then sums the members of this array to give the count. Table 4. shows the values that the conditional tests break down to before being acted upon by the '*' operator. Table 5. shows a virtual representation of those TRUE/FALSE values as their numerical equivalents of 1/0 and the individual multiplication results. From this, you should be able to see how SUMPRODUCT arrives at its result, namely 35. A 9 10 11 12 13 14 15 16 17 18 19 20 1 0 1 1 1 1 1 0 1 1 1 1 * * * * * * * * * * * * B 0 0 1 1 0 1 1 1 1 1 1 1 Table 5. Table 6. shows you the same virtual representation of 1/0 numerical values without the numbers sold column, that is using SUMPRODUCT to count the number of rows satisfying the two conditions, or
=SUMPRODUCT((A9:A20=A1)*(B9:B20="A"))

C * * * * * * * * * * * * 3 4 2 1 4 3 2 8 6 8 7 6 0 0 2 1 0 3 2 0 6 8 7 6 35

A 9 10 11 12 13 14 15 16 17 1 0 1 1 1 1 1 0 1 * * * * * * * * *

B 0 0 1 1 0 1 1 1 1 = 0 = 0 = 1 = 1 = 0 = 1 = 1 = 0 = 1

18 19 20

1 1 1

* * * Table 6.

1 1 1

= 1 = 1 = 1 8

If you have been able to follow this explanation all of the way through, it may have occurred to you that although we are using the SUMPRODUCT function, the '*' operators have resolved the multiple arrays into a single composite array, leaving SUMPRODUCT to simply sum the members of that composite array, that is, there is no product. This is perfectly correct, and perfectly valid, SUMPRODUCT can work on a single array (put 1,2,3 in cells A1,A2,A3, and insert =SUMPRODUCT(A1:A3) in a cell, it returns 6 correctly). In reality, we only need the '*' to coerce the arrays that are being tested for a particular condition, we do not need it for the array that is not subject to a conditional test. So we could also use =SUMPRODUCT((A9:A20="Ford")*(B9:B20="A"),(C9:C20)), which does use the product aspect (see more on this in the next section). When using the SUMPRODUCT function, all arrays must be the same size, as corresponding members of each array are multiplied by each other. When using the SUMPRODUCT function, no array can be a whole column (A:A), the array must be for a range within a column (although the best part of a column could be defined with A1:A65535 if so desired). Whole rows (1:1) are acceptable[3]. In a SUMPRODUCT function, the arrays being evaluated cannot be a mix of column and row ranges, they must all be columns, or all rows. However, the row data can be transposed to present it to SUMPRODUCT as columnar - see the Using TRANSPOSE to test against values in a column not row example.

Format of SUMPRODUCT
In the examples presented so far, the format has been
=SUMPRODUCT((array1=condition1)*(array2=condition2)*(array3))

As mentioned above, we could also use

=SUMPRODUCT((array1=condition1)*(array2=condition2),(array3))

which works as the '*' operator is only required to coerce the conditional arrays that resolve to TRUE/FALSE into numeric values. As it the use of a arithmetic operator that coreces the TRUE/FALSE values to 1/0, we could use many different operators and achieve the same result. Thus, it is also possible to coerce each of the conditional arrays individually by multiplying them by 1,
=SUMPRODUCT((array1=condition1)*1,(array2=condition2)*1,(array3))

or

=SUMPRODUCT(1*(array1=condition1),1*(array2=condition2),(array3))

or by raising to the power of 1,

=SUMPRODUCT((array1=condition1)^1,(array2=condition2)^1,(array3))

or by adding 0, or

=SUMPRODUCT((array1=condition1)+0,(array2=condition2)+0,(array3)) =SUMPRODUCT(0+(array1=condition1),0+(array2=condition2),(array3))

or even by using the N function,

=SUMPRODUCT(N(array1=condition1),N(array2=condition2),(array3))

These methods differ from the '*' operator in that they are applied to individual arrays, '*' operates on two arrays. All of these methods work, when there is more than one conditional array, so it is really a matter of preference as to which to use. If there is a single conditional array, then the '*' operator cannot be used (there are not two to multiply), so one of the other above methods has to be used. Yet another method is to use the double unary operator, --, in this way
=SUMPRODUCT(--(array1=condition1),--(array2=condition2),(array3))

The double unary operator also coerces the indivual array(s), which then acts more akin to classic SUMPRODUCT. There has been much discussion that one way is faster than another, or is more of a 'standard' than another, but in reality there will be few instances where one method will gain a noticeable performance advantage over another, and as for standards, this is all new territory, and will mainly be used by people who have never been involved in using these standards, and who care even less. For me, I believe it is a matter of preference. Personally, I am being swayed to the double unary -notation, because it avoids a function call, it works in all situations (the '*' operator won't work on a single array), and I don't like the '1*', '*1', '^1', or '+0' variations. So my preference is for
=SUMPRODUCT(--(array1=condition1),--(array2=condition2),(array3)) which also has more similarity to classic SUMPRODUCT,

There is one other varitaion which has been promoted recently, which is the single unary operator, '-', such as
=SUMPRODUCT(-(array1=condition1),-(array2=condition2),(array3))

but I would not encourage this as it has no real merit that I can see, and has to be paired off, otherwise it will return a negative result. So, to sum up ... Tests, like A=10 normally resolve to TRUE or FALSE, and any operator is only needed if you want to coerce an array of TRUE/FALSE values to 1/0 integers, such as
=SUMPRODUCT(--(B5:B1953=101))

SUMPRODUCT arrays are normally separated by the comma. So, to preserve this format, if you have multiple conditions, you can use the -- on both conditions like so
=SUMPRODUCT(--(B5:B1953=101),--(C5:C1953=7))

But, if you simply multiply two arrays of TRUE/FALSE, that implicitly resolves to 1/0 values that are then summed, you don;t need comma, so you could then use
=SUMPRODUCT((B5:B193=101)*(C5:C193=7))

Any further, final, array of values can use the same operator, or could revert to comma. So your formula can be written as
=SUMPRODUCT(--(B5:B1953=101),--(C5:C1953=7),(D5:D1953))

or or or

=SUMPRODUCT((B5:B1953=101)*(C5:C1953=7),(D5:D1953)) =SUMPRODUCT(--(B5:B1953=101),--(C5:C1953=7),--(D5:D1953)) =SUMPRODUCT((B5:B1953=101)*(C5:C1953=7)*(D5:D1953))

or

=SUMPRODUCT(--(B5:B1953=101),--(C5:C1953=7)*(D5:D1953))

If the result is the product of two conditions being multiplied, it is fine to multiply them together as this will coerce the True/False values to 1/0 values to allow the summing
=SUMPRODUCT((condition1)*(condition2))

However, if there is only one condition, you can coerce to 1/0 with the double unary -=SUMPRODUCT(--(condition1))

You could achieve this equally as well with


=SUMPRODUCT((1*(condition1)))

and equally the first could be represented as


=SUMPRODUCT(--(condition1),--(condition2))

There is no situation that I know of whereby a solution using -- could not be achieved somehow with a '*'. Conversely, if using the TRANSPOSE function within SUMPRODUCT, then the '*' has to be used. So, as you can see there are a number of possibilities, and you make your own choice. I leave the final word to Harlan Grove, who once wrote this paragraph on why he prefers the double unary operator ... ....as I've written before, it's not the speed of double unary minuses I like, it's the fact that due to Excel's operator precedence it's harder to screw up double unary minuses with typos than it is to screw up the alternatives ^1, *1, +0. Also, since I read left to right, I prefer my number type coercions on the left rather than the right of my Boolean expressions, and -- looks nicer than 1* or 0+. Wrapping Boolean expressions inside N() is another alternative, possibly clearer, but it eats a nested function call level, so I don't use it.

Conditional Counting and Summing in VBA


All of the discussion so far has been about conditional formulae, that is directly within Excel worksheets. It is often necessary to count or sum conditionally some worksheet ranges within a VBA routine. In these instances, we could code a simple loop to go through all of the data and check if it matches the condition, summing the matching items as we go. Excel VBA has a method that allows a call out from VBA routines to a built-in worksheet function, saving ourselves having to build that functionality, and greatly improving the power of our VBA code. Whilst there is an overhead to calling an Excel function from within VBA, any performance impact should be minimal if not over-used, and the usefulness of this facility is clear. We can utilise this facility to achieve conditional counting and summing in VBA with little effort, but there are a few things to be aware of. As an example, consider the data in Table 1. above. If we needed to know how many Fords were in the range A1:A10from within a VBA procedure, we could simply use the following code Dim mModel As String Dim mCount As Long mModel = "Ford" mCount = Application.WorksheetFunction.Countif( _ Range("A1:A10"), mModel)

This will load the mCount variable with the number of Fords, 4 in this instance. Similalry, we can use SUMIF to calculate the value Dim mModel As String Dim mCount As Long mModel = "Ford" mValue = Application.WorksheetFunction.SumIf( _ Range("A1:A10"), mModel, Range("C1:C10")) This will load the mCount variable with the value of the Fords, 33873 in this instance. The natural next step is to assume that we can extend this technique to our multiple condition test formulae discussed above. If we are using COUNTIFS and SUMIFS in Excel 2007 (see SUMPRODUCT and Excel 2007) then this is correct. For example, we can count how many Fords were sold in June using Dim mModel As String Dim mMonth As String Dim mCount As Long mModel = "Ford" mMonth = "June" mCount = Application.WorksheetFunction.CountIfs( _ Range("A1:A10"), mModel, _ Range("B1:B10"), mMonth) We get a result of 3 here in our mCount variable. Unfortunately, this technique cannot be extended to array formulae, or conditional testing SUMPRODUCT formulae. For example, a simple formula to count how many Fords were sold in Feb might be
=SUMPRODUCT((A2:A10="Ford")*(B2:B10="Feb"))

(none, as it happens), and you might think that we could use the following VBA to get the same result Dim mModel As String Dim mMonth As String Dim mCount As Long mModel = "Ford" mMonth = "Feb" mCount = Application.WorksheetFunction.Sumproduct( _ Range("A1:A10") = mModel , Range("C1:C10") = mMonth)) This fails to compile, never mind getting the correct result. In this case, VBA is trying to make a simple call to the worksheet function, but when array and these type of SUMPRODUCT formulae are resolved in Excel each item is within the array is resolved and then passed to the main function for SUMming, AVERAGEing, or whatever is being actioned. As VBA doesnt evaluate the ranges, it is not passing correct information to the worksheet function, so we get the error[4]. There is a solution to this problem, and that is to evaluate the function call within VBA, using the VBA Evaluate method, which converts a Microsoft Excel name to an a value. The code here is

Dim Dim Dim Dim

mModel As String mMonth As String mFormula As String mCount As Long mModel = "Ford" mMonth = "Feb" mFormula = "SUMPRODUCT((A1:A10=""" & mModel & _ """)*(B1:B10=""" & mMonth & """))" mCount = Application.Evaluate(mFormula)

Although there is more effort required to ensure that the syntax of the function call is properly constructed, and that strings tested against are properly formed with quotes around them[5], it is still a useful technique to have, and provides the capability to use SUMPRODUCT (and by association, array formulae) within VBA.

SUMPRODUCT and Excel 2007


When Microsoft introduced Excel 2007, the main focus was on ease of use, and improved business analysis functionality. Unfortunately, the worksheet functions did not get much attention, but there were a few new functions. Two of the new functions, COUNTIFS and
SUMIFS, support multiple conditional tests. For instance, in our previous examples , =SUMPRODUCT((A1:A10="Ford")*(B1:B10="June")) =SUMPRODUCT((A1:A10="Ford")*(B1:B10="June")*(C1:C10))

where we count those items where A1:A10 is = Ford AND B1:B10 = June, and where A1:A10 = Ford AND B1: B10 = June multiplied by C1:C10. In Excel 2007, COUNTIFS and SUMIFS can be used in place of SUMPRODUCT. The Excel 2007 formulae would be
=COUNTIFS(A1:A10,"Ford",B1:B10,"June") =SUMIFS(C1:C10,A1:A10,"Ford",B1:B10,"June")

A further improvement is that in Excel 2007, SUMPRODUCT can address a whole column, which is a helpful change. So, with Excel 2007 supporting multiple conditional tests, does this mean that the special use of SUMPRODUCT is now redundant, and that it is relegated to its original, simple array multiplication role? Whilst this may seem to be the case at first sight, a little thought shows that SUMPRODUCT retains its unique position in the Excel developers toolkit. Why? Because COUNTIFS and SUMIFS are still unable to calculate values in closed workbooks just as their predecessors could not; and the Excel 2007 functions are still not able to accommodate the complex extra functions that can be added to the conditional ranges in SUMPRODUCT.

Performance Considerations
Double Unary v * Operator

In most circumstances, either the '*' or -- versions of SUMPRODUCT can be used, and both will function correctly. There are some exceptions to this. Consider a table of names and amounts in A1:B10, where row 1 is a text heading of 'Name' and 'Amount'. The formula
=SUMPRODUCT(--(A1:A10="Bob"),--(B1:B10>0),B1:B10)

will correctly sum the positive values in column B where the value in column is 'Bob'. However, this formula
=SUMPRODUCT((A1:A10="Bob")*(B1:B10>0)*(B1:B10))

returns a #VALUE! Error. The reason for the error is due to the text in B1, multiplying a text value creates an error. To overcome it with the latter form, the ranges need to start beyond the heading, in A2 and B2[6]. Similalrly, if one or more of the ranges within the formula is multi-column, then the '*' operator again has to be used. Whilst this formula fails
=SUMPRODUCT(--(A1:A10="Bob"),--(B1:C10>0),--(B1:C10))

this formula works perfectly well


=SUMPRODUCT((A1:A10="Bob")*(B1:C10>0)*(B1:C10))

as indeed does this


=SUMPRODUCT((A1:A10="Bob")*(B1:C10>0),B1:C10)[7]

Using Transpose
If using the TRANSPOSE function within SUMPRODUCT, then the '*' operator has to be used.

Formula Efficiency
Most people will be familiar with the fact that array formulas can be very expensive, and if overused can significantly impair the recalculation of a worksheet/workbook. Whilst SUMPRODUCT is not an array formula per se, it suffers from the same problem. Although SUMPRODUCT is often faster than an equivalent array formula, it is marginal. And like array formula, SUMPRODUCT is much slower than COUNTIF/SUMIF,thus it is better to use these if appropriate. So, never use SUMPRODUCT in this situation
=SUMPRODUCT((A1:A10="Ford")*(C1:C10))

Use the equivalent SUMIF


=SUMIF(A1:A10,"Ford",C1:C10)

Even two COUNTIF /SUMIF functions are quicker than one SUMPRODUCT, so this formula
=COUNTIF(A1:A10,>=10)-COUNTIF(A1:A10,>20)

will be more efficient than this one,


=SUMPRODUCT((A1:A10>=10)*(A1:A10<=20))

by a factor of roughly 20%.

Notes
[1] We can also use =SUMPRODUCT(--(A1:A10={"Ford","Renault"})) in this instance as we have a single range being tested for two (or more) values, the -- is to coerce the Booleans to numbers that can be counted - see later. [2] Although array formulae are mentioned here, they are not explained. For a detailed discussion, see Chip Pearson's Array Formulas web page. [3] Excel 2007 has now removed this constraint, SUMPRODUCT can now use whole columns, as can any array formulae - see SUMPRODUCT and Excel 2007 [4] Note that the simple form of SUMPRODUCT, =SUMPRODUCT(rng1,rng2) works perfectly well in VBA as Application.WorksheetFunction.SUMPRODUCT(rng1,rng2), as VBA is conforming to the functions call criteria [5] When embedding quotes within a string, the quotes have to be doubled up, otherwise the single quote is taken as the start or end of the string. This gets more complex if the quotes are just after or just before an opening/closing quote, as we then have three quotes, i.e. one to tell VBA that the next quotes is part of the string, one for the embedded quotes, and one to close the string [6] The error is not caused because the text field is being summed, SUM happily ignores text, but rather because the value in column B is multiplied by the result of the conditional tests, it is multiplying text by a number that causes the #VALUE! [7] As can be seen, this restriction applies to SUMPRODUCT formulae with multiple columns, whether the multiple columns are within a conditional range or a value range

Introduction
Array formulas are a powerful tool in Excel. An array formula is a formula that works with an array, or series, of data values rather than a single data value. There are two flavors of array formulas: first, there are those formulas that work with an array or series of data and aggregate it, typically using SUM, AVERAGE, or COUNT, to return a single value to a single cell. In this type of array formula, the result, while calculated from arrays, is a single value. We will examine this type of array formula first. The second flavor of array formulas is a formula that returns a result in to two or more cells. These types of array formulas return an array of values as their result.

Single Value Result Array Formulas


For example, in its simple form, the formula =ROW(A1:A10) returns the number 1, which is the row number of the first cell in the range A1:A10. However, if this is entered as an array formula, it will return an array or series of numbers, each of which is the row number of a cell in the range A1:A10. That is, instead of returning the single value 1, it returns the array of numbers {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}. (In standard notation, arrays are written enclosed in curly braces { }.) When using array formulas, you typically use a container function such as SUM or COUNT to aggregate the array to a single number result. Expanding on the example above, the formula =SUM(ROW(A1:A10)) entered normally will return a value of 1. This because in its normal mode, ROW(A1:A10) returns a single number, 1, and then SUM just sums that single number. However, if the formula is entered as an array formula, ROW(A1:A10) returns the array of row numbers and then SUM adds up the elements of the array, giving a result of 55 ( = 1 + 2 + 3 + ... + 10). ENTERING AN ARRAY FORMULA: To enter a formula as an array formula, type the formula in the cell and press the CTRL SHIFT and ENTER keys at the same time rather then just ENTER. You must do this the first time you enter the formula and whenever you edit the formula later. If you do this properly, Excel will display the formula enclosed in curly braces { }. You do not type in the braces -- Excel will display them automatically. If you neglect to enter the formula with CTRL SHIFT ENTER, the formula may return a #VALUE error or return an incorrect result. All formulas on this page are array formulas and thus must be entered with CTRL SHIFT ENTER. You can download a workbook with the data and formulas on this page here.

Creating Array Formulas Using Arrays Of Data


The IF function can be used in an array formula to test the result of multiple cell tests at one time. For example, you may want to compute the average of the values in A1:A5 but exclude numbers that are less than or equal to zero. For this, you would use an array formula with an IF function to test the cell values and an AVERAGE function to aggregate the result. The following formula does exactly this:

=AVERAGE(IF(A1:A5>0,A1:A5,FALSE)) This formula works by testing each cell in A1:A5 to > 0. This returns an array of Boolean values such as {TRUE, TRUE, FALSE, FALSE, TRUE}. A BOOLEAN VALUE is a data type that contains either the value TRUE or the value FALSE. When converted to numbers in an arithmetic operation, TRUE is equivalent to 1 and FALSE is equivalent to 0. Most arithmetic functions like SUM and AVERAGE ignore Boolean values, so those values must be converted to numeric values before passing them to SUM or AVERAGE. The IF function tests each of these results individually, and returns the corresponding value from A1:A5 if True or the value FALSE if false. Fully expanded, the formula would look like the following: =AVERAGE(IF({TRUE,TRUE,FALSE,FALSE,TRUE},{A1,A2,A3,A4,A5}, {FALSE,FALSE,FALSE,FALSE,FALSE}) Note that the single FALSE value at the end of the original formula is expanded to an array of the appropriate size to match the array from the A1:A5 range in the formula. In array formulas, all arrays must be the same size. Excel will expand single elements to arrays as necessary, but will not resize arrays with more than one element to another size. If the arrays are not of the same size, you will get a #VALUE or in some cases a #N/A error. When the IF function evaluates, the following intermediate array is formed: {A1, A2, FALSE, FALSE, A5}. This is a substitution of the TRUE elements with the values from A1:A5 and the FALSE elements by FALSE. Since the AVERAGE function is designed within Excel to ignore Boolean values (TRUE or FALSE values), it will average only elements A1, A2, and A5 ignoring the TRUE and FALSE values. Note that the FALSE value is not converted to a zero. It is ignored completely by the AVERAGE function. Array formulas are ideal for counting or summing cells based on multiple criteria.

Consider the table of data shown to the right. It lists the number of products (column C) in different categories (column A) sold by various salesman (column B). To calculate the number of Fax machines sold by Brown, we can use the following array formula: =SUM((A2:A10="Fax")*(B2:B10="Brown")*(C2:C10)) This function builds three arrays. The first array is a series of TRUE or FALSE values which are the results of comparing A2:A10 to the word "Fax". (Remember, Excel will expand the single "Fax" element to an array of items all of which are "Fax".) The second array is also a series of TRUE or FALSE

values, the result of comparing B2:B10 to "Brown". (The single "Brown" element in the formula is expanded to an array of the appropriate size.) The third array is comprised of the number of units sold from the range C2:C10. These three arrays are multiplied together. When you multiply two arrays, the result is itself an array, each element of which is the product of the corresponding elements of the two arrays being multiplied. For example, {1, 2, 3} times {4, 5, 6} is {1*4, 2*5, 3*6} = {4, 10, 18}. When TRUE and FALSE values are used in any arithmetic operation, they are given the values 1 and 0, respectively. Thus in the formula above, Excel expands the formula into the three arrays: (A2:A10="Fax") {TRUE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE} (B2:B10="Brown") {TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE} (C2:C10) {1, 10, 20, 30, 40, 50, 60, 70, 80} When these array are multiplied, treating TRUE equal to 1 and FALSE equal to 0, we get the array {1, 0, 0, 0, 0, 0, 60, 0, 0} which are the quantities of Brown's two Fax sales. The SUM function simply adds up the elements of the array and return a result of 61, the number of Fax machines sold by Brown. You may have noticed that the logic of the formula tests Product equals "Fax" AND Salesman equals "Brown", but nowhere do we use the AND function. Here, we use multiplication to act as a logical AND function. Multiplication follows the same rules as the AND operator. It will return TRUE (or 1) only when both of the parameters are TRUE (or <> 0). If either or both parameters are FALSE (or 0), the result is FALSE (or 0).

Logical Operations With Array Formulas


In addition to the logical AND operation using multiplication shown above, other logical operations can be performed arithmetically. A logical OR operation can be accomplished with addition. For example, =SUM(IF(((A2:A10="Fax")+(B2:B10="Jones"))>0,1,0)) will count the number of sales (not the number of units sold) in which the product was a Fax OR the salesman was Jones (or both). Addition acts as an OR because the result it TRUE (or <> 0) if either one or both of the elements are TRUE (<> 0). It is FALSE ( = 0) only when both elements are FALSE (or 0). This formula adds two arrays: the results of the comparisons A2:A10 to "Fax", and the results of the comparisons B2:B10 to "Jones". Each of these arrays is an array of TRUE and FALSE values, each element being the result of comparing one cell to "Fax" or "Jones". It then adds these two arrays. When you add two arrays, the result is itself an array, each element of which is the sum of the corresponding element of the original arrays. For example, {1, 2, 3} + {4, 5, 6} = {1+4, 2+5, 3+6} = {5, 7, 9}. For each element in the sum array (A2:A10="Fax")+(B2:B10="Jones"), if that element is greater than 0, IF returns 1, otherwise it returns 0. Finally, SUM just adds up the array. An "exclusive or" or XOR operation is a comparison that returns TRUE when exactly one of the two elements is TRUE. XOR is FALSE if both elements are TRUE or if both elements are FALSE. Arithmetically, we can use the MOD operator to simulate an XOR operation. For example, to count the number of sales in which the product was a Fax XOR the salesman was Jones (excluding Faxes sold by Jones), we can use the following formula: =SUM(IF(MOD((A2:A10="Fax")+(B2:B10="Jones"),2),1,0))

A "negative and" or NAND operation is a comparison that returns TRUE when neither or exactly one of the elements is TRUE, but returns FALSE if both elements are TRUE. For example, we can count the number of sales except those in which Jones sold a Fax with the formula: =SUM(IF((A2:A10="Fax")+(B2:B10="Jones")<>2,1,0))

Creating Sequences And Loops For Array Formulas


When you are constructing some types of array formulas, you need to create a sequence of numbers for a function to process as an array. As an example, consider an array formula that will compute the average of the Nth largest elements in a range. To do this, we will use the LARGE function to get the largest numbers, and then pass those numbers as an array to AVERAGE to compute the average. Normally, the LARGE function takes as parameters a range to process and a number indicating which largest value to return (1 = largest, 2 = second largest, etc.,). But LARGE does work with arrays for its second parameter. You might be tempted to type in the array in the formula yourself: =LARGE(A1:A10,{1,2,3}). While this will indeed work, it is tedious. Instead, you can use the ROW function to return a sequence of numbers. When used in an array formula, the function ROW(m:n) will return an array of integers from m to n. Therefore, we can use ROW to create the array to pass to LARGE. This changes our array formula to =LARGE(A1:A10,ROW(1:3)). This brings us closer to a good formula, but two things remain. First, if you insert a row between rows 1 through 3, Excel will change the row reference 1:3, and therefore the formula will average the wrong numbers. Second, the formula is locked into the three largest values. We can make it more flexible by making the number of elements to average a cell reference that can be easily changed. For example, we can specify that cell C1 contains the size of the array to pass to LARGE. This is accomplished with the INDIRECT function. (Click here for more information about INDIRECT.) The INDIRECT function converts a string representing a cell reference into an actual cell reference. The sub-formula ROW(INDIRECT("1:"&C1)) will return an array of numbers between 1 and the value in cell C1. Now, coming together the formula to average the N largest values in A1:A10 becomes: =AVERAGE(LARGE(A1:A10,ROW(INDIRECT("1:"&C1))))

Formulas That Return Arrays


The other type of array formula is one that returns an array of numbers as its result. These sort of array formulas are entered into multiple cells that are then treated as a group. For example, consider the formula =ROW(A1:A10). If this is entered into one cell, either as a normal formula or as an array formula, the result will be 1 in that single cell. If, however, you array enter it into a range of cells each cell will contain one element of the array. To do this, you first must select the range of cells in to which the array should be written, say C1:C10, type the formula =ROW(A1:A10), and then press CTRL SHIFT ENTER. The elements of the array {1, 2, ...., 10} will be written to the range of cells, with one element of the array in each cell. When you array enter a formula into an array of cells, Excel prevents you from modifying a single cell with that array range. You may select the entire range, edit

the formula, and array-enter it again with CTRL SHIFT ENTER, but you cannot change a single element of the array. Some of the built-in Excel functions return an array of values. These formulas must be entered into an array of cells. For example, the MINVERSE function returns the inverse of a matrix with an equal number of rows and columns. Since the inverse of a matrix is itself a matrix, the MINVERSE function must be entered into a range of cells with the same number of rows and columns as the matrix to be inverted. Therefore, if your matrix is in cells A1:B2 (two rows and two columns), you must select a range the same size, type the formula =MINVERSE(A1:B2) and press CTRL SHIFT ENTER rather than just ENTER. This enters the formula as an array formula into all the selected cells. If you were to use the MINVERSE function in a single cell, only the upper left corner value of the inverted matrix would be returned. For information about writing your own VBA functions that return arrays, see Writing Your Own Functions In VBA.

Other Useful Array Functions


Array formulas can do a wide variety of tasks. A few miscellaneous array formulas are shown below: Sum Ignoring Errors Normally, if there is an error in a cell, the SUM function will return that error. The following formula will ignore the error values. =SUM(IF(ISERROR(A1:A10),0,A1:A10)) Average Ignoring Errors This formula will ignore errors when averaging range. =AVERAGE(IF(ISERROR(A1:A10),FALSE,IF(A1:A10="",FALSE,A1:A10))) Average Ignoring Zeros This formula will ignore zero values in an AVERAGE function. =AVERAGE(IF(A1:A10<>0,A1:A10,FALSE)) Sum Of Absolute Values You can sum a range of number treating them all as positive using the ABS function. =SUM(ABS(A1:A10)) Sum Of Integer Portion Only This formula will sum only the integer portion of the numbers in A1:A10. The fractional portion is discarded. =SUM(TRUNC(A1:A5)) Longest Text In Cells This formula will return the contents of the cell with the longest amount of text in it. =OFFSET(A1,MATCH(MAX(LEN(A1:A10)),LEN(A1:A10),0)-1,0,1,1)

Array Formulas Versus The Data Functions


There is considerable overlap between what you can accomplish with array formulas and what you can do with the so called D-Functions (DSUM, DCOUNT, and so on). Broadly speaking, the D-Functions are faster than their array formula counterparts. If you have a large and complex workbook with many array formulas, you may see a significant improvement in calculation time if you convert your array formulas to D-Functions. The primary differences between the DFunctions and array formulas are as follows:

D-Functions are typically faster than array formulas, all else being equal The selection criteria in a D-Function must reside in cells. Array formulas can include the selection criteria directly in the formula D-Functions can return only a single value to a single cell, while array formulas can return arrays to many cells

Tables And Lookups


This page describes a number of formulas to return data from tables and formulas to look up data in tables.

Introduction
Almost every worksheet contains at least one table of data, typically a set of rows and columns. Very frequently, you will need to return a row or column of values from the table the row or column position in the table, or you may need to return a value from the table based upon a match of values in the row headers and column headers. For example, you may need to return the 5th row of a table, or you may need to return the row where the ID number is 1234. The simplest types of lookups are performed with the VLOOKUP or HLOOKUP functions. The functions are well documented in the Help file and are not discussed in detail on this page. It is assumed that you are familiar with VLOOKUP and HLOOKUP. For more complicated lookups in tables, we will use formulas based on the OFFSET, MATCH, and INDEX functions. While the Help file describes these functions individually, it does not describe how these functions can be combined to create more powerful and flexible lookup formulas. That is the goal of this page. At the core of most of the formulas on this page is the OFFSET function. You should be familiar with this function before proceeding with this page. Most of the formulas on this page are array formulas. Array formulas are described in detail on the Array Formulas page on this web site. You should be at ease with array formulas in order to modify the lookup formulas presented on this page. With few exceptions, the formulas on this page use only a single range reference, a Defined Name that refers to the data table against which the lookup is

performed. Using a single reference may make the formulas longer, but it also makes them considerably more flexible. To use the formulas on your own worksheets, you need only modify a single name. This convenience makes up for, in my opinion, the longer formula length. Of course, if you are not using a defined name, simply replace the name in the formula with the appropriate range reference. If the formulas on this page do not return the expected result when you use them on your own worksheets, the first thing to check is to ensure that the formula is entered as an array formula. If you are unsure whether a formula needs to be array entered, go ahead and enter it as an array formula; that is completely safe. ENTERING AN ARRAY FORMULA: When you enter a formula as an array formula, you must press CTRL SHIFT ENTER rather than just ENTER when you first enter the formula and whenever you edit it later. If you do this properly, Excel will display the formula in the formula bar enclosed in curly braces, { }. You do not type in the curly braces, { }; Excel will display them automatically. In the interest of brevity and clarity, the formulas on this page do not have any error checking and handling. For example, there is nothing to prevent you from attempting to return the 6th row of a table that has only 4 rows. If a parameter in a function call is invalid, you will most likely get a #N/A error. You may want to add some error checks when you use these formulas in your own worksheets. As is the case with many types of formulas in Excel, there are several different ways to accomplish the same thing. Many of the formulas on this page could be written with a combination of the INDEX and MATCH functions instead of the OFFSET function. OFFSET is neither better nor worse than INDEX/MATCH. For consistency, I have chosen to use OFFSET for nearly all the tasks at hand. Other sources may use other methods. I encourage you to learn a variety of ways to accompish a task.

Example Data
The example formulas in the first section of this page, those formulas for returning rows and columns of a table, use the following data table.

This table contains two named ranges that are used in the formulas. The name Table refers to the entire table, cells B2:G7, which includes the row labels and column labels. The name InnerTable refers only the the actual data, cells C3:G7, which does not include the row labels and the column labels. For illustration, the values of the row labels (abby, beth, etc.) and the column labels (apples, oranges, etc) are in alphabetical order. This is for illustration only. The formulas do not require that the values be in any particular order.

Returning A Row Or Column From A Table


You can use an array formula to return a single row or column from a table. The formulas in this section need to be array entered (press CTRL SHIFT ENTER rather than just ENTER) into a number of cells equal to the size of the row or column of the table. The example table contains 6 columns (including the row header); thus, you would select a range that is 6 columns wide and 1 row tall, enter the formula and press CTRL SHIFT ENTER. The first formulas return a single row, based on position, from Table or InnerTable. =OFFSET(Table,E13-1,0,1,COLUMNS(Table)) In this formula, cell E13 contains the row to return. The row is 1-based (the title row is 1, the first row of data is 2, etc). The OFFSET function uses 0-based rows and columns, so we subtract 1 from the row number before passing it into the OFFSET function. If cell E13 contains the number 5, the formula returns the following values:

The following formula returns a row from the InnerTable range. It return only the data values, not the row header. =OFFSET(InnerTable,E18-1,0,1,COLUMNS(InnerTable)) In this formula, cell E18 contains the 1-based row of InnerTable to return. Thus, if cell E18 contains 5, the formula returns the following values.

By changing the values that are passed to the OFFSET function, we can return a column from either the Table or InnerTable range, either by using a column offset or the value of a column label. The following formula will return a column from the Table range. =OFFSET(Table,0,E22-1,ROWS(Table),1) If cell E22 contains the value 3, the third column of Table is returned, as shown below:

Since this formula returns a column of data from Table, it should be array entered into to a range that is one column wide and has the same number of rows and the Table range. You can also return a column from Table that corresponds to a matching column label. The following formula will return the column from Table whose column label is equal to the value in cell E39. =OFFSET(Table,0,MATCH(E39,OFFSET(Table,0,0,1,COLUMNS(Table)),0)1,ROWS(Table),1) If cell E39 contains the value plums, the following values are returned.

Calculations On Rows Or Columns Of A Table


Because the formulas described above return arrays of values, either a row or column of the InnerTable, you can use those formulas with functions that accept arrays. Indeed, you can use the row and column functions in any function or formula where you would normally provide a range of cells, such as in the SUM, MIN, MAX, or AVERAGE functions, among others. For example, the following formula will return the SUM of the row whose row label is equal to the value in cell E48. =SUM(OFFSET(InnerTable,MATCH(E48,OFFSET(Table,0,0,ROWS(Table),1),0)2,0,1,COLUMNS(InnerTable))) If cell E48 contains the value callie, this formula will return the value 560. You can get the maximum or minimum of the row by changing SUM to MAX or MIN. These formula do not need to be entered as array formulas, although it is harmless to do so. A very similar formula can be used to return the sum, minimum, or maximum of a column in the table. The following formula will return the sum of the values in the column of Table where the column label is equal to the value in cell E52. =SUM(OFFSET(InnerTable,0,MATCH(E52,OFFSET(Table,0,0,1,COLUMNS(Table)),0)2,ROWS(InnerTable),1)) If cell E52 contains oranges, the formula will return 535. As before, you can change SUM to MIN or MAX to return the minimum or maximum of the column. Again, these formulas need not be array entered.

Last Value In A Row Or Column


You can use a formula to return the last cell in a row or column, where the row or column is select either by its position in the table or by a match of a value with the row or column label. The following formula will return the last (right-most) value in a row of Table, where cell E56 contains the 1-based row position: =OFFSET(Table,E56-1,COLUMNS(Table)-1,1,1) If E56 contains 4, the result is 122, the last value in the 4th column of Table (including the column labels). You also select the row to use by matching a row label. If cell E59 contains the value callie, the following formula will return 122, the right-most value in the row whose row label is callie. =OFFSET(Table,MATCH(E59,OFFSET(Table,0,0,ROWS(Table),1),0)-1,COLUMNS(Table)1,1,1) The following formulas will return the last (bottom-most) value of a column, selected by either its position in Table (cell E62) or by a match of a column label (in cell E65). =OFFSET(Table,E62-1,COLUMNS(Table)-1,1,1) =OFFSET(Table,ROWS(Table)-1,MATCH(E65,OFFSET(Table,0,0,1,COLUMNS(Table)),0)1)

Double Lookups
A double lookup is a formula that returns a value from a table based on a match of values in both the rows and columns. Refering to the example data shown above, you may want to return the value corresponding to the dora row and the plums column. If cell E74 contains the value to match on the rows (e.g., dora) and cell E75 contains the value to match on the columns (e.g., plums), the following formula will return the appropriate value from the Table range: =OFFSET(Table,MATCH(E74,OFFSET(Table,0,0,ROWS(Table),1),0)-1, MATCH(E75,OFFSET(Table,0,0,1,COLUMNS(Table)),0)-1)

Left Lookups
While the VLOOKUP function is very useful, it has a significant limitation. That is that you can only return a value to the right of the lookup column. For example, you can look in column B for a value and then return the corresponding value from column D. However, the reverse is not true. You cannot look up a value in column D and return the corresponding value from column B. This is where a Left Lookup formula is useful. For example, suppose you have the following table, and a defined name of LLTable that refers to the actual data (colored in red).

The following formula will look for a value in the Value column and return the corresponding value in the Type column. =OFFSET(LLTable,MATCH(F67,OFFSET(LLTable,0,1,ROWS(LLTable),1),0)-1,0,1,1) In this formula, cell F67 contains the value to be searched for in the Value column. Thus, if F67 contains 44, the formula will return dd.

Upper Lookups
The HLOOKUP function is the "transpose" of the VLOOKUP function. As VLOOKUP scans down a column for a match and then moves to the right to return a value, HLOOKUP scans across a row for a match and then moves down to return a value. HLOOKUP cannot move upwards to return a value. For example, you can search row 5 to find a match and then return the corresponding value from row 8, but the reverse is not possible. You cannot scan row 8 and return a value from row 5. Just as the Left Lookup formula overcame the limitation of VLOOKUP, an Upper Lookup formula can overcome the limitation of HLOOKUP. Consider the following table:

In this table, the range displayed in red has the name ULTable. The followng formula will allow you to look in the Value row for a value equal to cell J82 and return the corresponding value from the Type row. =OFFSET(ULTable,0, MATCH(J82, OFFSET(ULTable,ROWS(ULTable)1,0,1,COLUMNS(ULTable)),0)-1,1,1) For example, if J82 contains 33, the formula will return cc.

Arbitrary Lookups
Another limitation of the VLOOKUP function is that if there are duplicate matches in the lookup column, the first occurrence of the matching value is used. For example, consider the following table of data:

With a simple VLOOKUP function for the value Beth, the value 22 will be returned, since 22 corresponds to the first occurrence of the value Beth. It may be necessary, however, to return the value corresponding to the second or third occurrence of Beth. If the table of values (colored in red, excluding the Name and Score column labels) is named ALTable, the following formula will return the value form the Score column corresponding the the Nth occurrence of the value in cell F90, where the number N is in cell F91. For example, if F90 contains the value Beth and cell F91 contains the value 3 (indicating to find the 3rd occurrence of Beth), the formula will return the value 88. =INDEX(ALTable,SMALL(IF(OFFSET(ALTable,0,0,ROWS(ALTable),1)=F90, ROW(OFFSET(ALTable,0,0,ROWS(ALTable),1))-ROW(OFFSET(ALTable,0,0,1,1))+1, ROW(OFFSET(ALTable,ROWS(ALTable)-1,0,1,1))+1),F91),2) A special case of the arbitrary lookup formula above is to return the value corresponding to the last occurrence in the list. For example, if cell F94 contains the value Beth, the following formula will return the value 88, which corresponds to the last occurrence of the value Beth. =INDEX(ALTable,SMALL(IF(OFFSET(ALTable,0,0,ROWS(ALTable),1)=F94, ROW(OFFSET(ALTable,0,0,ROWS(ALTable),1))-ROW( OFFSET(ALTable,0,0,1,1) )+1, ROW(OFFSET(ALTable,ROWS(ALTable)-1,0,1,1)) +1),COUNTIF(OFFSET(ALTable,0,0,ROWS(ALTable),1),F94)),2)

Closest Match Lookups


The MATCH function is an important tool when working with lists of data. If you are searching for an exact match in a range of cells, the values may be in any order. However, if you are attempting to find a closest match, the values must be in sorted order. Using the INDEX and MATCH functions, you can write a formula that will return the number in a list that is closest to a specified value. We will look at three related Closest Match formula. These three formulas are based on the example data shown below. All three formulas are array formulas and must be properly entered. This list of values has the defined name of CMTable.

The following array formula will return the smallest number in the list CMTable that greater than or equal to the value in cell E105. =INDEX(CMTable,MATCH(MIN(IF(CMTable-E105>=0,CMTable,FALSE)),IF(CMTableE105>=0,CMTable,FALSE),0)) Thus is E105 has the value 5, the formula will return 5.1, which is the smallest number in the list that is greater than or equal to 5. The second Closest Match formula will return the largest number in a list that is less than or equal to a specified number. In the following formula, cell E108 contains the test value. =INDEX(CMTable,MATCH(MAX(IF(CMTable-E108<=0,CMTable,FALSE)),IF(CMTableE108<=0,CMTable,FALSE),0)) Thus, if cell E108 has the value 8, the formula will return 7.4, which is the largest number in the range that is less than or equal to 8. The third and final Closest Match formula will return the value in a list that is closest to a specified value. The returned value might be less than the test value or it might be greater than the test value. =INDEX(CMTable,MATCH(MIN(ABS(CMTable-E111)),ABS(CMTable-E111),0),1) Thus, if cell E111 contains the value 5, the formula will return 5.1, since 5.1 is closer to 5 than any other value in the list.

Duplicate Data In Lists


This page describes techniques for dealing with duplicate items in a list of data.

Introduction
Very often, Excel is used to manage lists of data, such as employee names or phone lists. In such circumstances, duplicates may exist in the list and need to be identified. This page contains a number of formulas that can be used to work with duplicate items in a list of data. All the formulas on this page are array formulas. DEFINITION: Array Formula An array formula is a formula that works with arrays or series of data rather than single data values. When you enter an array formula, type the formula in the cell and then press CTRL SHIFT ENTER rather than just ENTER when you first enter the formula and when you edit it later. If you do this properly, Excel will display the formula enclosed in curly braces { }. Array formulas are discussed in detail on the Array Formulas page. You can download an example workbook here that illustrates all the formulas on this page. For a VBA Function that returns an array of the distinct items in a range or array, see the Distinct Values Page. This function can be called either from a range of worksheet cells or from other VB code.

Testing A List For Duplicate Items


The formula below will display the words "Duplicates" or "No Duplicates" indicating whether there are duplicates elements in the list A2:A11. =IF(MAX(COUNTIF(A2:A11,A2:A11))>1,"Duplicates","No Duplicates") An alternative formula, one that will work with blank cells in the range, is shown below. Note that the entire formula should be entered in Excel on one line. =IF(MAX(COUNTIF(INDIRECT("A2:A"&(MAX((A2:A11<>"")*ROW(A2:A11)))), INDIRECT("A2:A"&(MAX((A2:A11<>"")*ROW(A2:A11))))))>1,"Duplicates","No Duplicates")

Highlighting Duplicate Entries


You can use Excel's Conditional Formatting tool to highlight duplicate entries in a list. All of the examples in this section assume that the data to be tested and highlighted is in the range B2:B11. You should change the cell references to the appropriate values on your worksheet.

This first example will highlight duplicate rows in the range B2:B11. Select the cells that you wish to test and format, B2:B11 in this example. Then, open the Conditional Formatting dialog from the Format menu, change Cell Value Is to Formula Is, enter the formula below, and choose a font or background format to apply to cells that are duplicates. =COUNTIF($B$2:$B$11,B2)>1 The formula above, when used in Conditional Formatting, will highlight all duplicates. That is, if the value 'abc' occurs twice in the list, both instances of 'abc' will be highlighted. This is shown in the image to the left, in which all occurrences of 'a' and 'g' are higlighted.

You can use the following formula in Conditional Formatting to highlight only the first occurrence of an entry in the list. For example, the first occurrence of 'abc' will be highlighted, but the second and subsequent occurrences of 'abc' will not be highlighted. =IF(COUNTIF($B$2:$B$11,B2)=1,FALSE,COUNTIF($B$2:B2,B2)=1) This is shown at the left where only the first occurrences of the duplicate items 'a', 'e', and 'g' are highlighted. The second and subsequent occurrences of these values are not highlighted.

You can also do the reverse of this with Conditional Formatting. Using the formula below in Conditional Formatting will highlight only the second and subsequent occurrences of a value. The first occurrence of the value will not be highlighted. =IF(COUNTIF($B$2:$B$11,B2)=1,FALSE,NOT(COUNTIF($B$2:B2,B2)=1)) This is shown at the left where only the second occurrences of 'a', 'b', 'c' and 'f' are highlighted. The first occurrences of these items are not highlighted.

Another formula for Conditional Formatting will highlight only the last occurrence of a duplicate element in a list (or the element itself if it occurs only once). =IF(COUNTIF($B$2:$B$11,B2)=1,TRUE,COUNTIF($B$2:B2,B2)=COUNTIF($B$2:$B$11,B2) ) As you can see only the last occurrences of elements 'a', 'b', 'c', and 'f' are highlighted. Element 'd' is highlighted because it occurs only once. The occurrences of 'a', 'b', 'c' and 'f' that occurs before the last occurrence are not highlighted.

We can round out our discussion of highlighting duplicate rows with two additional formula related to distinct items in a list.

The following can be used in Conditional Formatting to highlight elements that occur only once in the range B2:B11. =COUNTIF($B$2:$B$11,B2)=1 This image illustrates the formula. Elements 'b', 'c', and 'e' are highlighted because they occur only once in the list. Items 'a', 'd' and 'f' are not highlighted because they occur more than one time in the list.

Finally, the following formula can be used in Conditional Formatting to highlight the distinct values in B2:B11. If an element occurs once, it is highlighted. If it occurs more then once, then only the first occurrence is highlighted. =COUNTIF($B$2:B2,B2)=1 As you can see, only the first or only occurrences of the elements are highlighted. If an element is duplicated, as is 'b', the duplicate elements are not highlighted.

Functions For Duplicates


All of the formulas described above for Conditional Formatting can also be used in worksheet cells. They are all array formulas, so you must select the range for the results, type in the formula, and press CTRL SHIFT ENTER. The results of each formula will be a series of True or False values. The True results correspond to those cells that are highlighted in Conditional Formatting and the False results correspond to those cells that are not highlighted by Conditional Formatting.

Counting Distinct Entries In A Range


The following formulas will return the number of distinct items in the range B2:B11. Remember, all of these are array formulas. The following formula is the longest but most flexible. It will properly count a list that contains a mix of numbers, text strings, and blank cells. =SUM(IF(FREQUENCY(IF(LEN(B2:B11)>0,MATCH(B2:B11,B2:B11,0),""), IF(LEN(B2:B11)>0,MATCH(B2:B11,B2:B11,0),""))>0,1)) If your data does not have any blank entries, you can use the simpler formula below. =SUM(1/COUNTIF(B2:B11,B2:B11)) If your data has only numeric values or blank cells (no string text entries), you can use the following formula: =SUM(N(FREQUENCY(B2:B11,B2:B11)>0))

Array Formulas
Many of the formulas described here are Array Formulas, which are a special type of formula in Excel. If you are not familiar with Array Formulas, click here.

Array To Column
Sometimes it is useful to convert an MxN array into a single column of data, for example for charting (a data series must be a single row or column). Click here for more details.

Averaging Values In A Range


You can use Excel's built in =AVERAGE function to average a range of values. By using it with other functions, you can extend its functionality. For the formulas given below, assume that our data is in the range A1:A60.

Averaging Values Between Two Numbers


Use the array formula =AVERAGE(IF((A1:A60>=Low)*(A1:A60<=High),A1:A60)) Where Low and High are the values between which you want to average.

Averaging The Highest N Numbers In A Range


To average the N largest numbers in a range, use the array formula =AVERAGE(LARGE(A1:A60,ROW(INDIRECT("1:10")))) Change "1:10" to "1:N" where N is the number of values to average.

Averaging The Lowest N Numbers In A Range


To average the N smallest numbers in a range, use the array formula =AVERAGE(SMALL(A1:A60,ROW(INDIRECT("1:10")))) Change "1:10" to "1:N" where N is the number of values to average. In all of the formulas above, you can use =SUM instead of =AVERAGE to sum, rather than average, the numbers.

Counting Values Between Two Numbers


If you need to count the values in a range that are between two numbers, for example between 5 and 10, use the following array formula: =SUM((A1:A10>=5)*(A1:A10<=10)) To sum the same numbers, use the following array formula:

=SUM((A1:A10>=5)*(A1:A10<=10)*A1:A10)

Counting Characters In A String


The following formula will count the number of "B"s, both upper and lower case, in the string in B1. =LEN(B1)-LEN(SUBSTITUTE(SUBSTITUTE(B1,"B",""),"b",""))

Date And Time Formulas


A variety of formulas useful when working with dates and times are described on the DateTime page. Other Date Related Procedures are described on the following pages. Adding Months And Years The DATEDIF Function Date Intervals Dates And Times Date And Time Entry Holidays Julian Dates

Duplicate And Unique Values In A Range


The task of finding duplicate or unique values in a range of data requires some complicated formulas. These procedures are described in Duplicates.

Dynamic Ranges
You can define a name to refer to a range whose size varies depending on its contents. For example, you may want a range name that refers only to the portion of a list of numbers that are not blank. such as only the first N non-blank cells in A2:A20. Define a name called MyRange, and set the Refers To property to: =OFFSET(Sheet1!$A$2,0,0,COUNTA($A$2:$A$20),1) Be sure to use absolute cell references in the formula. Also see then Named Ranges page for more information about dynamic ranges.

Finding The Used Part Of A Range


Suppose we've got a range of data called DataRange2, defined as H7:I25, and that cells H7:I17 actually contain values. The rest are blank. We can find various properties of the range, as follows:

To find the range that contains data, use the following array formula: =ADDRESS(ROW(DataRange2),COLUMN(DataRange2),4)&":"& ADDRESS(MAX((DataRange2<>"")*ROW(DataRange2)),COLUMN(DataRange2)+ COLUMNS(DataRange2)-1,4) This will return the range H7:I17. If you need the worksheet name in the returned range, use the following array formula: =ADDRESS(ROW(DataRange2),COLUMN(DataRange2),4,,"MySheet")&":"& ADDRESS(MAX((DataRange2<>"")*ROW(DataRange2)),COLUMN(DataRange2)+ COLUMNS(DataRange2)-1,4) This will return MySheet!H7:I17. To find the number of rows that contain data, use the following array formula: =(MAX((DataRange2<>"")*ROW(DataRange2)))-ROW(DataRange2)+1 This will return the number 11, indicating that the first 11 rows of DataRange2 contain data. To find the last entry in the first column of DataRange2, use the following array formula: =INDIRECT(ADDRESS(MAX((DataRange2<>"")*ROW(DataRange2)), COLUMN(DataRange2),4)) To find the last entry in the second column of DataRange2, use the following array formula: =INDIRECT(ADDRESS(MAX((DataRange2<>"")*ROW(DataRange2)), COLUMN(DataRange2)+1,4))

First And Last Names


Suppose you've got a range of data consisting of people's first and last names. There are several formulas that will break the names apart into first and last names separately. Suppose cell A2 contains the name "John A Smith". To return the last name, use =RIGHT(A2,LEN(A2)-FIND("*",SUBSTITUTE(A2," ","*",LEN(A2)LEN(SUBSTITUTE(A2," ",""))))) To return the first name, including the middle name (if present), use =LEFT(A2,FIND("*",SUBSTITUTE(A2," ","*",LEN(A2)LEN(SUBSTITUTE(A2," ",""))))-1) To return the first name, without the middle name (if present), use =LEFT(B2,FIND(" ",B2,1)) We can extend these ideas to the following. Suppose A1 contains the string "First Second Third Last".

Returning First Word In A String


=LEFT(A1,FIND(" ",A1,1)) This will return the word "First".

Returning Last Word In A String


=RIGHT(A1,LEN(A1)-MAX(ROW(INDIRECT("1:"&LEN(A1))) *(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)=" "))) This formula in as array formula. (This formula comes from Laurent Longre). This will return the word "Last"

Returning All But First Word In A String


=RIGHT(A1,LEN(A1)-FIND(" ",A1,1)) This will return the words "Second Third Last"

Returning Any Word Or Words In A String


The following two array formulas come compliments of Laurent Longre. To return any single word from a single-spaced string of words, use the following array formula: =MID(A10,SMALL(IF(MID(" ("1:"&LEN(A10)+1)),1)=" B10),SUM(SMALL(IF(MID(" ("1:"&LEN(A10)+2)),1)=" B10+{0,1})*{-1,1})-1) "&A10,ROW(INDIRECT ",ROW(INDIRECT("1:"&LEN(A10)+1))), "&A10&" ",ROW(INDIRECT ",ROW(INDIRECT("1:"&LEN(A10)+2))),

Where A10 is the cell containing the text, and B10 is the number of the word you want to get. This formula can be extended to get any set of words in the string. To get the words from M for N words (e.g., the 5th word for 3, or the 5th, 6th, and 7th words), use the following array formula: =MID(A10,SMALL(IF(MID(" "&A10,ROW(INDIRECT ("1:"&LEN(A10)+1)),1)=" ",ROW(INDIRECT("1:"&LEN(A10)+1))), B10),SUM(SMALL(IF(MID(" "&A10&" ",ROW(INDIRECT ("1:"&LEN(A10)+2)),1)=" ",ROW(INDIRECT("1:"&LEN(A10)+2))), B10+C10*{0,1})*{-1,1})-1) Where A10 is the cell containg the text, B10 is the number of the word to get, and C10 is the number of words, starting at B10, to get.

Note that in the above array formulas, the {0,1} and {-1,1} are enclosed in array braces (curly brackets {} ) not parentheses.

Grades
A frequent question is how to assign a letter grade to a numeric value. This is simple. First create a define name called "Grades" which refers to the array: ={0,"F";60,"D";70,"C";80,"B";90,"A"}

Then, use VLOOKUP to convert the number to the grade: =VLOOKUP(A1,Grades,2) where A1 is the cell contains the numeric value. You can add entries to the Grades array for other grades like C- and C+. Just make sure the numeric values in the array are in increasing order.

High And Low Values


You can use Excel's Circular Reference tool to have a cell that contains the highest ever reached value. For example, suppose you have a worksheet used to track team scores. You can set up a cell that will contain the highest score ever reached, even if that score is deleted from the list. Suppose the score are in A1:A10. First, go to the Tools->Options dialog, click on the Calculation tab, and check the Interations check box. Then, enter the following formula in cell B1: =MAX(A1:A10,B1) Cell B1 will contian the highest value that has ever been present in A1:A10, even if that value is deleted from the range. Use the =MIN function to get the lowest ever value. Another method to do this, without using circular references, is provided by Laurent Longre, and uses the CALL function to access the Excel4 macro function library. Click here for details.

Left Lookups
The easiest way do table lookups is with the =VLOOKUP function. However, =VLOOKUP requires that the value returned be to the right of the value you're looking up. For example, if you're looking up a value in column B, you cannot retrieve values in column A. If you need to retrieve a value in a column to the left of the column containing the lookup value, use either of the following formulas: =INDIRECT(ADDRESS(ROW(Rng)+MATCH(C1,Rng,0)-1,COLUMN(Rng)-ColsToLeft)) Or =INDIRECT(ADDRESS(ROW(Rng)+MATCH(C1,Rng,0)-1,COLUMN(A:A) )) Where Rng is the range containing the lookup values, and ColsToLeft is the number of columns to the left of Rng that the retrieval values are. In the second syntax, replace "A:A" with the column containing the retrieval data. In both examples, C1 is the value you want to look up.

Minimum And Maximum Values In A Range


Of course you can use the =MIN and =MAX functions to return the minimum and maximum values of a range. Suppose we've got a range of numeric values called NumRange. NumRange may contain duplicate values. The formulas below use the following example:

Address Of First Minimum In A Range


To return the address of the cell containing the first (or only) instance of the minimum of a list, use the following array formula: =ADDRESS(MIN(IF(NumRange=MIN(NumRange),ROW(NumRange))),COLUMN(NumRange),4) This function returns B2, the address of the first '1' in the range.

Address Of The Last Minimum In A Range


To return the address of the cell containing the last (or only) instance of the minimum of a list, use the following array formula: =ADDRESS(MAX(IF(NumRange=MIN(NumRange),ROW(NumRange)*(NumRange<>""))), COLUMN(NumRange),4) This function returns B4, the address of the last '1' in the range.

Address Of First Maximum In A Range


To return the address of the cell containing the first instance of the maximum of a list, use the following array formula: =ADDRESS(MIN(IF(NumRange=MAX(NumRange),ROW(NumRange))),COLUMN(NumRange),4) This function returns B1, the address of the first '5' in the range.

Address Of The Last Maximum In A Range


To return the address of the cell containing the last instance of the maximum of a list, use the following array formula: =ADDRESS(MAX(IF(NumRange=MAX(NumRange),ROW(NumRange)*(NumRange<>""))), COLUMN(NumRange),4) This function returns B5, the address of the last '5' in the range.

Most Common String In A Range


The following array formula will return the most frequently used entry in a range: =INDEX(Rng,MATCH(MAX(COUNTIF(Rng,Rng)),COUNTIF(Rng,Rng),0)) Where Rng is the range containing the data.

Ranking Numbers
Often, it is useful to be able to return the N highest or lowest values from a range of data. Suppose we have a range of numeric data called RankRng. Create a range next to RankRng (starting in the same row, with the same number of rows) called TopRng. Also, create a named cell called TopN, and enter into it the number of values you want to return (e.g., 5 for the top 5 values in RankRng). Enter the following formula in the first cell in TopRng, and use Fill Down to fill out the range: =IF(ROW()-ROW(TopRng)+1>TopN,"",LARGE(RankRng,ROW()-ROW(TopRng)+1)) To return the TopN smallest values of RankRng, use =IF(ROW()-ROW(TopRng)+1>TopN,"",SMALL(RankRng,ROW()-ROW(TopRng)+1)) The list of numbers returned by these functions will automatically change as you change the contents of RankRng or TopN.

Removing Blank Cells In A Range


The procedures for creating a new list consisting of only those entries in another list, excluding blank cells, are described in NoBlanks.

Summing Every Nth Value


You can easily sum (or average) every Nth cell in a column range. For example, suppose you want to sum every 3rd cell. Suppose your data is in A1:A20, and N = 3 is in D1. The following array formula will sum the values in A3, A6, A9, etc. =SUM(IF(MOD(ROW($A$1:$A$20),$D$1)=0,$A$1:$A$20,0)) If you want to sum the values in A1, A4, A7, etc., use the following array formula: =SUM(IF(MOD(ROW($A$1:$A$20)-1,$D$1)=0,$A$1:$A$20,0)) If your data ranges does not begin in row 1, the formulas are slightly more complicated. Suppose our data is in B3:B22, and N = 3 is in D1. To sum the values in rows 5, 8, 11, etc, use the following array formula: =SUM(IF(MOD(ROW($B$3:$B$22)-ROW($B$3)+1,$D$1)=0,$B$3:B$22,0)) If you want to sum the values in rows 3, 6, 9, etc, use the following array formula: =SUM(IF(MOD(ROW($B$3:$B$22)-ROW($B$3),$D$1)=0,$B$3:B$22,0))

Miscellaneous
Sheet Name

Suppose our active sheet is named "MySheet" in the file C:\Files\MyBook.Xls. To return the full sheet name (including the file path) to a cell, use =CELL("filename",A1) Note that the argument to the =CELL function is the word "filename" in quotes, not your actual filename. This will return "C:\Files\[MyBook.xls]MySheet" To return the sheet name, without the path, use =MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1, LEN(CELL("filename",A1))-FIND("]",CELL("filename",A1))) This will return "MySheet"

File Name
Suppose our active sheet is named "MySheet" in the file C:\Files\MyBook.Xls. To return the file name without the path, use =MID(CELL("filename",A1),FIND("[",CELL("filename",A1))+1,FIND("]", CELL("filename",A1))-FIND("[",CELL("filename",A1))-1) This will return "MyBook.xls" To return the file name with the path, use either =LEFT(CELL("filename",A1),FIND("]",CELL("filename",A1))) Or =SUBSTITUTE(SUBSTITUTE(LEFT(CELL("filename",A1),FIND("]", CELL("filename",A1))),"[",""),"]","") The first syntax will return "C:\Files\[MyBook.xls]" The second syntax will return "C:\Files\MyBook.xls" In all of the examples above, the A1 argument to the =CELL function forces Excel to get the sheet name from the sheet containing the formula. Without it, and Excel calculates the =CELL function when another sheet is active, the cell would contain the name of the active sheet, not the sheet actually containing the formula.

Potrebbero piacerti anche