首页 文章

Excel 2007 VBA中重叠范围的条件格式设置 - 错误?

提问于
浏览
2

目前正试图帮助on this question - 但偶然发现了一个非常奇怪的问题:

尝试在重叠范围上添加条件格式时(在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
End Sub

Sub ProduceError9()
    Cells.FormatConditions.Delete
    Range("A1:A3").FormatConditions.Add Type:=2, Formula1:="=1"
    Range("A1:A2").FormatConditions.Add Type:=2, Formula1:="=1"
    Range("A1:A2").FormatConditions.Add Type:=2, Formula1:="=1"
    Range("A1:A2").FormatConditions(Range("A1:A2").FormatConditions.Count).Font.ColorIndex = 3
End Sub

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

有人知道解决方法吗?

1 回答

  • 0

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

    A1有2种格式条件,A2有1种格式条件 .

    范围(“A1:A2”) . FormatConditions.Count给出A1的计数,对于A2不存在FormatConditions(2),因此出错 .

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

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

    据推测,Excel 2010通过动态拆分格式条件来改善这种情况 .

相关问题