【问题标题】:VBA FormatCondition to skip the first rowVBA FormatCondition 跳过第一行
【发布时间】:2018-04-21 18:11:56
【问题描述】:

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

'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 语句,将第一行涂成白色,以覆盖后面的格式。有没有更有效的方法来保持第一行不填充?

【问题讨论】:

  • 将最后一行设置为变量并使用Range("A2:A" & LastRow)

标签: vba excel conditional-formatting


【解决方案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

【讨论】:

  • 这只会突出显示第一行橙色。列中的所有单元格都是白色而不是条件橙色...
【解决方案2】:

使用此代码可以更轻松地定义和设置您需要的所有 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-07
    • 2013-08-17
    • 2013-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    相关资源
    最近更新 更多