简体   繁体   English

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

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

SO Community, SO社区,

The problem is that I have a cell that is colored red via conditional formatting, but when I check the cell's .Interior.ColorIndex , it returns 35 , which corresponds to light green. 问题是我有一个通过条件格式设置为红色的单元格,但是当我检查该单元格的.Interior.ColorIndex ,它返回35 ,对应于浅绿色。

Contextual Information 语境信息

  1. The cell in question is a merged cell , named partNumber_1 , and it spans cells I12:L12 所讨论的单元格是一个名为partNumber_1合并单元格 ,它跨越了单元格I12:L12
  2. In the immediate window, ?Range("I12").Interior.ColorIndex returns 35 在立即窗口中, ?Range("I12").Interior.ColorIndex返回35
  3. ?Range("partNumber_1").Interior.ColorIndex returns 35 ?Range("partNumber_1").Interior.ColorIndex返回35
  4. If I change 2. and 3. to .Interior.Color (to get the long value), it returns 13434828 which corresponds to RGB(205, 255, 205) 如果将2.和3.更改为.Interior.Color (以获取long值),它将返回13434828 ,该值对应于RGB(205, 255, 205) 13434828 RGB(205, 255, 205)
  5. The named range in question, partNumber_1 belongs to another named range, ScannedPartNumbers , which is the named range referenced in the conditional formatting formula. 有问题的命名范围partNumber_1属于另一个命名范围ScannedPartNumbers ,这是条件格式公式中引用的命名范围。
  6. ?Range("ScannedPartNumbers").Interior.ColorIndex also returns 35 ?Range("ScannedPartNumbers").Interior.ColorIndex也返回35

I apply and remove the conditional formatting via VBA, where target is the ScannedPartNumbers range, fillColor is 3 (red), and fontColor is 6 (yellow). 我通过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

All of the cells in the ScannedPartNumbers named range have turned red, but I haven't yet been able to find a single cell that has the .Interior.ColorIndex or .Interior.Color value that even remotely resembles red! ScannedPartNumbers命名范围内的所有单元格都变成红色,但是我还没有找到具有.Interior.ColorIndex.Interior.Color值甚至与红色相似的单个单元格!

Help please? 请帮助?

Think of Conditional Formatting as a sheet of transparency that's laid over the top of your cell formats. 将条件格式设置为单元格格式顶部的透明表。 Conditional formatting doesn't actually change the interior colour of the cell, it places a new colour over the top of it and forces Excel to print that colour instead. 条件格式实际上并不会改变单元格的内部颜色,它会在单元格的顶部放置新的颜色,并强制Excel打印该颜色。

The result is that the cells Color and ColorIndex values do not change, so VBA checking them will not see any difference. 结果是单元格的ColorColorIndex值不变,因此VBA检查它们不会看到任何差异。

Rather than looking at the colour of the cells, may I suggest using the same conditional logic you're using for Conditional Formatting to check which cells you want to work with? 除了查看单元格的颜色,我是否建议使用与条件格式设置相同的条件逻辑来检查要使用的单元格?

Colour options in Excel are strictly there for display purposes, and generally should not be seen as values. Excel中的颜色选项严格用于显示目的,通常不应视为值。 You can't sort or filter by colour, for good reason. 出于充分的原因,您不能按颜色排序或过滤。 While it is perforce possible to do something of the sort with VBA, it's rarely a good idea, and generally better to encourage users to use values to sort and filter by, rather than using colours. 虽然可以使用VBA进行某种排序,但这并不是一个好主意,通常更好地鼓励用户使用值进行排序和筛选,而不是使用颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM