简体   繁体   English

Excel VBA - 循环中单元格的条件格式

[英]Excel VBA - conditional formatting of cells in a loop

I have a number of figures in cells in range C1:E3.我在 C1:E3 范围内的单元格中有许多数字。 I want to apply a conditional formatting of the cells in each row, based on the value provided in column 1. So, for example for range C1:E1 I want to fill with red all the cells with a value greater than value in cell A1, for range C2:E2 - with a value greater than A2 etc. I've tried to write a loop, but I don't know how to properly refer to the values that determine formatting - in the code below it's the part "Formula1:="=A&row". How to do it right?我想根据第 1 列中提供的值对每行中的单元格应用条件格式。因此,例如对于范围 C1:E1,我想用红色填充所有值大于单元格 A1 中的值的单元格, 对于范围 C2:E2 - 值大于 A2 等。我尝试编写一个循环,但我不知道如何正确引用确定格式的值 - 在下面的代码中,它是“Formula1”部分:="=A&row". 怎么做才对?

Sub color()

For Row = 1 To 3

    Range(Cells(Row, 3), Cells(Row, 5)).Select
    Application.CutCopyMode = False
    With Selection
         .FormatConditions.Delete

         .FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
            Formula1:="=A&row"
        .FormatConditions(1).Interior.color = RGB(255, 0, 0)

    End With

Next Row
End Sub

The $A1 thing is not intuitive unless you've used Excel / Conditional Formatting a lot, but this also works:除非您经常使用 Excel / 条件格式,否则 $A1 的东西并不直观,但这也有效:

Sub color()

    Application.CutCopyMode = False
    With Range(Cells(1, 3), Cells(3, 5))
      .FormatConditions.Delete

      .FormatConditions.add Type:=xlCellValue, Operator:=xlGreater, _
        Formula1:="=$A1"
     .FormatConditions(1).Interior.color = RGB(255, 0, 0)

    End With
End Sub

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

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