Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Summing Only Visible Values.
by Allen Wyatt
(last updated February 25, 2017)
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:
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 this 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:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12123) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Summing Only Visible Values.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
Need your macro to get some input from a user? The standard way to do this is with the InputBox function, described in this ...Discover More
When your macro is humming along, minding its own business, a user watching the screen may not see any activity and therefore ...Discover More
Want a quick way to convert positive values to negative and vice versa? You can create your own plus/minus button by using a ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.