Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.
With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company.
Learn more about Allen...
Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Averaging a Non-Contiguous Range.
Tom needs to average a series of non-contiguous cells, excluding any cells that may contain zero values. Specifically, he needs to average cells A1, C1, E1, G1, and J1, but only counting those cells that don't contain zero.
Before getting into what works, let's take a look at what doesn't work. First of all, it doesn't work to simply add the cells and divide by 5; that doesn't take zero values into account. Second, it doesn't work to use COUNTIF in the denominator of your formula, as shown here:
=(A1+C1+E1+G1+J1) / COUNTIF(A1:J1,"<>0")
This doesn't work because it examines and counts cells within the entire range of A1:J1, not just the five cells you want considered in the average. You might also think that you could select your five non-contiguous cells, give them a name, and then use the name in your formula. While Excel allows you to create the name, the following gives an error:
=SUM(MyCells) / COUNTIF(MyCells,"<>0")
It appears that COUNTIF will only work with a single contiguous range, so the non-contiguous nature of the MyCells range throws the function into a tailspin.
You can calculate the average of these five cells by applying a bit of "trickery" to your denominator, in this manner:
=(A1+C1+E1+G1+J1) / ((A1<>0)+(C1<>0)+(E1<>0)+(G1<>0)+(J1<>0))
The evaluation done on each cell in the denominator returns either a 1 (for True) or a 0 (for False) depending on whether the cell contains a non-zero value or not. This series of values is added together, providing the necessary count of non-zero cells for the denominator.
Notice that the discussion here has been all about the denominator in the formula, not the numerator. The reason is simple—you can add all five values into the numerator; zero values there don't really matter. The only place they matter is in the denominator, which is what makes calculating this average so tricky.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (7842) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Averaging a Non-Contiguous Range.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!