S
Sh
=IF(ISERROR(SUM(B23:M23)/(COUNTIF(B23:M23,">0"))),0,SUM(B23:M23)/(COUNTIF(B23:M23,">0")))
I just wanted to share this for anybody who needed a useful AVERAGE
function that intelligently sums a range of cells but only divides by
the number of cells which contain a value above zero.
This can be useful when calculating running monthly averages over time
in a household account spreadsheet for example.
You will need to change the cell ranges (B23:M23) to suit your own
needs of course...
The built-in AVERAGE function simply adds together the values for a
range of cells and then divides the result by the number of cells in
the range.
This is useful if all of the monthly totals contain valid values, but
if you are only filling in the total values for January and February
then the results of two months divided by 12 is somewhat meaningless.
The heart of this function is:
SUM(B23:M23)/(COUNTIF(B23:M23,">0"))
The SUM part obviously sums the range of cells, the clever part is the
COUNTIF function, which counts up the number of cells that are NOT
zero, and will either return a zero (if all of the cells actually
conatin zero) or a value from one to the maximum number of cells in
the range. This will provide the divisor for the second part of the
average function.
The result of this formula can produce a #DIV0 error if no cells
contain a value above zero, because COUNTIF will return a zero as the
divisor, so I have nested the whole function inside an IF(ISERROR
function to trap the #DIV0 error, which will return a proper zero if
required.
I admit this is somewhat clumsy but it does work, I'm sure a more
experienced EXCEL user could probably find an alternative method or a
tidier method, however my knowledge of Excel is fairly basic and I do
not know how to program any vbscript.
I just wanted to share this for anybody who needed a useful AVERAGE
function that intelligently sums a range of cells but only divides by
the number of cells which contain a value above zero.
This can be useful when calculating running monthly averages over time
in a household account spreadsheet for example.
You will need to change the cell ranges (B23:M23) to suit your own
needs of course...
The built-in AVERAGE function simply adds together the values for a
range of cells and then divides the result by the number of cells in
the range.
This is useful if all of the monthly totals contain valid values, but
if you are only filling in the total values for January and February
then the results of two months divided by 12 is somewhat meaningless.
The heart of this function is:
SUM(B23:M23)/(COUNTIF(B23:M23,">0"))
The SUM part obviously sums the range of cells, the clever part is the
COUNTIF function, which counts up the number of cells that are NOT
zero, and will either return a zero (if all of the cells actually
conatin zero) or a value from one to the maximum number of cells in
the range. This will provide the divisor for the second part of the
average function.
The result of this formula can produce a #DIV0 error if no cells
contain a value above zero, because COUNTIF will return a zero as the
divisor, so I have nested the whole function inside an IF(ISERROR
function to trap the #DIV0 error, which will return a proper zero if
required.
I admit this is somewhat clumsy but it does work, I'm sure a more
experienced EXCEL user could probably find an alternative method or a
tidier method, however my knowledge of Excel is fairly basic and I do
not know how to program any vbscript.