+ Reply to Thread
Results 1 to 9 of 9

Exclude Rows that are hidden from SUM

Hybrid View

  1. #1
    Registered User
    Join Date
    01-19-2007
    Posts
    1

    Exclude Rows that are hidden from SUM

    Does anyone know how to exclude a hidden row with a value from the sum of multiple rows?

  2. #2
    Forum Contributor
    Join Date
    11-29-2003
    Posts
    1,203
    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.

  3. #3
    Forum Contributor
    Join Date
    11-29-2003
    Posts
    1,203
    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:
    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
    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.

  4. #4
    Forum Contributor
    Join Date
    11-29-2003
    Posts
    1,203

    This is rather awkward, but it works

    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.

    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
    Running low on brain power. Might not come up with anything better today.

  5. #5
    Forum Contributor
    Join Date
    03-13-2005
    Posts
    6,195
    Quote Originally Posted by markdc01
    Does anyone know how to exclude a hidden row with a value from the sum of multiple rows?
    Hi,

    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.

  6. #6
    Forum Contributor
    Join Date
    11-29-2003
    Posts
    1,203
    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:
    SUBTOTAL will ignore any hidden rows that result from a list being filtered.
    (emphasis added)

    I am using Excel 2002. Is "109" something new in Excel 2003 or 2007?

  7. #7
    Forum Contributor
    Join Date
    11-29-2003
    Posts
    1,203
    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).

  8. #8
    Forum Contributor
    Join Date
    03-13-2005
    Posts
    6,195
    Quote Originally Posted by MSP77079
    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?).

    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.

  9. #9
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    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 !!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1