VBA FormatCondition跳过第一行

时间:2018-04-21 05:46:13

标签: excel vba excel-vba conditional-formatting

我试图通过为每列应用不同的条件格式来突出显示值。但是,我找不到跳过整个第一行的有效方法,因为它基本上是标题行,不应该应用任何高亮显示。以下是我对某些专栏的代码:

'highlight first row white
With y.Sheets(Sh).Range("Y1:Y1").FormatConditions.Add(Type:=xlExpression, Formula1:="=NOT(ISBLANK(Y1))")
.Interior.Color = rgbWhite

With y.Sheets(Sh).Range("Y:Y").FormatConditions.Add(Type:=xlCellValue, Operator:=xlEqual, Formula1:="Z")
   .Interior.Color = rgbWhite
    End With
With y.Sheets(Sh).Range("Y:Y").FormatConditions.Add(Type:=xlCellValue, Operator:=xlEqual, Formula1:="NA")
   .Interior.Color = rgbWhite
    End With
With y.Sheets(Sh).Range("Y:Y").FormatConditions.Add(Type:=xlCellValue, Operator:=xlGreaterEqual, Formula1:="=" & imput)
   .Interior.Color = rgbOrange
    End With

   End With

'highlight first row white
With y.Sheets(Sh).Range("Z1:Z1").FormatConditions.Add(Type:=xlExpression, Formula1:="=NOT(ISBLANK(Z1))")
.Interior.Color = rgbWhite
   End With

With y.Sheets(Sh).Range("Z:Z").FormatConditions.Add(Type:=xlCellValue, Operator:=xlEqual, Formula1:="Z")
   .Interior.Color = rgbWhite
    End With
With y.Sheets(Sh).Range("Z:Z").FormatConditions.Add(Type:=xlCellValue, Operator:=xlEqual, Formula1:="NA")
   .Interior.Color = rgbWhite
    End With
With y.Sheets(Sh).Range("Z:Z").FormatConditions.Add(Type:=xlCellValue, Operator:=xlGreaterEqual, Formula1:="=" & imput)
   .Interior.Color = RGB(255, 153, 0) 'Orange
    End With

我几乎要为每一列添加第一个with语句,颜色第一行白色,以覆盖后面的格式。是否有更有效的方法来保持第一行未填充?

2 个答案:

答案 0 :(得分:1)

您不需要将其他单元格着色为白色,您可以将Y列和Z列组合在一起;像这样......

Dim LastRow As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).Row

With y.Sheets(Sh).Range("Y2:Z" & LastRow).FormatConditions.Add(Type:=xlExpression, Formula1:="=" & imput)
    .Interior.Color = RGB(255, 153, 0) 'Orange
End With

答案 1 :(得分:1)

使用此代码,可以更轻松地定义和设置所需的所有CF规则(适用于所有列)。它应用从第2行(波纹管标题)开始向下到最后使用的行的所有规则,因此标题不受影响

要从工作表中删除所有当前规则并开始清理,请取消注释此行

  • ws.Columns.FormatConditions.Delete
Option Explicit

Public Sub CFRules()
    Dim ws As Worksheet, minVal As String, lr As Long
    Dim rngCol1Rows As Range, rngCol2Rows As Range

    Set ws = ActiveSheet
    lr = ws.UsedRange.Rows.Count
    minVal = 3  '(imput)

    'ws.Columns.FormatConditions.Delete  'If uncommented, this will remove all CF rules!

    Set rngCol1Rows = ws.Range("Y2:Y" & lr) 'Col Y, except Header (Start Range at Row 2)
    Set rngCol2Rows = ws.Range("Z2:Z" & lr) 'Col Z, except Header

    SetCFRule rngCol1Rows, "=AND(Y2>=" & minVal & ", ISNUMBER(Y2))", rgbOrange
    SetCFRule rngCol2Rows, "=AND(Z2>=" & minVal & ", ISNUMBER(Z2))", RGB(255, 153, 0)

    SetCFRule rngCol1Rows, "=OR(Y2=""Z"", Y2=""NA"")", vbWhite
    SetCFRule rngCol2Rows, "=OR(Z2=""Z"", Z2=""NA"")", vbWhite
End Sub

Private Sub SetCFRule(ByRef cfRng As Range, cfFormula As String, ByVal cfColor As Long)
    With cfRng
        .FormatConditions.Add Type:=xlExpression, Formula1:=cfFormula
        .FormatConditions(cfRng.FormatConditions.Count).SetFirstPriority
        With .FormatConditions(1)
            .Interior.Color = cfColor
            .StopIfTrue = False
        End With
    End With
End Sub