Averaging without Hidden Cells

by Allen Wyatt
(last updated November 21, 2019)


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:


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:


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:


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
    If iCount > 0 Then
        AverageVisible = dTtl / iCount
        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:


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.


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 Excel in Office 365.

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


Hiding Table Gridlines, by Default

The edges to table cells are shown two ways in Word: gridlines and borders. Table gridlines are only seen in Word; they ...

Discover More

Creating a Split Page

In WordPerfect terminology, a split page allows you to put information side-by-side on opposite halves of the page. If ...

Discover More

Using an Exact Number of Digits

Excel allows you to format numeric data in all sorts of ways, but specifying a number of digits independent of the ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies 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 ...

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

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

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

View most recent newsletter.


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 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 eight more than 5?

2017-10-05 10:51:21


Do you have anything for an AverageIF for Visible Cells only? I've been able to use a CountIF but no AverageIF with Visible Cells only. I'm putting a report together that changes these values when the user filters. Thanks~!

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


@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


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


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

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


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())
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
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
AverageVisible = IIf(iCount, dTtl / iCount, 0)
End Function

2014-09-27 09:49:16



For the formula, can't you just use:


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

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.