实现条件格式

时间:2018-01-31 00:04:19

标签: excel vba excel-vba conditional-formatting

我在每列的第11行和第13行都有一组容差值。我有第21行到第118行的数据,我希望条件格式为高和低。高红色,低黄色。这是一个较大的宏的一部分,它将数据复制出工作簿,关闭它并将数据粘贴到新工作簿中。我需要有人指出为什么我的代码不会选择我正在尝试的单元格。

以下是一些示例数据

1

2 Max

3 4787

4分钟

5 2331

6 Spec

7 3250

8容忍

9 750

10 Hi Spec

11 4000

12低规格

13 2500

14

15

16

17失败

18 31

19

20数据

21 3947

...

118 4409

Dim i As Integer
For i = 7 To 13

    Worksheets(1).Range(Cells(i, 21), Cells(i, 118)).Select

    Worksheets(1).Range(Cells(i, 21), Cells(i, 118)).FormatConditions.Add 
        Type:=xlCellValue, Operator:=xlGreater, _
            Formula1:=Cells(i, 11)

Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority

    With Selection.FormatConditions(1).Interior
      .PatternColorIndex = xlAutomatic
          .Color = 255
          .TintAndShade = 0
    End With



    Worksheets(1).Range(Cells(i, 21), Cells(i, 118)).FormatConditions.Add 
         Type:=xlCellValue, Operator:=xlLess, _
           Formula1:=Cells(i, 13)

Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority

    With Selection.FormatConditions(1).Interior
       .PatternColorIndex = xlAutomatic
       .ColorIndex = 6
       .TintAndShade = 0
    End With
      'i=i+1 fragment from previous code, deleted (please ignore)

下一步

请使用此代码作为学习练习,正确的方法如下

Dim i As Integer
For i = 7 To 13

Range(Cells(21, i), Cells(118, i)).Select

Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
    Formula1:=Cells(11, i)

With Selection.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .Color = 255
    .TintAndShade = 0
 End With


 Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
      Formula1:=Cells(13, i)

 Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
   .PatternColorIndex = xlAutomatic
   .ColorIndex = 6
   .TintAndShade = 0
    End With

Next

0 个答案:

没有答案