Simplify Condition Code

xsitm

New Member
Joined
Jun 16, 2011
Messages
3
I've created a code to color rows based on a condition. However I can't seem to get the coding right to only look at a row if there is data in Column I. Right now it checks every row up until 20,000.

Any help simplifying would be greatly appreciated.


Sub Color()
Dim LRow As Integer<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Dim LCell As String<o:p></o:p>
Dim LColorCells As String<o:p></o:p>
'Start at row 2<o:p></o:p>
LRow = 2<o:p></o:p>
'Update row colors for the first 20000 rows<o:p></o:p>
While LRow < 20000<o:p></o:p>
LCell = "I" & LRow<o:p></o:p>
'Color will changed in columns A to U<o:p></o:p>
LColorCells = "A" & LRow & ":" & "U" & LRow<o:p></o:p>
Select Case Left(Range(LCell).Value, 6)<o:p></o:p>
'Set row color to light purple<o:p></o:p>
Case "A"<o:p></o:p>
Range(LColorCells).Interior.ColorIndex = 7<o:p></o:p>
Range(LColorCells).Interior.Pattern = xlSolid<o:p></o:p>
'Set row color to light yellow<o:p></o:p>
Case "AM"<o:p></o:p>
Rows(LRow & ":" & LRow).Select<o:p></o:p>
Range(LColorCells).Interior.ColorIndex = 44<o:p></o:p>
Range(LColorCells).Interior.Pattern = xlSolid<o:p></o:p>
End Select<o:p></o:p>
LRow = LRow + 1<o:p></o:p>
Wend<o:p></o:p>
Range("A1").Select<o:p></o:p>

End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
what the conditions for changing color
It is difficult to find the conditions form the macro

e.g.
Code:
Select Case Left(Range(LCell).Value, 6)

what does this mean ???
what condition do you want to set up.
 
Upvote 0
I agree with venkat1926

Here's your code cleaned up a bit.

Code:
Sub Color()

    Dim LastRow As Long
    Dim cell As Range

    LastRow = Range("I" & Rows.Count).End(xlUp).Row 'Last used row in column I
    If LastRow > 20000 Then LastRow = 20000
    
    'Update row colors for the first 20000 rows
    For Each cell In Range("I2:I" & LastRow)
        'Color will changed in columns A to U
        With Range("A" & cell.Row).Resize(, 21).Interior 'Columns A to U
            Select Case Left(cell.Value, 6)
                Case "A": .ColorIndex = 7   'light purple
                Case "AM": .ColorIndex = 44 'light yellow
                Case Else: .ColorIndex = xlNone
            End Select
            .Pattern = xlSolid
        End With
    Next cell

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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