vba - Excel 2013 - VBA - 创建和访问 [超过 4 个!] 条件格式项目时出现运行时错误 9

标签 vba excel

我已经搜索和搜索并没有提出解决方案,也无法解释正在发生的事情。

基本上,我在 Excel 2013 中有一个工作表,其中每行都有一组发票数据。工作表上有 8 种不同范围的条件格式。

当使用这种方法将行插入 [在范围的顶部] 到范围中时:

    Manager.Select
    Manager.Range("headerRow").Offset(1, 0).Range("a1:M1").Select
    Selection.Copy
    Selection.Insert Shift:=xlDown

条件格式变得令人费解、困惑、支离 splinter ,几个月后最终变得如此困惑,似乎无法正常工作。

在各种方法上浪费了几个小时之后,我的解决方案很简单。 (当然!)当在范围顶部插入一行时,我想使用 VBA 清除和重置 8 x 条件格式 - 本质上是执行快速清理 - 通过删除工作表上的所有条件格式并使用正确的范围和整个范围(从上到下)的格式重新应用 8 种干净的条件格式。

问题是,在使用 .FormatConditions.Add 和 .FormatConditions(x) 添加和格式化 4 种条件格式后,当我尝试格式化第 5 项字体或背景颜色时,它会引发下标错误。我已经搜索了解释并了解基本上我可以添加任意数量的 CF,这是怎么回事?

疯狂的是,它似乎一次正常工作,然后再也没有?

这是我的测试代码,在公式 5 之前一切正常!
Sub CFReset()

' Get top and bottom of Invoices
TopRow = (Manager.Range("headerRow").Row) + 1
BottomRow = ActiveSheet.UsedRange.Rows.Count

' Clear All Current Conditional Formatting on Manager Sheet between Invoice Top Row and Invocie Bottom Row
Range("A" & TopRow & ":L" & BottomRow).FormatConditions.Delete

'CD Formula 1 - 'Formula:  =AND(I17="Paid",G17<>0) - 'Colour: Red on White Background 'Applies to: =$G$17:$G$49
With ActiveSheet.Range("$G$" & TopRow & ":$G$" & BottomRow)
    .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(I" & TopRow & "=""Paid"",G" & TopRow & "<>0)"
    .FormatConditions(1).Font.ColorIndex = 3 ' Red
End With

'CD Formula 2 - '=AND(I17="Partial",F17<>0) 'Colour: Red on white background 'Applies to: =$G$17:$G$49
With ActiveSheet.Range("$G$" & TopRow & ":$G$" & BottomRow)
    .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(I" & TopRow & "=""Partial"",F" & TopRow & "<>0)"
    .FormatConditions(2).Font.ColorIndex = 3 ' Red
End With

'CD Formula 3 '=OR($I17="Void") - Text Colour RGB: 255,179,179 'Applies to: =$A$17:$I$49
With ActiveSheet.Range("$A$" & TopRow & ":$I$" & BottomRow)
    .FormatConditions.Add Type:=xlExpression, Formula1:="=OR($I" & TopRow & "=""Void"")"
    .FormatConditions(3).Font.Color = RGB(255, 179, 179) ' Pinkish
End With

'CD Formula 4 - '=OR($I17="Paid",$I17="Closed") - Text Colour: RGB 192, 192, 192 'Applies to: =$A$17:$I$49
With ActiveSheet.Range("$A$" & TopRow & ":$I$" & BottomRow)
    .FormatConditions.Add Type:=xlExpression, Formula1:="=OR($I" & TopRow & "=""Paid"",$I" & TopRow & "=""Closed"")"
    .FormatConditions(4).Font.Color = RGB(192, 192, 192) ' Gray
End With

'CD Formula 5 '=AND(I17="Paid",G17<>0) 'Color: RGB 0, 0, 0 'Black 'Background Colour:RGB: 255,255,204 ' Light Yellow 'Applies to: =$F$17:$F$49
With ActiveSheet.Range("$F$" & TopRow & ":$F$" & BottomRow)
    .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(I" & TopRow & "=""Paid"",G" & TopRow & "<>0)"
    .FormatConditions(5).Font.ColorIndex = 1 ' Black
    .FormatConditions(5).Interior.Color = RGB(255, 255, 204) ' Light Yellow
