Excel SpecialCells

时间:2015-11-08 00:27:25

标签: excel excel-vba vba

对我来说101课。尝试计算数据透视表中的颜色。下面是计算所有颜色,包括过滤掉的颜色。

有没有办法只统计正在显示的行?

此方法无效:

For Each TCell In CountRange.SpecialCells(xlCellTypeVisible)

问题:正在计算该标题的已过滤行。

    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

1 个答案:

答案 0 :(得分:0)

我不是SpecialCells的忠实粉丝,因为它们存在一些更新困难。因此,在测试隐藏行时,我会去旧学校,如下面的代码所示:

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