Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何根据条件对单元格进行颜色编码?_Excel_Vba_Conditional Statements_Color Coding - Fatal编程技术网

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

Excel 如何根据条件对单元格进行颜色编码?,excel,vba,conditional-statements,color-coding,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 = xlSo

我试图让这个循环在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
我的循环不起作用,我如何使它一直沿着这些行运行。
我让它通过条件格式和录制创建宏来工作。

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

在“重新安排日期”列(P14)的第一个单元格上设置规则,将其与“接收”列(O14)的第一个单元格进行比较,如果您对结果满意,请使用格式画师将格式复制到“重新安排日期”列的其余单元格

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

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


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

在“重新安排日期”列(P14)的第一个单元格上设置规则,将其与“接收”列(O14)的第一个单元格进行比较,如果您对结果满意,请使用格式画师将格式复制到“重新安排日期”列的其余单元格

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

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


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

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

这里是一个基本的VBA解决方案;列标题位于第1行,列中可能有空白单元格;此宏将比较
重新安排日期
列中的日期与左侧列中的日期。如果日期比左列中的日期新,则会将单元格
涂成红色。如果日期早于左列中的日期,则该单元格的颜色将为绿色。见附件图片

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
Dim tCol尽可能长,cel尽可能长
带工作表(“表2”)
'使用“查找”标识列号
tCol=.Rows(1).Find(“重新安排日期”,xlFormulas,xlWhole,xlByRows,xlPrevious).Column
'循环遍历列中的每个单元格,从行到最后使用的行
对于.范围内的每个单元格(.Cells(2,tCol),.Cells(.Rows.Count,tCol).End(xlUp))
'测试每个细胞;如果不为空且cel值小于
'左侧单元格的值,然后将单元格染成绿色
如果单元格值“”和单元格值<单元格偏移量(,-1).Value,则
cel.Interior.ColorIndex=4
'elseif测试每个细胞;如果不为空且cel值大于
'左侧单元格的值,然后将单元格涂成红色
ElseIf cel.Value“”和cel.Value>cel.Offset(-1)。然后选择cel.Value
cel.Interior.ColorIndex=3
如果结束
下一个cel'循环到下一个cel
以

这里是一个基本的VBA解决方案;列标题位于第1行,列中可能有空白单元格;此宏将比较
重新安排日期
列中的日期与左侧列中的日期。如果日期比左列中的日期新,则会将单元格
涂成红色。如果日期早于左列中的日期,则该单元格的颜色将为绿色。见附件图片

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
Dim tCol尽可能长,cel尽可能长
带工作表(“表2”)
'使用“查找”标识列号
tCol=.Rows(1).Find(“重新安排日期”,xlFormulas,xlWhole,xlByRows,xlPrevious).Column
'循环遍历列中的每个单元格,从行到最后使用的行
对于.范围内的每个单元格(.Cells(2,tCol),.Cells(.Rows.Count,tCol).End(xlUp))
'测试每个细胞;如果不为空且cel值小于
'左侧单元格的值,然后将单元格染成绿色
如果单元格值“”和单元格值<单元格偏移量(,-1).Value,则
cel.Interior.ColorIndex=4
'elseif测试每个细胞;如果不为空且cel值大于
'左侧单元格的值,然后将单元格涂成红色
ElseIf cel.Value“”和cel.Value>cel.Offset(-1)。然后选择cel.Value
cel.Interior.ColorIndex=3
如果结束
下一个cel'循环到下一个cel
以

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


红色:
=C1>B1
,绿色:
=和(C1“”,C1对于与的等效数据和结果,以下CF公式规则有效:


红色:
=C1>B1
,绿色:
=和(C1“”,c1我找不到在条件格式中引用另一个单元格的方法。我创建了一个适用于该区域顶行的代码,但现在正在尝试使其循环。我已使用更多信息编辑了我的问题。我找不到在条件格式中引用另一个单元格的方法。我创建了一个适用于该区域顶行的代码范围的op行,但