[英]Count number of colored cells in range using VBA
我有一個計算彩色單元格數量的代碼。 你能建議如何擴大范圍嗎? 基本上我需要計算彩色單元格列范圍,如(“A:Z”)和 nRowIndex = 2 到 100?
Sub CountCellsWithBackgroundColor()
Dim nRowIndex As Integer, nCellNumber As Integer
'Go through the range
Worksheets("Report_Rule_S").Activate
For nRowIndex = 2 To 100
If Range("E" & nRowIndex).Interior.ColorIndex <> -4142 Then 'need to do somthing like Range("A:Z")
nCellNumber = nCellNumber + 1
End If
Next nRowIndex
' Output the result
Worksheets("cover").Range("B12") = nCellNumber
End Sub
您可以將所有這些放在一個 For-Each 循環中:
Dim aCell As Range
Dim cellNumber As Integer
cellNumber = 0
For Each aCell In Range("A2:Z100").Cells:
If aCell.Interior.ColorIndex <> -4142 Then
cellNumber = cellNumber + 1
End If
Next
我在下面的評論
Sub CountCellsWithBackgroundColor()
Dim nRowIndex As Integer, nCellNumber As Integer
'Go through the range
Worksheets("Report_Rule_S").Activate
For Each cl In Worksheets("Report_Rule_S").UsedRange.SpecialCells(xlCellTypeVisible).Cells
If cl.Interior.ColorIndex <> -4142 Then
nCellNumber = nCellNumber + 1
End If
Next cl
'Output the result
Worksheets("cover").Range("B12") = nCellNumber
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.