Averaging without Hidden Cells

by Allen Wyatt
(last updated October 18, 2014)

8

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.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Using Large Toolbar Buttons

Having trouble seeing the icons used on the various Word toolbars? You can make the tools larger (and easier to see) by using ...

Discover More

Printing without Opening

Want to print one ore more workbooks without the need of actually opening the file? It's easy to do when you rely on Windows ...

Discover More

Stepping Through a Macro with a Worksheet Visible

When developing a macro, it is often necessary to step through the various code lines so you can see what is happening on the ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Excluding Values from Averaging

Calculating an average of a group of numbers is easy. What if you want to exclude a couple of the numbers from the group you ...

Discover More

Averaging Values for a Given Month and Year

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

Discover More

Averaging the Last Numbers in a Column

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

Discover More
Subscribe

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

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 8Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 - 4?

2014-11-23 08:22:29

Bill Vernola

Hi Allen - Thanks for the information!!
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

Don

@Willy .... Thanks, however a lot of credit goes to Alan and his site. He introduced me to UNION and INTERSECT.

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

Willy, smaller macros aren't necessarily better. IMO Allen's example might be considered more intuitive, especially for those not as familiar with VBA.


2014-09-29 08:23:31

Kelly

I turn my data into a Table, then use the drop down arrows in the headers to hide the data I don't want.

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

@Don
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

Don

Alan...Sorry I got my email to you too late for this solution.

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

Here is a much smaller macro that does the job equally well:

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)


This Site

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.

Newest Tips
Subscribe

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

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.