简体   繁体   中英

Excel VBA Conditional Formatting Error

I am using the following code to format the Cell.Interior.Color and Cell.Font.Color of certain cells which fullfill a in Tabelle4.Cells("B2") pre-definied criteria:

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

However my condition line is not working out, but no error shown:

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

Does someone know whats going on here and probably how to fix it?

This is how the code would look like, if you simplify it as much as possible:

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

Now, if you enter something in the Range A1:E5 of the ActiveSheet , it will become green with green background.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM