Home » excel » excel – If Not (range) Or (range) = Nothing Then

excel – If Not (range) Or (range) = Nothing Then

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am working on a macro to highlight certain rows/cells for further review. Some values/blanks are to be highlighted in red, and others in orange, while the entire row should be yellow. I have gotten some help from a previous question and added a lot more detail, it is working almost perfectly but I’m stuck on this one last thing and can’t find any similar questions asked. I would like the designated cells to be orange and red on the same yellow row. I can achieve orange OR red, but when it works through the next check it re-highlights the row in yellow – losing the previous color.

For example, column A must be red if blank and column B must be orange if populated with ‘No’. All of my checks are working if I use the same color, but fail when I set it to use both.

The code I have uses:

If Not rngRed Is Nothing Then
rngRow.Interior.Color=vbYellow
rngRed.Interior.Color=vbRed

I can’t figure out a way to work orange (color index 45) into that mix without bugging. Let me know if you need more code than the small section I’ve provided or have any other ideas for how to accomplish this. Thank you!!!!

this works:

    If Not rngRed Is Nothing Then
    rngRow.Interior.Color=vbYellow
    rngRed.Interior.Color=vbRed

this does not work:

        If Not rngRed Or rngOrange = Nothing Then
            rngRow.Interior.Color = vbYellow
            rngRed.Interior.Color = vbRed
            rngOrange.Interior.ColorIndex = 45
        End If
How to&Answers:

Based on rereading your question, it sounds like you are looking to do the following:

1) Color the row yellow.

2) Color rngRed if it is not nothing.

3) Color rngOrange if it is not nothing.

So like this:

rngRow.Interior.Color = vbYellow

If Not rngRed Is Nothing Then
    rngRed.Interior.Color = vbRed
End If

If Not rngOrange Is Nothing Then
    rngOrange.Interior.ColorIndex = 45
End If