Susie has a worksheet that has quite a bit of data in it. It is not unusual for her to hide columns within the worksheet. She needs a formula that will return the average of a range of cells, all in the same row, while ignoring any hidden cells. For instance, if Susie selects B7:G7, she would like an average of only the cells in that range that are visible (columns D and E, in this case, are hidden).

There is no intrinsic function in Excel to calculate such an average. If you were trying to find the average of a range of cells in a column, then you could use the SUBTOTAL function, in this manner:

=SUBTOTAL(101,A7:A12)

The SUBTOTAL function won't return an accurate value when the range provided is within a row, however. You could, if desired, use some helper cells to do the calculation. Just put the following into cell B8:

=(CELL("width",B7)>0)+0

Copy cell B8 to the range C8:G8. The result is that each cell in the range B8:G8 will contain either a 0 or a 1 based on whether the column is hidden or not. You could then use the following formula to determine the average:

=SUMIFS(B7:G7,B8:G8,">0")/SUM(B8:G8)

It calculates the average only for those columns in which the cells in B8:G8 contain a value of 1 (they aren't hidden).

Of course, you may not be able to use the helper cells and you might find it more beneficial to create a user-defined function to do calculate the average. The following works very quickly:

Function AverageVisible(rng As Range) Dim rCell As Range Dim iCount As Integer Dim dTtl As Double iCount = 0 dTtl = 0 For Each rCell In rng If rCell.ColumnWidth > 0 _ And rCell.RowHeight > 0 _ And Not IsEmpty(rCell) _ And IsNumeric(rCell.Value) Then dTtl = dTtl + rCell iCount = iCount + 1 End If Next If iCount > 0 Then AverageVisible = dTtl / iCount Else AverageVisible = 0 End If End Function

In order to use the function, simply put this formula in the cell you want to contain the average:

=AverageVisible(B7:G7)

The function checks every cell in the range (which means you could use it on rows, columns, or, indeed, any range at all) to make sure it is not hidden and not empty. If the cell contains a numeric value, then it is used to calculate the average. If the range you specify contains no values that can be averaged, then the function returns a value of 0.

The function automatically runs everytime the worksheet is recalculated. If you change which columns are hidden, Excel doesn't automatically recalculate. Thus, you'll need to force recalculation after hiding or unhiding columns.

*ExcelTips* is your source for cost-effective Microsoft Excel training.
This tip (13262) applies to Microsoft Excel 2007, 2010, and 2013.

**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!

Need to calculate a running average for the last twelve values in a constantly changing range of values? The formula ...

Discover MoreExcel allows you to use functions and formulas to analyze your data. One way you can analyze your data is to use the AVERAGE ...

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

Discover More**FREE SERVICE:** Get tips like this every week in *ExcelTips,* a free productivity newsletter. Enter your address and click "Subscribe."

2017-10-05 10:51:21

Hoosierdaddy

2014-11-23 08:22:29

Bill Vernola

I have a question about the AverageVisible function. I copied and pasted it into my VBA code window, but I found that it didn't adjust the function result when I hid columns or rows, even after recalculating. I'm new to this VBA so hopefully you can straighten me out. Thnx

2014-09-29 09:10:03

To be honest, my first version looked much like what Alan published from others' suggestions.

As an exercise, I started duplicating the other SUBTOTAL functions. When I had trouble with VAR/P & StDev/P I realized this was an opportunity to use the Worksheet Functions for flexibility and consistency.

2014-09-29 09:04:46

Bill

2014-09-29 08:23:31

Kelly

Then I use my mouse to select all the data I want to average and simply look in the status bar at the bottom for that the average says. Or go to the bottom of the data and add the average into a cell.

I don't understand macro's so this approach works for me (and the data I'm using).

2014-09-28 11:14:47

Willy Vanhaelen

Compared to the clumsy macro presented in this tip, your approuch using the Union function together with Excel's Average function is a gem.

2014-09-27 15:24:18

I took a different approach, building a range consisting of the cells in visible columns then using the Average() worksheet function to provide the function's value.

There are several advantages

(1) Quickly adaptable for other functions.

(2) Results more consistent with Worksheet functions, e.g. instead of ")" returns "#DIV/0!" if nothing to average. (3) Allows for multiple ranges and controls for double-counting if ranges overlap.

(4) Works when rows and/or columns are hidden.

(5) Adding a SELECT CASE...END CASE could extend it to the other 10 SUBTOTAL() function.

Function AveUnhiddenCells(ParamArray rngSource())

Application.Volatile

Dim rngCell As Range

Dim rngVisible As Range

Dim dblCtr As Double

For dblCtr = LBound(rngSource) To UBound(rngSource)

For Each rngCell In rngSource(dblCtr).Cells

If Not (rngCell.Columns.Hidden Or rngCell.Rows.Hidden) Then

If rngVisible Is Nothing Then

Set rngVisible = rngCell

Else

Set rngVisible = Union(rngVisible, rngCell)

End If

End If

Next rngCell

Next dblCtr

AveUnhiddenCells= WorksheetFunction.Average(rngVisible)

End Function

2014-09-27 11:38:30

Willy Vanhaelen

Function AverageVisible(rng As Range)

Dim rCell As Range, dTtl As Double, iCount As Integer

For Each rCell In rng

If Not rCell.Columns.Hidden Then

If IsNumeric(rCell) And Not IsEmpty(rCell) Then

iCount = iCount + 1

dTtl = dTtl + rCell

End If

End If

Next

AverageVisible = IIf(iCount, dTtl / iCount, 0)

End Function

2014-09-27 09:49:16

Dean

Hello,

For the formula, can't you just use:

=AVERAGEIFS(B7:G7,B8:G8,1)

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

## Comments