2014-02-19 90 views
0

我正在嘗試對高亮顯示的所有行進行計數(不隱藏)。我的計數公式有效,但它仍在計算隱藏的行,這些行也被隱藏起來。我怎樣才能只計算突出顯示和可見行?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 
+4

檢查'rngCell.RowHeight'> 0 –

+0

@simoco在小區i要計算formla,我會做「= COUNTCELLCOLORSIF(A1:A10)」 – thedeepfield

+0

@TimWilliams,謝謝你做到了! – thedeepfield

回答

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

嘗試是這樣的:

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