Averaging without Hidden Cells

Written by Allen Wyatt (last updated September 14, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and 2021


1

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 every time 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.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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

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

Determining the Week of the Year

If you are working with dates in a macro, you may need to determine which week of the year a date falls within. This can ...

Discover More

Tabbing from One Table to the Next

Press the Tab key in a table, and Word dutifully moves to the next cell in that table. Press it in the last cell of a ...

Discover More

Keyboard Shortcut for Comments

Adding comments or notes to the cells in your worksheets can help to document different aspects of that worksheet. Adding ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

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

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

Determining a Simple Moving Average

A moving average can be a great way to analyze a series of data points that you've collected over time. Setting up a ...

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}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. 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 0 + 7?

2020-02-01 06:29:06

Alex B

Hello Allen, I don't know in which version of Excel the CELL function changed but in my version CELL("width",B7) uses 2 column the first to return a number and the second to return a TRUE or FALSE (visible or invisible respectively).
If I follow the above and put =(CELL("width",B7)>0)+0 into B8 I get
B8 = 1 and C8 = 1 which means if I try to copy it to C8:G8, I get #SPILL!

The workaround is to use the INDEX function to return only 1 of the 2 values which changes your formula to
=(INDEX(CELL("width",B7),0,1)>0)+0
See: Comment at the bottom or https://stackoverflow.com/questions/28589839/ignoring-a-hidden-column-in-an-excel-sum-formula

You could change the formula to take advantage of the TRUE / FALSE value returned but that will error out in the older versions of excel that as yet don't return 2 values.


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.