Excel 2013 – VBA – 运行时错误9当创build和访问条件格式化项目

我已经search和search,而不是提出一个解决scheme,不能解释发生了什么。

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

当行被插入[在范围的顶部]在使用这种方法的范围内:

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

条件格式变得复杂,杂乱,碎片化,最终在几个月后如此混乱似乎停止正常工作。

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

问题是使用.FormatConditions.Add和.FormatConditions(x)添加和格式化4个条件格式后,当我尝试格式化Item 5字体或背景色时,会引发下标错误。 我寻找一个解释,并明白,基本上我可以添加尽可能多的CF,因为我喜欢这是怎么回事?

疯狂的事情是,它似乎一次正确的工作,然后再也不是?

这是我的testing代码,这一切都运作良好,直到公式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规则被添加到工作表中,但显然这是没有格式化的浪费! 🙂

我也尝试了一种不同的格式化字体颜色背景颜色的方法,但是select大于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