【问题标题】:Conditional Formatting not being applied未应用条件格式
【发布时间】:2021-04-10 14:03:36
【问题描述】:

如果填充了某些单元格,我正在尝试将规则应用于模板的列,然后格式将使用公式的值/地址。在这一点上我有点迷茫,因为其他一切都在正常工作,我没有收到任何错误。 QAcol 在用于填充这些单元格的代码中较早地生成。任何将工作表指定为条件格式范围的尝试都会导致对象定义错误,我不知道为什么。

Dim CFQcol As Integer
Dim j As Integer


wb.Sheets("QA Data").Activate
row = 0
col = 0

For CFQcol = 17 To QAcol
    If IsEmpty(wb.Sheets("QA Data").Cells(3, CFQcol)) = False And InStr(wb.Sheets("QA Data").Cells(3, CFQcol).Value, "Repo") = 0 Then
        j = 1
        If IsEmpty(wb.Sheets("QA Data").Cells(4, CFQcol)) = False Then
            Range(Cells(11, CFQcol), Cells(10000, CFQcol)).FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:=Cells(4, CFQcol).Address
            Range(Cells(11, CFQcol), Cells(10000, CFQcol)).FormatConditions(j).Font.Color = RGB(255, 0, 0)
            j = j + 1
            End If
        If IsEmpty(wb.Sheets("QA Data").Cells(7, CFQcol)) = False Then
            Range(Cells(11, CFQcol), Cells(10000, CFQcol)).FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:=Cells(7, CFQcol).Address
            Range(Cells(11, CFQcol), Cells(10000, CFQcol)).FormatConditions(j).Font.Color = RGB(0, 0, 200)
            j = j + 1
            End If
        If IsEmpty(wb.Sheets("QA Data").Cells(5, CFQcol)) = False Then
            Range(Cells(11, CFQcol), Cells(10000, CFQcol)).FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:=Cells(5, CFQcol).Address
            Range(Cells(11, CFQcol), Cells(10000, CFQcol)).FormatConditions(j).Font.Color = RGB(255, 0, 0)
            j = j + 1
            End If
        If IsEmpty(wb.Sheets("QA Data").Cells(8, CFQcol)) = False Then
            Range(Cells(11, CFQcol), Cells(10000, CFQcol)).FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:=Cells(8, CFQcol).Address
            Range(Cells(11, CFQcol), Cells(10000, CFQcol)).FormatConditions(j).Font.Color = RGB(0, 0, 200)
            End If
    End If
Next

感谢您的帮助!

【问题讨论】:

  • 您不需要将地址更改为亲戚吗? .地址(假,假)

标签: vba formatting conditional-statements


【解决方案1】:

Range 默认使用活动工作表。您正在混合隐式和显式引用。尽可能使用显式引用。除非您为用户移动视图,否则您永远不必.Activate。您在最后一个块上缺少j = j + 1。让我们将 j = 1 移到循环之外,因为您每次都将添加更多条件格式。

注意:添加大量条件格式是个坏主意。它会减慢您的工作簿并使其在某些时候无法使用。

当我运行我的测试代码时,格式确实将文本更改为红色。如果不查看您的数据并更好地了解最终结果,就很难判断代码是否正在执行您想要的操作。

Dim CFQcol As Integer
Dim j As Integer

Dim qaData As Worksheet
Set qaData = wb.Sheets("QA Data")

row = 0
col = 0
j = 1

For CFQcol = 17 To QAcol
    If Not IsEmpty(qaData.Cells(3, CFQcol)) And InStr(qaData.Cells(3, CFQcol).Value, "Repo") = 0 Then

        If Not IsEmpty(qaData.Cells(4, CFQcol)) Then
            With qaData.Range(Cells(11, CFQcol), Cells(10000, CFQcol))
                .FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:=qaData.Cells(4, CFQcol).Address
                .FormatConditions(j).Font.Color = RGB(255, 0, 0)
            End With
            j = j + 1
        End If
        
        If Not IsEmpty(qaData.Cells(7, CFQcol)) Then
            With qaData.Range(Cells(11, CFQcol), Cells(10000, CFQcol))
                .FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:=qaData.Cells(7, CFQcol).Address
                .FormatConditions(j).Font.Color = RGB(0, 0, 200)
            End With
            j = j + 1
        End If
        
        If Not IsEmpty(qaData.Cells(5, CFQcol)) Then
            With qaData.Range(Cells(11, CFQcol), Cells(10000, CFQcol))
                .FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:=qaData.Cells(5, CFQcol).Address
                .FormatConditions(j).Font.Color = RGB(255, 0, 0)
            End With
            j = j + 1
        End If
        
        If Not IsEmpty(qaData.Cells(8, CFQcol)) Then
            With qaData.Range(Cells(11, CFQcol), Cells(10000, CFQcol))
                .FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:=qaData.Cells(8, CFQcol).Address
                .FormatConditions(j).Font.Color = RGB(0, 0, 200)
            End With
            j = j + 1
        End If
    End If
Next

【讨论】:

  • 谢谢!这几乎对我有用,但我不得不将地址添加到范围内的单元格中。也许不同的excel版本? With qaData.Range(Cells(11, CFQcol).Address, Cells(10000, CFQcol).Address) 知道如何阻止条件格式在我的公式地址周围加上双引号吗?它目前正在报告="$A$1",它将地址中的任何数字都归零。
  • 我不认为这段代码是原因。尝试调试。逐行运行,看看每一行的作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-07
  • 2021-08-08
  • 2022-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多