循环计数颜色

时间:2018-09-27 21:35:30

标签: vba loops

下面是一些简单的代码。我想我在循环中引用和循环时遇到麻烦。

我想计算一行中的绿色单元格的数量,然后移至下一行。我在这里遇到错误:

If RowRange.DisplayFormat.Interior.colour = SourceColour.DisplayFormat.Interior.colour Then

运行时错误91-未设置对象。...

有什么想法吗?

完整代码:

Sub countcolourloop1()

Dim rng As Range
Dim RowRange As Range
Dim SourceColour As Range
Dim xBackColour As Integer

'count green cells
Set SourceColour = ActiveWorkbook.ActiveSheet.Range("BN2:BN2")

    'cyle through each row, add 1 to the counter xBackColour every time you find a cell in the range matching the SourceColour
    For I = 4 To 300
        Set RowRange = ActiveWorkbook.ActiveSheet.Range("B" & I & ":BK" & I)
        If RowRange.DisplayFormat.Interior.colour = SourceColour.DisplayFormat.Interior.colour Then
        xBackColour = xBackColour + 1
        End If
    Next

End Sub

0 个答案:

没有答案