循环通过Cols&使用IF语句vba的行

时间:2017-03-29 12:20:10

标签: vba excel-vba for-loop excel

全部,

我编写了以下代码来检查变量范围内的单元格是否具有条件格式。然而,代码落在"如果Cells.ColorIndex = 3那么"任何人都可以建议为什么错误发生,如果有一个比下面的代码更好的解决方案来实现循环通过cols&行(可变长度)

Sub Check_Conditional()

Dim rng As Range
Dim row As Range
Dim cell As Range

Dim RW As Long
RW = ActiveSheet.Range("Total").Offset(rowOffset:=-1).row
Set rng = Range("O7:AB" & RW)

For Each row In rng.Rows
    For Each cell In row.Cells
        If Cells.ColorIndex = 3 Then
            MsgBox "Not all the cells have been filled out"
            Exit For
        End If
    Next cell
Next row

End Sub

1 个答案:

答案 0 :(得分:0)

cell.ColorIndex不是有效的Range属性。

如果您要检查字体的颜色,请使用If cell.Font.ColorIndex = 3 Then

如果您要检查填充颜色,请使用If cell.Interior.ColorIndex = 3 Then

当您在编辑器中输入Cell.时,VBA会使用以下选项自动填充它:

enter image description here

列表中没有cell.ColorIndex

enter image description here