Does anyone know how to exclude a hidden row with a value from the sum of multiple rows?
Does anyone know how to exclude a hidden row with a value from the sum of multiple rows?
One option is to use AutoFilter. When you autofilter a range and use the summation key, it automatically sets up a subtotal formula, and this gives you the sum only for visible rows.
Now, this is kind of odd ... I was thinking you could get what you want in a more general case (i.e., a range that is not easy to autofilter to get what you want) using a User-Defined-Function.
This macro works:
But, converting this code to a function and calling it from a cell DOES NOT work. Don't know why yet, but it does not work.![]()
Sub testme() Dim rng As Range Dim mySum As Variant Set rng = Range("B3:D24").Cells.SpecialCells(xlCellTypeVisible) mySum = 0 On Error Resume Next mySum = Application.WorksheetFunction.Sum(rng) MsgBox mySum End Sub
Still puzzled why the macro above does not work as a User-Defined-Function (UDF). The problem seems to be in the very first line of code. The visible cells collection seems to lose its mind in a UDF.
Here is something that will work as as UDF. So, if you put this code into a Module, then in a cell you put the formula (for example)
=visibleSum(B3:D22)
it will return the correct answer as you hide and unhide rows.
Running low on brain power. Might not come up with anything better today.![]()
Public Function visibleSum(Target As Range) Dim mySum As Variant Dim c As Range Dim ws As Worksheet On Error GoTo leave visibleSum = CVErr(xlErrNA) Set ws = Target.Parent mySum = 0 For Each c In Target.Cells If Not ws.Rows(c.Row).Hidden Then mySum = mySum + c.Value End If Next c visibleSum = mySum leave: End Function
Hi,Originally Posted by markdc01
alternately you can use
=Subtotal(9, ~~etc
to include hidden rows, or
=Subtotal(109, ~~etc
to exclude hidden rows.
hth
---
Si fractum non sit, noli id reficere.
Bryan, I cannot find any reference to the "109" argument you suggest. When I try using it, I get an error.
The documentation I can find for SUTOTAL says:(emphasis added)SUBTOTAL will ignore any hidden rows that result from a list being filtered.
I am using Excel 2002. Is "109" something new in Excel 2003 or 2007?
Ignore last question. I found the answer here: http://www.contextures.com/xlFunctions01.html
Looks like SUMPRODUCT(109,...) is available starting with Excel 2003 (which I have on my other PC, but not this one).
good to see that you found the answer, thanks also to VBA Noob, I guess after using the option for the past 4 years it seems like it was always there, how quickly we forget the features that were not available in earlier releases (didn't '97 do all of these things?).Originally Posted by MSP77079
added,
also, in relation to your comment "Still puzzled why the macro above does not work as a User-Defined-Function (UDF). The problem seems to be in the very first line of code. The visible cells collection seems to lose its mind in a UDF." is perhaps the wrong way. The 'visible' cells are selected in both cases, however, you are using Application.WorksheetFunction.Sum - and sum includes hidden rows. So I see the question more as 'Why did the subroutine err and not include hidden rows', but admit they should both produce the same answer.
---
Last edited by Bryan Hessey; 01-21-2007 at 07:04 PM.
MSP77079,
Introduced in 2003
http://www.contextures.com/xlFunctions01.html#Filter
VBA Noob
Last edited by VBA Noob; 01-21-2007 at 12:17 PM.
_________________________________________
![]()
![]()
Credo Elvem ipsum etian vivere
_________________________________________
A message for cross posters
Please remember to wrap code.
Forum Rules
Please add to your signature if you found this link helpful. Excel links !!!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks