Conditional Formatting

helpexcel

Well-known Member
Joined
Oct 21, 2009
Messages
656
I'm using this code to try and conditional format the rows with the values shown below. My problem is "Red" is the only color that's working, and i don't know why.


Code:
Dim i As LongDim Lastrow, Lastcolumn As Long
Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    For i = 1 To Lastrow
        Lastcolumn = .Cells(i, .Columns.Count).End(xlToLeft).Column
        If .Cells(i, 13).Value = "Red" Then
            .Range(.Cells(i, 1), .Cells(i, Lastcolumn)).Interior.Color = vbRed
        ElseIf Cells(i, 13).Value = "Green" Then
            .Range(.Cells(i, 1), .Cells(i, Lastcolumn)).Interior.Color = vbGreen
        ElseIf Cells(i, 13).Value = "Yellow" Then
            .Range(.Cells(i, 1), .Cells(i, Lastcolumn)).Interior.Color = vbYellow
        End If
    Next i
End With
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Make sure that the cells with "Green" & "Yellow" are
a) Spelt correctly
b) Are lower case with the exception of the first character
c) Have nothing else in the cell, including leading/trailing spaces.
 
Upvote 0
All good on a,b, and c. It always stops at the first If. So if i switch "red" and "green", then I only get green.
 
Upvote 0
Got it, on both the elseif lines you've missed the . from infront of cells.
So on those lines it's looking at the active sheet.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top