Summing, Counting, or Averaging visible cells only in Excel

Often when you use tools like filter, grouping or hiding rows in Excel you want to be able to do the summing, counting or averaging of the visible cells only. Alternatively perhaps you just want the total of cells even if there are errors in some of the cells. You want Excel to ignore the cells with errors.

As shown below, the totals show everything and if there is an error in the cells, the total is lost.

Summing Counting or Averaging visible cells only

If you filter on the results (say all the 1st years only), the total you see is still adding up all the hidden cells.

Summing Counting or Averaging visible cells only

There are a couple of ways to do this but in the newer versions of Excel there is a great function called AGGREGATE.

Excel’s AGGREGATE Function

The aggregate function can be used to

  • average,
  • count (in 2 ways),
  • find the maximum or minimum,
  • determine the product,
  • sum,
  • obtain the median,
  • extract the largest or smallest, and
  • other statistical features

where you want Excel to:

  • Ignore hidden rows and error values, or
  • Ignore hidden rows, or
  • Ignore error values, or
  • Ignore nested SUBTOTAL and AGGREGATE functions
  • and several other options.

So in the example below, note that the total in G shows the total of all the rows above, and column F has an error message due to some errors in the cells (but note that there are some cells with numbers which we would want to add up)

Summing Counting or Averaging visible cells only

Also, note that when we filter the rows to only 1st years, the totals still stay the same. You run the risk of assuming that the total is for those few cells.

Summing Counting or Averaging visible cells only

In the cell below we can use the AGGREGATE function.

When you click in the cell and type ‘=AGGREGATE(‘ you will see the following dropdown option. You can choose the one you want to use, either by typing the number or clicking on it.

Summing Counting or Averaging visible cells only

Once you have chosen and typed the comma, you then get to decide what Excel must do. There are several options which allow you to ignore errors, hidden cells and certain functions (SUBTOTAL, AGGREGATES) etc. Again you can either type the appropriate number or click on the one you want.

Summing Counting or Averaging visible cells only

The last step is just to tell Excel where to look, in much the same way as with a normal SUM or AVERAGE.

Summing Counting or Averaging visible cells only

Now notice when we filter on the 1st years, the one total gives us all the cells (visible and hidden) whereas the AGGREGATE total only adds the visible cells.

In column F the AGGREGATE function ignores the hidden cells AND the error cells and gives us an answer for what we see.

Summing Counting or Averaging visible cells only