End With

'CD Formula 6  '=AND(I17="Partial",F17=0) 'Color: RGB 0, 0, 0 'Black 'Background Colour:RGB: 255,255,204 ' Light Yellow 'Applies to: =$F$17:$F$49
With ActiveSheet.Range("$F$" & TopRow & ":$F$" & BottomRow)
    .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(I" & TopRow & "=""Partial"",F" & TopRow & "=0)"
    .FormatConditions(6).Font.ColorIndex = 1 ' Black
    .FormatConditions(6).Interior.Color = RGB(255, 255, 204) ' Light Yellow
End With


'CD Formula 7 '=AND(G17>0,AND(D17<TODAY(),D17<>"")) 'Colour: Red 'Background Colour:RGB: 255,255,204 ' Light Yellow 'Applies to: =$D$17:$D$49
With ActiveSheet.Range("$D$" & TopRow & ":$D$" & BottomRow)
    .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(G" & TopRow & ">0,AND(D" & TopRow & "<TODAY(),D" & TopRow & "<>""""))"
    .FormatConditions(7).Font.ColorIndex = 3 ' Red
    .FormatConditions(7).Interior.Color = RGB(255, 255, 204) ' Light Yellow
End With


'CD Formula 8 '=COUNTIF($B:$B,B17)>1 'Color: RGB 255,255,255 -'White 'Background Colour: Red ' Rgb 255 ' Applies to: =$B$17:$B$49
With ActiveSheet.Range("$B$" & TopRow & ":$B$" & BottomRow)
    .FormatConditions.Add Type:=xlExpression, Formula1:="=COUNTIF($B:$B,B" & TopRow & ")>1"
    .FormatConditions(8).Font.ColorIndex = 2 ' White
    .FormatConditions(8).Interior.ColorIndex = 3 ' Red
End With


End Sub

如果我删除字体和背景的格式,CF 规则就会添加到工作表中,但显然这是在浪费时间而不进行格式化! :)

我还尝试了另一种格式化字体颜色背景颜色的方法,但是选择高于 4 的条件格式项目会返回相同的错误。
'Attempt2
With ActiveSheet.Range("$A$" & TopRow & ":$I$" & BottomRow).FormatConditions
    .Add Type:=xlExpression, Formula1:="=OR($I" & TopRow & "=""Void"")"
    With .Item(3).Font
        .Color = RGB(255, 179, 179) ' Pinkish
    End With
End With

我已经包含了我的评论,这些评论是我对每种格式的注释——这些实际上来自我原来的 CF。

最佳答案

每个FormatConditions集合与您指定的范围有关,而不是与整个工作表有关。您的条件格式 #5 是破坏性的,因为它仅适用于 F 列的一部分,并且仅定义了两个先前的 CF,其中包括 F 列的该部分。如果您将引用从 .FormatConditions(5) 更改为集合至.FormatConditions(3)那么它应该工作

CF#1 和 #2 仅适用于 G 列的一部分,而 CF#3 和 #4 适用于 A:I 列的部分。因此,当您尝试将 CF#5 添加到 F 列的一部分时,集合中只有两个先前的 CF。为了解决这个问题,CF#5 应该使用索引 3,CF#6 应该使用索引 4,CF#7 应该使用索引 3,CF#8 应该使用索引 3

关于vba - Excel 2013 - VBA - 创建和访问 [超过 4 个!] 条件格式项目时出现运行时错误 9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28721581/

相关文章:

vba - 排序,然后重新排序

asp.net-mvc - 将过滤结果导出到 MVC 4 中的 Excel(无 Web 表单)

excel - VBA Excel 中的排列

excel - 如何识别一行中连续标记的两个不同范围的单元格

excel - 如果两张表的值匹配,则添加新表

VBA:格式化 MS Word 文本

regex - Excel VBA - 查找带有通配符的字符串

c# - 是否可以使用 C# 在 excel 工作簿中编写功能

java - 为什么 Apache POI xlsx 换行文本不起作用?

excel - 在一个逗号分隔的单元格中显示所有匹配的值