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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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,214,641
Messages
6,120,694
Members
448,979
Latest member
DET4492

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