The Excel veterans out there probably already know about the SubTotal function, which contains a parameter that allows you to include or exclude hidden rows. This works well as a simple SUM (or count, average, max, min, etc) when working on filtered sets, but does not allow you to include any additional conditions.

In the end we opted for the power-user's approach: writing a custom function to provide the missing logic. This is the function: SumIF_X
Function SumIF_X(rngCriteria As Range, _
rngCompare As Range, _
rngValues As Range) As Variant
'new version of the SumIf function
'that ignores hidden rows!
Dim intRow As Long
Dim rng As Range
Dim cTemp As Variant
Dim intTop As Long
Dim strTemp As String
On Error Resume Next
intTop = rngCriteria.Row
strTemp = rngCompare
For Each rng In rngCriteria
If rng = strTemp Then
If rng.RowHeight > 0 Then
cTemp = cTemp + rngValues.Cells(rng.Row - intTop + 1)
End If
End If
Next
Set rng = Nothing
SumIF_X = cTemp
End Function
It is designed to mimic the SumIF function, so the parameters are the same: A criteria range, a compare value and a data range. Using the For Each construct, we cycle through all the cells in the criteria range, and for any that match the compare value (and who's rowheight is not zero,) we add the associated data element to our running total.
It works fine, but there's a potential danger in using this approach: if the data set is really large (thousands of rows), recalculation time for the worksheet will be impacted due to the looping nature of the code. In our application it was not deemed a show-stopper, but for larger workbooks, or when the custom function is being used in thousands of cells, we would probably seek out a leaner approach (or bite the bullet and set up a pivot table!)
No comments:
Post a Comment