简体   繁体   English

如何根据条件对单元格进行颜色编码?

[英]How to colorcode cells based on conditions?

I am trying to get this loop to repeat down both rows from O14:P434. 我正在尝试使此循环重复执行O14:P434的两行。 I want it to run for the entire range but only apply the coloration if there is a value in column P. 我希望它在整个范围内运行,但仅当列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. 我通过条件格式来工作并记录了创建它的宏。

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. 在“重新安排日期”列(P14)的第一个单元格上与“收据”列(O14)的第一个单元格进行比较,设置规则,如果对结果满意,请使用格式刷将其余的格式复制下来重新安排日期列的单元格。

You'll need two rules. 您将需要两个规则。 Here are screenshots on how to set them up on cell P14: 以下是有关如何在单元格P14上进行设置的屏幕截图: 在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

The end result should look like this once the formatting is painted down to all of the cells: 格式化完成后,所有单元格的最终结果应如下所示:

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. 在此代码中,x是随每个循环而继续变化的行,而15、16是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

Here is a basic VBA solution; 这是基本的VBA解决方案; with your column headers in row 1, and there are possible blank cells in the column; 如果您的列标题位于第1行,则该列中可能有空白单元格; this macro will compare the dates in the Reschedule date column with the dates in the column to the left. 此宏会将“ Reschedule date列中的日期与左侧列中的日期进行比较。 If the date is newer then the date in the left column, it will color the cell Red . 如果日期较新,则左栏中的日期将为Red If the date is older then the date in the left column, it will color the cell Green . 如果日期较早,则左栏中的日期将为单元格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

在此处输入图片说明

For equivalent data and results to @GMalc 's A , the following CF formula rules work: 对于与@GMalcA等价的数据和结果,以下CF公式规则起作用:

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

if applied to ColumnC. 如果应用于ColumnC。 Details of how to apply CF here . 有关如何在此处应用CF的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM