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 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:
=Sum_Visible(A1:A1000)
Note:
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.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
Macros that run automatically when you open or close a workbook are quite helpful. You may not want them to run, however, ...
Discover MoreEver want to have Excel run a procedure whenever you open a workbook? It's not as difficult as you might think. Here's how.
Discover MoreWhen opening a workbook, you may want to make sure that a particular worksheet is always displayed first. The only way to ...
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:27:21
Willy Vanhaelen
Instead oi ths rather complicated macro with a double 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
2019-02-22 21:02:55
Bo
Hi.
I just wanted to thank you - this helped me optimise my program.
With kind regards,
Bogdan
2015-08-27 15:13:06
Ken Sadeckas
Another alternative is to:
1. Highlight the range to sum
2. Press F5 (to open the Go To menu)
3. Select Special
4. Choose "Visible cells only"
5. Enter your formula using the Sum function and highlighting the range again
The result is =SUBTOTAL(9,XXX:YYY) which only includes visible cells.
2015-08-27 07:19:49
Nancy Harpe
Sometimes the easiest formulas save the most time! I agree with JoyM, I wish I knew this years ago. It's a keeper! Thanks Allen!
2015-03-10 02:21:21
Bishnu
Dear sir,
=Sum_Visible(A1:A1000)
The above formula is not working with Excel 2007.
I hope your great replay.
Thank you.
2012-09-18 09:40:33
JanL
I generally format my data in a table and use the Total Row function. Anytime you filter data, the subtotal is automatically changed. It really is a lifesaver!
2012-09-17 08:20:44
JoyM
I cannot count the number of times this would have come in handy over ther past 10 years!!
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2021 Sharon Parq Associates, Inc.
Comments