【问题标题】:Conditional Formatting blank cells -VBA条件格式空白单元格-VBA
【发布时间】:2018-08-20 15:04:03
【问题描述】:

如果 B 列中的单元格小于 F 列中的值,我需要突出显示该单元格。例如,如果单元格 B2 为 10,F2 为 20,则 B2 应为红色。但是在 B 列中有空白单元格,我不希望这些单元格突出显示。例如 B6 为空白,但 F6 为 10。在我的代码中,B6 也变为红色。

另外,我将如何突出显示同一行中已经突出显示的单元格。例如,如果 B2 突出显示,则突出显示 F2。

我的代码如下:

Sub threecf()
    Dim rg As Range
    Dim cond1 As FormatCondition, cond2 As FormatCondition
    Set rg = Range("B2", Range("B2").End(xlDown))

    'clear any existing conditional formatting
    rg.FormatConditions.Delete

    'define the rule for each conditional format
    Set cond1 = rg.FormatConditions.Add(xlCellValue, xlLess, "=f2")
    Set cond2 = rg.FormatConditions.Add(xlCellValue, xlEqual, "=isempty(f2)")

    'define the format applied for each conditional format
    With cond1
      .Interior.Color = vbRed
      .Font.Color = vbWhite
    End With

    With cond2
      .Interior.Color = vbWhite
      .Font.Color = vbWhite
    End With

End Sub

【问题讨论】:

  • 为什么是 VBA 而不是 Excel 公式?
  • 如果你的范围不是太大,也许你应该直接用VBA命令格式化你的单元格,而不是使用条件格式。

标签: excel vba


【解决方案1】:

正如我在评论中提到的,使用公式。无需使用VBA

最简单的方式(推荐方式)

我推荐这种方式,因为它考虑了正在添加的新行。

  1. 选择列 B
  2. 选择主页选项卡 |条件格式 |新规则 |使用公式确定要格式化的单元格
  3. 输入公式=AND(B1<F1,B1<>"")
  4. 选择格式 |填充标签
  5. 将填充颜色设置为红色:)

自定义方式

  1. 手动选择单元格 B2 到 col B 中的最后一行
  2. 选择主页选项卡 |条件格式 |新规则 |使用公式确定要格式化的单元格
  3. 输入公式=AND(B2<F2,B2<>"")
  4. 选择格式 |填充标签
  5. 将填充颜色设置为红色:)

VBA方式

如果你仍然想要 VBA,那么试试这个

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long

    '~~> Change as applicable
    Set ws = Sheet1

    With ws
        lRow = .Range("B" & .Rows.Count).End(xlUp).Row

        With .Range("B2:B" & lRow)
            .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(B2<F2,B2<>"""")"
            .FormatConditions(.FormatConditions.Count).SetFirstPriority

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

            .FormatConditions(1).StopIfTrue = False
        End With
    End With
End Sub

【讨论】:

    【解决方案2】:

    转到条件格式 --> 新规则 --> 使用公式确定要格式化的单元格 --> 粘贴:=IF(AND(B2&lt;F2,B2&lt;&gt;"") = TRUE,1,0)

    对于 F 列:=IF(AND(F2&gt;B2,F2&lt;&gt;"") = TRUE,1,0)

    【讨论】:

    • 无需使用IF()。另外你忘了提到用户需要选择相关的列:) 最简单的方法是选择 Col B 和 Conditional formatting --&gt; New Rule --&gt; Use a formula =AND(B1&lt;F1,B1&lt;&gt;"") 并将填充颜色设置为红色:)
    【解决方案3】:

    如果您想要 VBA 解决方案,请尝试不使用条件格式:

    Sub StackOverflow()
        Dim x As Long
        With ThisWorkbook.Sheets("Stack")
            For x = 1 To .Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
                If .Cells(x, 2).Value <> "" And .Cells(x, 2).Value < .Cells(x, 6).Value Then
                    .Cells(x, 2).Interior.Color = vbRed
                    .Cells(x, 6).Interior.Color = vbRed
                    .Cells(x, 2).Font.Color = vbWhite
                    .Cells(x, 6).Font.Color = vbWhite
                Else
                    .Cells(x, 2).Interior.Pattern = xlNone
                    .Cells(x, 6).Interior.Pattern = xlNone
                    .Cells(x, 2).Font.Color = vbBlack
                    .Cells(x, 6).Font.Color = vbBlack
                End If
            Next x
        End With
    End Sub
    

    根据您的需要调整代码(根据需要更改宏名称、电子表格地址和颜色)。

    【讨论】:

    • 我认为非常糟糕的建议/替代方案:(
    • @Siddharth Rout,请问为什么?
    • 当然。让我先发布一个解决方案,然后我会回复你的评论
    • 由于两个原因1 用户希望解决条件格式问题。而不是解决他的问题,你试图避免这个话题。 2 手动着色单元格不实用。如果用户更改单元格的值,那么他或她需要一次又一次地运行代码。条件格式可以防止这种情况:)
    • 对不起,这里没有任何私人关系 :)