单元格的 .Interior.ColorIndex 值与可见值不同

Cell's .Interior.ColorIndex Value Different From What Is Visible

SO 社区,

问题是我有一个通过条件格式设置为红色的单元格,但是当我检查单元格的 .Interior.ColorIndex 时,它 returns 35 对应于浅绿色.

上下文信息

  1. 有问题的单元格是一个合并单元格,名为partNumber_1,它跨越多个单元格I12:L12
  2. 立即window,?Range("I12").Interior.ColorIndexreturns35
  3. ?Range("partNumber_1").Interior.ColorIndex returns 35
  4. 如果我将 2. 和 3. 更改为 .Interior.Color(以获得 long 值),它 returns 13434828 对应于 RGB(205, 255, 205)
  5. 相关命名范围 partNumber_1 属于另一个命名范围 ScannedPartNumbers,这是条件格式公式中引用的命名范围。
  6. ?Range("ScannedPartNumbers").Interior.ColorIndex 也 returns 35

我通过 VBA 应用和删除条件格式,其中 targetScannedPartNumbers 范围,fillColor 是 3(红色),fontColor 是 6(黄色)。

Public Sub AddBlankCellFormatCondition(target As range, fillColor As Integer, Optional fontColor As Integer = 1)
    target.Parent.Unprotect password:=Strings.Mypw
    With target
        .FormatConditions.Add Type:=xlBlanksCondition
        .FormatConditions(.FormatConditions.Count).SetFirstPriority
        With .FormatConditions(1)
            .Interior.ColorIndex = fillColor
            .Font.ColorIndex = fontColor
        End With
    End With
    target.Parent.Protect password:=Strings.Mypw, userinterfaceonly:=True
End Sub

ScannedPartNumbers 命名范围内的所有单元格都变成了红色,但我还没有找到具有 .Interior.ColorIndex.Interior.Color 值的单个单元格甚至有点像红色!

请帮忙?

将条件格式视为放置在单元格格式顶部的 sheet 透明度。条件格式实际上并不会改变单元格的内部颜色,它会在其顶部放置一种新颜色并强制 Excel 改为打印该颜色。

结果是单元格 ColorColorIndex 的值没有改变,因此 VBA 检查它们不会发现任何差异。

与其查看单元格的颜色,不如使用与条件格式相同的条件逻辑来检查要使用哪些单元格?

Excel 中的颜色选项仅用于显示目的,通常不应被视为值。有充分的理由,您不能按颜色排序或过滤。虽然使用 VBA 确实有可能做一些类似的事情,但这并不是一个好主意,通常鼓励用户使用值来排序和过滤比使用颜色更好。