Written by Allen Wyatt (last updated August 18, 2022)
This tip applies to Excel 97, 2000, 2002, and 2003
Kirk is using the SUM function in many of his worksheets to (naturally) determine the sum of a range of values. The problem he is running into, however, is that the range he is summing contains some hidden rows, and he doesn't want those values—the hidden ones—included in the sum.
The SUM function is pretty simplistic in how it does its work; it simply sums a range. You can change the function you use and get the desired results, however. For instance, let's assume that you want to sum the range of A3:A45, and that you don't want any hidden values to be included in the sum. You should use the SUBTOTAL function in the following manner:
=SUBTOTAL(109,A3:A45)
The first parameter of the function (109) indicates how you want SUBTOTAL to do its work. In this case, it means you want SUBTOTAL to sum the range, using the SUM function, and you don't want any hidden values included in the value returned. (You can find out more about the controlling SUBTOTAL parameters if you look in the online Help for the SUBTOTAL function.)
If you don't want to use the SUBTOTAL function for some reason, you can create your own user-defined function (a macro) that will only sum the visible values in a range. Consider the following macro:
Function Sum_Visible(Cells_To_Sum As Object) Dim vTotal As Variant Application.Volatile vTotal = 0 For Each cell In Cells_To_Sum If Not cell.Rows.Hidden Then If Not cell.Columns.Hidden Then vTotal = vTotal + cell.Value End If End If Next Sum_Visible = vTotal End Function
To use the function, simply use a formula like this wherever you want your sum to appear:
=Sum_Visible(A1:A1000)
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3082) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Summing Only Visible Values.
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!
The MODE function is used to determine the most frequently recurring value in a range. This tip explains how to use the ...
Discover MoreWant to add up a bunch of scores, without including the lowest one in the bunch? You can make a small change to your ...
Discover MoreIf you want to round a value to some multiple of a whole number, you'll want to become familiar with the MROUND function. ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2019-08-07 11:15:00
Willy Vanhaelen
Instead oi ths rather complicated macro with a doube loop you can use this very simple one-liner which is simply the VBA implementation of the formula in this tip.-:
Function SumVisible(CellsToSum As Range)
SumVisible = Application.Subtotal(109, CellsToSum)
End Function
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments