**Please Note: **
This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. 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: An Average that Excludes Zero Values.

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

The worksheet function most suited to this purpose is to use AVERAGEIF. You can use it in this manner:

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

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

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

Of course, if you want to approach the problem "old school" (without using AVERAGEIF or AVERAGEIFS), then there are several ways you can proceed. The first is to remember how an average is calculated. It is defined as the sum of a range of values divided by the number of items in the range. Thus, you could figure the exclusionary average by simply making sure that the denominator (the number you are dividing by) does not include any zero values. For instance:

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

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

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

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

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

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

Remember that array formulas need to be entered by using the combination **Ctrl+Shift+Enter**. This array formula also excludes blanks or cells containing text.

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

*ExcelTips* is your source for cost-effective Microsoft Excel training.
This tip (7729) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: **An Average that Excludes Zero Values**.

**Save Time and Supercharge Excel!** Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out *Excel 2010 VBA and Macros* today!

Excel is often used to analyze data collected over time. In doing the analysis, you may want to only look at data ...

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 MoreNeed 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."

2019-09-30 11:25:51

Roy

Either I greatly misunderstand your desire, or there is a simple solution. Just SUMPRODUCT() the two columns and div ide by the SUM() of the first column. (Multiply each pairing of shares purchased by their purchase price, then divide by the total number of shares considered.)

Price blanks take care of themselves as their product is $0. Share blanks do the same. Naturally, if there is an error in the data, like 100 shares at $0 instead of some real price, well, those errors destroy any other approaches as well. You might wish to put in checks on the possiblities.

It really doesn't seem more complex than that. Same basic thing for any method of choosing some vs. all of the five (or 5,000 if desired) pairings.

2019-04-27 08:55:19

2019-04-26 19:44:41

Peter

Pablo,

Here is a formula version using the range from C9 to C27 so 10 values. just change the row numbers. Notice that the count in the combined formula is enclosed in brackets

(see Figure 1 below)

**Figure 1.** Formulas

2019-04-25 20:08:04

Peter Atherton

Here is a shorter version, no need to have a range.

Function AvgNonContig2(ParamArray args() As Variant) As Variant

Dim mysum As Double, counter As Integer

Dim i As Long

For i = 0 To UBound(args)

If IsError(args(i)) Then

AvgNonContig2 = args(i)

Exit Function

End If

If IsNumeric(args(i)) And args(i) > 0 Then

mysum = mysum + args(i)

counter = counter + 1

End If

Next i

AvgNonContig2 = mysum / counter

End Function

Entered in the same way as before.

2019-04-25 19:03:50

Peter Atherton

If there is no formula answer to this you migth linke this UDF

Function AvgNonContig(ParamArray args() As Variant) As Double

Dim c As Range, mysum As Double, counter As Integer

Dim i As Long, tmpRng As Range

For i = 0 To UBound(args)

Select Case TypeName(args(i))

Case "Range"

Set tmpRng = Intersect(args(i).Parent.UsedRange, args(i))

For Each c In tmpRng

mysum = mysum + c

If c > 0 Then

counter = counter + 1

End If

Next

End Select

Next i

AvgNonContig = mysum / counter

End Function

Enter is as

=AvgNonContig(C9, C17, C25, C33, C41, C49, C57, C65, C73, C81)

HTH

Peter

2019-04-24 10:32:28

PABLO RODE

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

or

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

These both would not work because of the non-consecutive cells that I am using in a column for the data.

2018-10-10 09:40:03

Ismail

Thank you!!!! This solved my formula problem!!!!

2018-06-16 18:56:37

A B

# of shares bought Price

100 14.25

200 14.75

500 13.22

blank blank

blank blank

If there were only three entries allowed and all three entries for the # of shares bought and price had to be filled in the formula would be the following:

=SUMPRODUCT( A2:A4, B2:B4 ) / SUM( B2:B4 )

But, if I want to allow for up to 5 buys and sometimes only needed 3 buys how do I not count the remaining 2 buys if those cells are blank?

Apparently, there is no SUMPRODUCTIFS to use as

=SUMPRODUCTIFS( A2:A6, A2:A6,">0",A2:A6,"<>""",B2:B6,B2:B6,">0",B2:B6,"<>""") / SUM( B2:B6,B2:B6,">0",B2:B6,"<>""")

Getting the #NAME? for this....

Any thoughts???

2018-03-14 14:20:36

Lance

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

or

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

These both would not work because of the non-consecutive cells that I am using in a column for the data.

2017-12-14 11:44:00

Flux Capacitor

2017-10-28 07:42:07

Elliot Penna

=AVERAGEIF([range],">0") or =AVERAGEIF([range],"<>0")

No coding calisthenics required! At least not with my version of Excel (2010).

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 © 2021 Sharon Parq Associates, Inc.

## Comments