Excel VBA条件格式错误

时间:2018-03-28 13:47:12

标签: excel vba excel-vba conditional-formatting

我使用以下代码格式化满足Cell.Interior.Color预先定义标准的某些单元格的Cell.Font.ColorTabelle4.Cells("B2")

Sub ColorTest1()

Dim lastcell As Long
Dim Cellclr As Long
Dim Fontclr As Long

lastcell = Tabelle3.Range("C1048576").End(xlUp).Row
Cellclr = RGB(232, 245, 246)
Fontclr = RGB(26, 155, 167)


    For Each Cell In Tabelle3.Range(Tabelle3.Cells(9, 3), Tabelle3.Cells(lastcell, 3))

        If Cell <> "" And Cell.Value <= Tabelle4.Cells("B2") Then

            Cell.Interior.Color = Cellclr
            Cell.Font.Color = Fontclr

        End If

    Next Cell

End Sub

但是我的条件行没有用完,但没有显示错误:

If Cell <> "" And Cell.Value <= Tabelle4.Cells("B2") Then

有人知道这里发生了什么,可能是如何修复它?

1 个答案:

答案 0 :(得分:0)

如果您尽可能地简化代码,这就是代码的样子:

Sub ColorTest()

    Dim cellClr     As Long
    Dim fontClr     As Long
    Dim cell        As Range

    cellClr = RGB(232, 245, 246)
    fontClr = RGB(26, 155, 167)

    For Each cell In Range(Cells(1, 1), Cells(5, 5))
        If cell <> "" And cell.Value <= 5 Then
            cell.Interior.Color = cellClr
            cell.Font.Color = fontClr
        End If
    Next cell

End Sub

现在,如果您在A1:E5的范围ActiveSheet中输入内容,它将变为带绿色背景的绿色。