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

时间:2014-02-19 20:53:31

标签: excel vba hidden visible

我正在尝试对突出显示且可见(未隐藏)的所有行进行计数。我的计数公式有效,但它仍在计算隐藏的行,这些行也恰好被隐藏了。如何只计算突出显示的行和可见行?

'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

2 个答案:

答案 0 :(得分:1)

使用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

答案 1 :(得分:1)

尝试这样的事情:

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