VBA:如何忽略范围内的隐藏行?

我正在尝试做一个突出显示,并可见(不隐藏)的所有行的计数。 我的计数公式的作品,但它仍然计算隐藏行也隐藏。 我怎样才能只计算突出显示和可见的行?

'This function will count how many cells in a given range for a given color and are visible Function COUNTCELLCOLORSIF(CellRange As Range) As Long Dim rngCell Application.Volatile For Each rngCell In CellRange If rngCell.Interior.ColorIndex = "36" and rngCell.visible Then COUNTCELLCOLORSIF = COUNTCELLCOLORSIF + 1 End If Next rngCell End Function 

使用specialcells(xlcelltypevisible)

 Function COUNTCELLCOLORSIF(CellRange As Range) As Long Dim rngCell Application.Volatile For Each rngCell In CellRange.specialcells(xlcelltypevisible) If rngCell.Interior.ColorIndex = "36" Then COUNTCELLCOLORSIF = COUNTCELLCOLORSIF + 1 End If Next rngCell End Function 

尝试这样的事情:

 Function COUNTCELLCOLORSIF(CellRange As Range) As Long Dim rngCell, visibleCells Application.Volatile visibleCells = CellRange.SpecialCells(xlCellTypeVisible) For Each rngCell In visibleCells If rngCell.Interior.ColorIndex = "36" and rngCell.visible Then COUNTCELLCOLORSIF = COUNTCELLCOLORSIF + 1 End If Next rngCell End Function