How to colorcode cells based on conditions?

74 Views Asked by At

I am trying to get this loop to repeat down both rows from O14:P434. I want it to run for the entire range but only apply the coloration if there is a value in column P.

For loopctr = 14 To 434
Dim gooddate As String
goodate = "O14"
Dim baddate As String
baddate = "P14"
If baddate > gooddate Then
Range("P14").Select
With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent6
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With


End If

Next

The loop I have doesn't work, how do I make it run all the way down those rows. I got it to work through conditional formatting and recording the macro of creating it.

4

There are 4 best solutions below

0
pnuts On BEST ANSWER

For equivalent data and results to @GMalc's A, the following CF formula rules work:

Red: =C1>B1, Green: =AND(C1<>"",C1<B1)

if applied to ColumnC. Details of how to apply CF here.

1
Luck On

It sounds like you simply need to use Conditional Formatting to set a Highlight Cells Rule to highlight the cells that are Greater Than or Less Than. Or you can set a more complex rule, using a formula, by selecting More Rules and then "Use a formula to determine which cells to format."

Set up your rules on the first cell of the Reschedule Date column (P14) comparing it to the first cell of the Receipt column (O14), and if you're happy with the results use the Format Painter to copy the formatting down the rest of the cells of the Reschedule Date column.

You'll need two rules. Here are screenshots on how to set them up on cell P14: enter image description here

enter image description here

enter image description here

The end result should look like this once the formatting is painted down to all of the cells:

0
David Podolak On

Hopefully I am understanding your question correctly. You don't need variables but if you want them you need to create them outside of the loop. In this code x is the row that continues to change with each loop and 15, 16 are column O & P.

For x = 14 To 434

   If CDate(Cells(x,16).Value) > CDate(Cells(x,15).Value) Then

       With Cells(x,16).Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent6
       End With

   End If

Next x
0
GMalc On

Here is a basic VBA solution; with your column headers in row 1, and there are possible blank cells in the column; this macro will compare the dates in the Reschedule date column with the dates in the column to the left. If the date is newer then the date in the left column, it will color the cell Red. If the date is older then the date in the left column, it will color the cell Green. See attached picture...

Dim tCol As Long, cel As Range

    With Worksheets("Sheet2")
    'use find to identify the column number
    tCol = .Rows(1).Find("Reschedule date", , xlFormulas, xlWhole, xlByRows, xlPrevious).Column

        'loop through each cell in the column from row to the last used row
        For Each cel In .Range(.Cells(2, tCol), .Cells(.Rows.Count, tCol).End(xlUp))
            'test each cel; if not empty and the cel value is less then the
            'value of the cell on the left, then color the cel green
            If cel.Value <> "" And cel.Value < cel.Offset(, -1).Value Then
                cel.Interior.ColorIndex = 4

            'elseif test each cel; if not empty and the cel value is greater then the
            'value of the cell on the left, then color the cel red
            ElseIf cel.Value <> "" And cel.Value > cel.Offset(-1).Value Then
                cel.Interior.ColorIndex = 3

            End If

        Next cel 'loop to the next cel
    End With

enter image description here