【问题标题】:How to colorcode cells based on conditions?如何根据条件为单元格着色?
【发布时间】:2019-11-12 06:10:44
【问题描述】:

我试图让这个循环重复从 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

我的循环不起作用,我如何让它一直沿着这些行运行。 我让它通过条件格式和记录创建它的宏来工作。

【问题讨论】:

    标签: excel vba conditional-statements color-coding


    【解决方案1】:

    对于与@GMalc 的A 等效的数据和结果,以下 CF 公式规则有效:

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

    如果应用于 ColumnC。如何申请CFhere的详细信息。

    【讨论】:

      【解决方案2】:

      希望我能正确理解您的问题。你不需要变量,但如果你想要它们,你需要在循环之外创建它们。在这段代码中,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
      

      【讨论】:

        【解决方案3】:

        这是一个基本的 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
        

        【讨论】:

          【解决方案4】:

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

          在 Reschedule Date 列 (P14) 的第一个单元格上设置您的规则,将其与 Receipt 列 (O14) 的第一个单元格进行比较,如果您对结果感到满意,请使用格式刷复制格式向下重新安排日期列的其余单元格。

          您需要两条规则。以下是有关如何在单元格 P14 上设置它们的屏幕截图:

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

          【讨论】:

          • 我找不到在条件格式中引用另一个单元格的方法。我创建了一个适用于该范围的第一行的代码,但现在正试图让它循环。我已经用更多信息编辑了我的问题
          猜你喜欢
          • 1970-01-01
          • 2014-05-04
          • 2023-04-07
          • 2022-12-08
          • 1970-01-01
          • 2015-12-27
          • 1970-01-01
          • 2017-02-25
          • 1970-01-01
          相关资源
          最近更新 更多