Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Averaging a Non-Contiguous Range.
by Allen Wyatt
(last updated July 31, 2017)
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. A similar problem occurs if you try to use a non-contiguous range with the AVERAGEIF function:
Since you can't use any of the functions you might want to use, you are left to rely on a bit longer formula to calculate the average. 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 (7845) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Averaging a Non-Contiguous Range.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
Calculating an average of a group of numbers is easy. What if you want to exclude a couple of the numbers from the group ...Discover More
A moving average can be a great way to analyze a series of data points that you've collected over time. Setting up a ...Discover More
Need to calculate a running average for the last twelve values in a constantly changing range of values? The formula ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.