101 class for me. Attempting to count colors in a Pivot table. Down below is counting all colors including the ones filtered out.
Is there a way to only count the rows being displayed?
This method is not working:
For Each TCell In CountRange.SpecialCells(xlCellTypeVisible)
Problem: The filtered out rows for that heading are being counted.
Function CountByColor(CellColor As Range, CountRange As Range)
Application.Volatile
Dim ICol As Integer
Dim TCell As Range
ICol = CellColor.Interior.ColorIndex
For Each TCell In CountRange.SpecialCells(xlCellTypeVisible)
If ICol = TCell.Interior.ColorIndex Then
CountByColor = CountByColor + 1
End If
Next TCell
End Function
I'm not a big fan of SpecialCells
as there are some updating difficulties with them. As a result, I go old school when it comes to testing for hidden rows, as in the code below:
Function CountByColor(CellColor As Range, CountRange As Range)
Application.Volatile
Dim ICol As Integer
Dim TCell As Range
ICol = CellColor.Interior.ColorIndex
For Each TCell In CountRange.Cells
If Not TCell.EntireRow.Hidden And ICol = TCell.Interior.ColorIndex Then
CountByColor = CountByColor + 1
End If
Next
End Function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.