Excel 2007 VBA中重叠范围的条件格式 – bug?

目前试图帮助解决这个问题 – 但偶然发现了一个非常奇怪的问题:

当试图在重叠范围(在VBA中)上添加条件格式时,Excel 2007会生成错误1004或错误9(下标超出范围)错误。 我设法把错误的代码烧到这个:

 Sub Produce1004()
     Cells.FormatConditions.Delete
     Range(“A1”)。FormatConditions.Add Type:= xlExpression,Formula1:=“= 1”
     Range(“A1:A2”)。FormatConditions.Add Type:= xlExpression,Formula1:=“= 1”
     Range(“A1:A2”)。FormatConditions(Range(“A1:A2”)。FormatConditions.Count).Font.ColorIndex = 7
结束小组

 Sub ProduceError9()
     Cells.FormatConditions.Delete
    范围(“A1:A3”)。FormatConditions.Addtypes:= 2,Formula1:=“= 1”
    范围(“A1:A2”)。FormatConditions.Addtypes:= 2,Formula1:=“= 1”
    范围(“A1:A2”)。FormatConditions.Addtypes:= 2,Formula1:=“= 1”
     Format(“A1:A2”)。FormatConditions.Count).Font.ColorIndex = 3
结束小组

这是导致错误的两个潜艇的最后一行。 该错误只发生在Excel 2007中,它在2010年运行良好。

有人知道一个解决方法吗?

我可以在Produce1004()中看到一个问题:

A1有2个格式条件,A2有1个格式条件。

Range(“A1:A2”)。FormatConditions.Count给出了A1的计数,FormatConditions(2)对于A2不存在,因此是错误的。

但对于ProduceError9(),格式条件的数量与A1和A2相同。

通过一些实验,我可以通过推导出该范围与格式条件(设置字体[A1] .FormatCondition(3)也失败)来解释这一点。 必须更改格式条件定义的范围的格式。

据推测,Excel 2010通过dynamic分割格式条件改善了这种情况。