Home » excel » vba – Excel-VB read cell color

vba – Excel-VB read cell color

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m wondering why my code is not reading the cell value correct.
The point is if excel marks a cell red and when a user pushes a validation button.
the macro gives a message depending on the cell color.

At the moment i’m testing this on “c4” but if i put it on red (255,0,0) it doesnt read it as “Red”

  Sub Validate()
        Dim xRng As Range
        Dim ws As Worksheet
        Set ws = Sheets("TEMPLATE")
        Set xRng = Range("A2:N1000")

        If ws.Range("C4").Interior.Color = RGB(255, 0, 0) Then
            MsgBox "Found some errors, please review your template", vbOKOnly + vbCritical, "Validation"
        Else
            MsgBox "No direct errors found!", vbOKOnly + vbQuestion, "Validation"
        End If

    End Sub
How to&Answers:

If the cell was colored by a conditional format you need to check it like that

If ws.Range("C4").DisplayFormat.Interior.Color = RGB(255, 0, 0) Then
    ' Your code here
Else
    ' Your code here
End If