【问题标题】:Excel VBA: Conditional Formatting - Interior ColorsExcel VBA:条件格式 - 内部颜色
【发布时间】:2018-02-23 19:08:12
【问题描述】:

我正在尝试在 Excel 2013 的 VBA 中获得一些条件格式。如果列表验证为“完成”,则代码的执行应使 Col N 中的单元格(内部颜色)变为绿色,并且任何其他时间为白色。如果 Col N 中的列表验证为“保留”,则 Col O 中的单元格(内部颜色)应变为红色,其他时间为白色。

目前,出现的结果是: 1. 如果未从列表验证中选择任何内容,则 Col N 和 Col O 为白色。 2. 当从列表验证中选择任何内容时,Col N 变为绿色。 3. 在 Col N 中选择“保持”时,Col O 变为红色,如果在 Col N 中选择其他任何内容,则再次变为白色。
4. 如果在 Col O 中选择了某些内容,则单元格变为红色。

我当前的代码是(连同我已注释掉的部分):

'Add conditional format for column N.  If Status is "Complete", color Status cell (col N) green (43).

With Worksheets(SheetNum & " - Work").Range("N2:N2000").Select
    'Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=OR(($N2=""Not Started""),($N2=""In Queue""), ($N2=""In Work""), ($N2=""Held""), ($N2="" "")"
    'Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual, _
        Formula1:="=($N2=""Complete"")"
    'With Selection.FormatConditions(1)
        '.Interior.ColorIndex = 2
        '.StopIfTrue = True
    'End With
'End With

    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual, _
        Formula1:="=($N2=""Complete"")"
    With Selection.FormatConditions(1)
        .Interior.ColorIndex = 43
        '.StopIfTrue = True
    End With
End With

'Add conditional format for column O.  If Status is "Held", color Held For cell (col O)
'red (3).
'With Worksheets(SheetNum & " - Work").Range("O2:O2000").Select
    'Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual, _
        Formula1:="=OR(($N2=""Not Started""),($N2=""In Queue""), ($N2=""In Work""), ($N2=""Complete""))"
    'Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=($N2=""Held"")"
    'With Selection.FormatConditions(1)
        '.Interior.ColorIndex = 2
        '.StopIfTrue = True
    'End With

    With Worksheets(SheetNum & " - Work").Range("O2:O2000").Select
        Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual, _
            Formula1:="=($N2=""Held"")"
        With Selection.FormatConditions(1)
            .Interior.ColorIndex = 3
            '.StopIfTrue = True
        End With
    End With

另外,有人可以解释何时使用 Operator:=xlNotEqual vs Operator:=xlEqual 吗?这些似乎与我的预期相反。

感谢您的帮助。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    不幸的是,这些列需要即时着色,这就是我尝试使用条件格式的原因。我正在尝试将大约 10 种条件格式应用于这些工作表,这些工作表将在程序的整个生命周期中创建。

    从好的方面来说,在尝试了一整天之后,我注意到我在 (""Held"") 之后缺少了一个 ")"。不过,它似乎并没有使文件正常工作。

    【讨论】:

      【解决方案2】:

      单元格 $N2 是否包含实际的单词“Complete”和“Held”?

      如果是这样,也许这会起作用

      Sub tester()
          For I = 1 To 100 ' or lastused row
              If InStr(1, UCase(Sheet6.Range("N" & I).Value), "COMPLETE") > 0 Then
                  Sheet6.Range("N" & I).Interior.ColorIndex = 43
              Else
                  If InStr(1, UCase(Sheet6.Range("N" & I).Value), "HELD") > 0 Then
                      Sheet6.Range("N" & I).Interior.ColorIndex = 3
                  Else
      
                      Sheet6.Range("N" & I).Interior.ColorIndex = 2
                  End If
              End If
      
          Next I
      End Sub
      

      【讨论】:

        【解决方案3】:

        我终于找到了正确的代码来完成我想做的事情。

            'Add conditional format for column N.  If Status is "Complete", color Status cell (col N) green (43).
        With Worksheets(SheetNum & " - Work").Range("N2:N2000").Select
                Selection.FormatConditions.Add Type:=xlExpression, _
                    Formula1:="=($N2=""Complete"")"
                With Selection.FormatConditions(1)
                    .Interior.ColorIndex = 43
                    .StopIfTrue = True
                End With
            End With
        

        【讨论】:

          猜你喜欢
          • 2020-06-01
          • 1970-01-01
          • 2017-07-20
          • 2016-12-30
          • 2018-05-05
          • 1970-01-01
          • 2011-10-02
          • 1970-01-01
          • 2014-11-12
          相关资源
          最近更新 更多