Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. 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.
Written by Allen Wyatt (last updated February 17, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021
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:
=AVERAGEIF(MyCells, "<>0")
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, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel 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!
Grabbing an average of a range of cells is easy using Excel functions. If you want that average to ignore hidden cells ...
Discover MoreA moving average can be a great way to analyze a series of data points that you've collected over time. Setting up a ...
Discover MoreExcel is often used to analyze data collected over time. In doing the analysis, you may want to only look at data ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2024-02-19 20:34:49
Peter
Lets say you had helper cells in row 2, with a value TRUE under the values you wanted to average. The simplest way to implement the average, in Office 365 anyway, would be
=AVERAGE(FILTER(A1:J1,A2:J2))
The helper cells would contain a formula like =A1<>0, which could be copied across to column J
2024-02-17 08:37:44
Alan Cannon
I take exception to not including zero values in the average as a general rule. Zero can be a legitimate value to be included depending on the nature of the elements being averaged. For example, if you are getting the average of the number of occurrences of an event over a period of days and the event didn't happen on a given day, then zero is a legitimate value to be included in the average. It should only be excluded if you are averaging the number of occurrences greater than zero. This requires differentiating between zero and a blank. A blank could occur if the number of events wasn't recorded for a given day.
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments