对象'Font'的方法'颜色'失败

我在Excel 2010 VBA代码中收到标题错误消息。 我已经看过这个问题 , 这个问题看起来很相似,但似乎是解决了这个问题。

我的代码parsing当前工作表上的所有条件格式,并将其作为文本转储到另一个(新创build的)工作表 – 最终目标是将这些相同的条件加载到几乎相同的工作表(因此我不能只复制基本工作表)。

代码是:

Public Sub DumpExistingRules() 'portions of the code from here: http://dailydoseofexcel.com/archives/2010/04/16/listing-format-conditions/ Const RuleSheetNameSuffix As String = "-Rules" Dim TheWB As Workbook Set TheWB = ActiveWorkbook Dim SourceSheet As Worksheet Set SourceSheet = TheWB.ActiveSheet Dim RuleSheetName As String RuleSheetName = SourceSheet.Name & RuleSheetNameSuffix On Error Resume Next 'if the rule sheet doesn't exist it will error, we don't care, just move on Application.DisplayAlerts = False TheWB.Worksheets(RuleSheetName).Delete Application.DisplayAlerts = True On Error GoTo EH Dim RuleSheet As Worksheet Set RuleSheet = TheWB.Worksheets.Add SourceSheet.Activate RuleSheet.Name = RuleSheetName RuleSheet.Range(RuleSheet.Cells(1, CellAddrCol), RuleSheet.Cells(1, OperatorCodeCol)).Value = Array("Cell Address", "Rule Type", "Type Code", "Applies To", "Stop", "Font.ColorRGB", "Formula1", "Formula2", _ "Interior.ColorIndexRGB", "Operator Type", "Operator Code") Dim RuleRow As Long RuleRow = 2 Dim RuleCount As Long Dim RptCol As Long Dim SrcCol As Long Dim RetryCount As Long Dim FCCell As Range For SrcCol = 1 To 30 Set FCCell = SourceSheet.Cells(4, SrcCol) For RuleCount = 1 To FCCell.FormatConditions.Count RptCol = 1 Application.StatusBar = "Cell: " & FCCell.Address PrintValue RuleSheet, RuleRow, CellAddrCol, FCCell.Address PrintValue RuleSheet, RuleRow, RuleTypeCol, FCTypeFromIndex(FCCell.FormatConditions.Item(RuleCount).Type) PrintValue RuleSheet, RuleRow, RuleCodeCol, FCCell.FormatConditions.Item(RuleCount).Type PrintValue RuleSheet, RuleRow, AppliesToCol, FCCell.FormatConditions.Item(RuleCount).AppliesTo.Address PrintValue RuleSheet, RuleRow, StopCol, FCCell.FormatConditions.Item(RuleCount).StopIfTrue If FCCell.FormatConditions.Item(RuleCount).Type <> 8 Then PrintValue RuleSheet, RuleRow, Formula1Col, "'" & Right(FCCell.FormatConditions.Item(RuleCount).Formula1, Len(FCCell.FormatConditions.Item(RuleCount).Formula1) - 1) 'remove the leading "=" sign If FCCell.FormatConditions.Item(RuleCount).Type <> 2 And _ FCCell.FormatConditions.Item(RuleCount).Type <> 1 Then PrintValue RuleSheet, RuleRow, Formula2Col, "'" & Right(FCCell.FormatConditions.Item(RuleCount).Formula2, Len(FCCell.FormatConditions.Item(RuleCount).Formula2) - 1) 'remove the leading "=" sign End If End If RetryCount = 0 RetryColor: PrintValue RuleSheet, RuleRow, FontColorCol, "'" & GetRGB(FCCell.FormatConditions(RuleCount).Font.Color) PrintValue RuleSheet, RuleRow, IntColorIdxCol, "'" & GetRGB(FCCell.FormatConditions.Item(RuleCount).Interior.Color) If FCCell.FormatConditions.Item(RuleCount).Type = 1 Then PrintValue RuleSheet, RuleRow, OperatorTypeCol, OperatorType(FCCell.FormatConditions.Item(RuleCount).Operator) PrintValue RuleSheet, RuleRow, OperatorCodeCol, FCCell.FormatConditions.Item(RuleCount).Operator End If RuleRow = RuleRow + 1 Next Next RuleSheet.Rows(1).AutoFilter = True CleanExit: If RuleRow = 2 Then PrintValue RuleSheet, RuleRow, RptCol, "No Conditional Formatted cells were found on " & SourceSheet.Name End If On Error Resume Next Set SourceSheet = Nothing Set TheWB = Nothing Application.StatusBar = "" On Error GoTo 0 MsgBox "Done" Exit Sub EH: If Err.Number = -2147417848 Then MsgBox "Font.Color = " & FCCell.FormatConditions(RuleCount).Font.Color If RetryCount < 5 Then RetryCount = RetryCount + 1 Resume RetryColor Else MsgBox "RetryCount = " & RetryCount Resume Next End If Else MsgBox "Error Number: " & Err.Number & vbCrLf & _ " Description: " & Err.Description & vbCrLf & _ "Cell Address: " & FCCell.Address & vbCrLf Resume Next End If End Sub 

有问题的线是紧跟着RetryColor:标签的线。 当为Unique Values条件格式规则(即突出显示重复项)执行该代码err.number = -2147417848' ,我得到err.number = -2147417848'err.description = "Method 'Color' of object 'Font' failed" 。 代码下降到EH:属于第一个IF语句,并显示MsgBox没有任何问题。

为什么FCCell.FormatConditions(RuleCount).Font.Color语句第一次失败,但在error handling程序中第二次完全执行? 一旦我点击了MsgBox上的OKbutton,执行继续在RetryColor:标签处,语句正确执行,并且一切正常。



为了确保这一点很清楚,如果我注释掉

 MsgBox "Font.Color = " & FCCell.FormatConditions(RuleCount).Font.Color 

EH:行中,代码将错误5次,而不会输出RGB代码到我的输出工作表,然后继续。 如果该行在EH:如上所示),我得到MsgBox ,现在将在主代码中读取.Font.Color ,并且执行将按预期继续而没有错误。



更新:看来,让这个代码坐了一个星期,而我的工作,现在它稍微坏了。 在error handling程序中,我现在得到的标题错误消息popup,向上。 如果我点击F5 ,它将执行并显示颜色代码的MsgBox

所以现在,它会失败两次,然后正确地执行第三次。


为了完整GetRGB ,下面是GetRGB的代码:

 Private Function GetRGB(ByVal ColorCode As Variant) As String Dim R As Long Dim G As Long Dim B As Long If IsNull(ColorCode) Then GetRGB = "0,0,0" Else R = ColorCode Mod 256 G = ColorCode \ 256 Mod 256 B = ColorCode \ 65536 Mod 256 GetRGB = R & "," & G & "," & B End If End Function 

我必须将参数作为Variant传递,因为当颜色select器中的.Font.Color设置为Automatic时,我得到一个返回的NULL ,因此.Font.Color中的If语句。

另一个更新 :在让这段代码再坐几个星期(这是为了让我的生活更轻松,而不是正式的项目,因此它在优先级列表的底部),似乎它会在每一个电话会产生错误,而不仅仅是有时。 但是 ,代码将在即时窗口中正确执行!

混乱的错误!

黄色突出显示的行是产生错误的行,但您可以在即时窗口中看到结果。


另外(我意识到这应该是另一个问题),如果有人快速查看SourceSheet.Activate行的任何原因,请让我知道 – 我没有它的随机错误,所以我把它。通常这些错误是因为在当前活动工作表(在RuleSheet创build完成后)上工作的不合格的引用,但是我认为我的所有引用都是合格的。 如果你看到我错过的东西,请竖起大拇指! 否则,我可能会去CodeReview,让他们看看我错过了什么,一旦我得到这个正常工作。

我想我已经把这个降到了根本原因。

我在Sheet1.A1单元格中手动添加了2种不同types的FormatConditions

在这里输入图像说明

这是我的代码,在同一个工作簿。

 Sub foo() Dim rng As Range Set rng = Sheet1.Range("A1") Dim fc As Object On Error Resume Next Sheet2.Activate Set fc = rng.FormatConditions(1) Debug.Print ActiveSheet.Name, TypeName(fc), fc.Type Debug.Print , fc.Font.Color Set fc = rng.FormatConditions(2) Dim fnt As Font2 Debug.Print ActiveSheet.Name, TypeName(fc), fc.Type Debug.Print , fc.Font.Color Sheet1.Activate Set fc = rng.FormatConditions(1) Debug.Print ActiveSheet.Name, TypeName(fc), fc.Type Debug.Print , fc.Font.Color Set fc = rng.FormatConditions(2) Debug.Print ActiveSheet.Name, TypeName(fc), fc.Type Debug.Print , fc.Font.Color End Sub 

这里是输出:

 Sheet2 FormatCondition 1 3243501 Sheet2 Top10 5 Sheet1 FormatCondition 1 3243501 Sheet1 Top10 5 13998939 

所以FormatConditions.Item方法不会总是返回一个FormatCondition

我无法重现您的即时窗口行为,所以也许你无意中激活了表单?

如果我删除On Error Resume ,并在Top10.Font.Color调用的错误处Top10.Font.Color ,然后在debugging窗口中查询,则会得到:

运行时错误“-2147417848(80010108)”:

自动化错误被调用的对象已从其客户端断开连接。

在Visual Basic中使用早期绑定时,哪些Google将我带到Office自动化出现错误或意外的行为

根据我的结果,当FormatConditions.Item返回Top10 (也可能包括您的UniqueValuestypes在内的其他types)时, 除非范围的表单处于活动状态,否则无法访问Font.Color属性。

但它看起来像你有活动? 我想知道你是否正在改变PrintValue的活动工作表?

关于你的第二个问题:
我一直在设置不在活动工作表中的单元格时遇到问题,最可能的原因是SourceSheet.Activate的问题依赖于Set范围的事实:

 Set FCCell = SourceSheet.Cells(4, SrcCol) 

我发现,如果工作表没有激活,它将在cells()参数内失败,我认为最好的方法是使用Range之前的单元格。
情况可能如此 。 所以对于这个例子我会做这样的事情:

 With SourceSheet:Set FCCell = .Range(.Cells(4,SrcCol):End With