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

时间:2019-07-01 15:14:41

标签: excel vba conditional-statements color-coding

我正在尝试使此循环重复执行O14:P434的两行。我希望它能在整个范围内运行,但仅当列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

我的循环不起作用,如何使其一直沿这些行向下运行。 我通过条件格式来工作并记录了创建它的宏。

4 个答案:

答案 0 :(得分:0)

听起来您只需要使用条件格式设置“突出显示单元格规则”即可突出显示大于或小于的单元格。或者,您可以使用公式,通过选择“更多规则”,然后选择“使用公式来确定要格式化的单元格”,来设置更复杂的规则。

在“重新安排日期”列(P14)的第一个单元格与“收据”列(O14)的第一个单元格上设置规则,如果对结果满意,请使用“格式刷”复制格式在“重新安排日期”列的其余单元格下面。

您将需要两个规则。以下是有关如何在单元格P14上进行设置的屏幕截图: enter image description here

enter image description here

enter image description here

格式化所有单元格后,最终结果应如下所示:

答案 1 :(得分:0)

希望我能正确理解您的问题。您不需要变量,但是如果需要变量,则需要在循环外部创建它们。在此代码中,x是随每次循环而继续变化的行,而15、16是O&P列。


cluster <- makeCluster(detectCores() - 1) # convention to leave 1 core for OS
registerDoParallel(cluster)

partial(cforest_adjusted, 
pred.var = c("avg_mtg_duration", "avg_mtg_attd"), 
trim.outliers = TRUE, chull = TRUE, parallel = TRUE,
grid.resolution = 30)

答案 2 :(得分:0)

这是基本的VBA解决方案;如果您的列标题位于第1行,则该列中可能有空白单元格;此宏会将Reschedule date列中的日期与左侧列中的日期进行比较。如果日期较新,则左列中的日期将为单元格Red上色。如果日期早于左栏中的日期,它将为单元格Green上色。见所附图片...

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

答案 3 :(得分:0)

对于与@GMalcA等效的数据和结果,以下CF公式规则起作用:

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

(如果应用于ColumnC)。有关如何应用CF here的详细信息。

相关问题