VBA 在条件格式中使用 UDF 时,编译器不会中断/捕获错误并且没有错误消息
VBA compiler not breaking / trapping on errors and no error message, when using UDF in conditional formatting
看到新的发展。
我在 Excel 中遇到了一个奇怪的问题。我有一个正在使用的 Worksheet_Change
事件,我正在尝试调试它。我保存程序并重新打开它,突然间编译器没有因错误而中断。事实上,它根本没有破裂!!我会在子句的开头(以及接下来的三行)休息一下,但它并没有发生。我想也许事件没有启用......所以,我把一个消息框作为第一行代码之一。弹出消息框....即使它的行中断。
这在另一个宏的特定行之前发生过一次,我尝试将所有内容复制到 .txt 文件中并将其粘贴回我的程序的早期版本中。这工作了几个月,但现在问题又回来了。
编码并不是很重要,但我会把它粘贴在下面以供娱乐和咯咯笑。无论我是否删除所有 "on error",它都会无误地中止。我已经将代码剪切并粘贴到一个新的 sub 中,它工作正常。我还检查了选项并检查了 "break on all errors." 什么都没有,即使是未定义的调用也不会抛出错误,会阻止程序中止。
Private Sub Worksheet_Change(ByVal target As Range)
Application.EnableEvents = False
Dim aVar() As String
Dim iVar As Integer
On Error GoTo 0
MsgBox "you changed something" 'this is a msgbox that does pop up during execution, verifying that the sub did in fact, run.
Call iRandomNonsense 'this is a sub that does not exist which the compiler does not tell me about any more.
If target.Columns.Count = 1 Then
Select Case target.Column
Case 2
If target.Count = 1 And Cells(target.Row, 1) = "" Then _
Cells(target.Row, 1) = Now
Case 8
On Error GoTo ExitSub
aVar = Split(target.Value)
For Each sVar In aVar
If IsNumeric(sVar) And Len(sVar) = 5 Then
If sVar > 30000 Then
aVar(iVar) = "ALN-" & sVar
Else
aVar(iVar) = "DEV-" & sVar
End If
End If
iVar = iVar + 1
Next
target.Value = Join(aVar, " ")
End Select
Else
On Error GoTo ExitSub
target.Resize(target.Rows.Count, Cells(target.Row, target.Columns.Count).End(xlToLeft).Column + 1 - target.Column).Select
Select Case Selection.Columns.Count
Case 18, 21 'Paste from Scrap report
Debug.Print "Paste from Scrap report" & Now
Call purgeCheckboxes
With Selection
.Copy
.PasteSpecial (xlValues)
End With
OnSelRow(4, 8).Select
Selection.Copy Destination:=OnSelRow(1)
'desc
OnSelRow(6) = OnSelRow(10)
OnSelRow(4) = OnSelRow(15)
With Range(Cells(Selection.Row, 10), Cells(Selection.Row + Selection.Rows.Count - 1, 10))
.FormulaR1C1 = _
"=RC[2]&"" ""&RC[3]&"" ""&RC[-3]&"" ""&RC[4]&"" ""&RC[7]&"" ""&RC[11]"
.Copy
.PasteSpecial (xlValues)
End With
Application.CutCopyMode = False
Range(Cells(Selection.Row, 7), Cells(Selection.Row + Selection.Rows.Count - 1, 7)).FormulaR1C1 = "TRUE"
Range(Cells(Selection.Row, 8), Cells(Selection.Row + Selection.Rows.Count - 1, 8)).FormulaR1C1 = "T D Q 9 A Wav DMR"
Range(Cells(Selection.Row, 9), Cells(Selection.Row + Selection.Rows.Count - 1, 9)).FormulaR1C1 = "2"
Range(Cells(Selection.Row, 11), Cells(Selection.Row + Selection.Rows.Count - 1, 11)).Select
Range(Selection, Cells(Selection.Row, UsedRange.Columns.Count)).Select
Selection.ClearContents
ActiveWindow.ScrollColumn = 1
End Select
Call RefreshCondFormats
End If
ExitSub:
On Error GoTo 0
Application.EnableEvents = True
End Sub
新进展:
我听从了其中一条评论中的建议。 "Long shot: do you have any conditional formatting that uses UDFs? – Rory yesterday" 它解决了我删除条件格式中的用户公式时出现的中断错误。现在编译器停止了,当我注释掉 "iRandomNonsense" 时,它在我的命令中中断了。当我把格式放回去时,它又搞砸了。
Rory,请将您的评论作为答案(并详细说明您是如何得出结论的),我会核实给您。
如果有人愿意,我真的很想知道解决 excel 中这个故障的方法。这似乎是我将来可能会使用的实用程序,这真的让我很困扰我不能以条件格式使用用户函数。此外,这段代码对我非常有用,如果没有条件格式中的用户公式或毛茸茸的自动更正代码,我看不到任何其他方法可以完成我所做的事情。
我建议使用 Rob Bovey 的代码清理器来清除可能导致此类错误的累积 p 代码。当 Excel VBA 开始表现异常时,清理代码通常可以解决问题。
http://www.appspro.com/Utilities/CodeCleaner.htm
如果您在条件格式中使用 UDF,则可能会出现此类问题。如果您没有错误处理,或者如果您尝试访问 .Value
或 .Formula
以外的任何属性,则很可能是这种情况。通常可以解决这个问题 - 例如使用替代计算,或将 UDF 放入单元格中 - 但有时您可能只是运气不好。
看到新的发展。
我在 Excel 中遇到了一个奇怪的问题。我有一个正在使用的 Worksheet_Change
事件,我正在尝试调试它。我保存程序并重新打开它,突然间编译器没有因错误而中断。事实上,它根本没有破裂!!我会在子句的开头(以及接下来的三行)休息一下,但它并没有发生。我想也许事件没有启用......所以,我把一个消息框作为第一行代码之一。弹出消息框....即使它的行中断。
这在另一个宏的特定行之前发生过一次,我尝试将所有内容复制到 .txt 文件中并将其粘贴回我的程序的早期版本中。这工作了几个月,但现在问题又回来了。
编码并不是很重要,但我会把它粘贴在下面以供娱乐和咯咯笑。无论我是否删除所有 "on error",它都会无误地中止。我已经将代码剪切并粘贴到一个新的 sub 中,它工作正常。我还检查了选项并检查了 "break on all errors." 什么都没有,即使是未定义的调用也不会抛出错误,会阻止程序中止。
Private Sub Worksheet_Change(ByVal target As Range)
Application.EnableEvents = False
Dim aVar() As String
Dim iVar As Integer
On Error GoTo 0
MsgBox "you changed something" 'this is a msgbox that does pop up during execution, verifying that the sub did in fact, run.
Call iRandomNonsense 'this is a sub that does not exist which the compiler does not tell me about any more.
If target.Columns.Count = 1 Then
Select Case target.Column
Case 2
If target.Count = 1 And Cells(target.Row, 1) = "" Then _
Cells(target.Row, 1) = Now
Case 8
On Error GoTo ExitSub
aVar = Split(target.Value)
For Each sVar In aVar
If IsNumeric(sVar) And Len(sVar) = 5 Then
If sVar > 30000 Then
aVar(iVar) = "ALN-" & sVar
Else
aVar(iVar) = "DEV-" & sVar
End If
End If
iVar = iVar + 1
Next
target.Value = Join(aVar, " ")
End Select
Else
On Error GoTo ExitSub
target.Resize(target.Rows.Count, Cells(target.Row, target.Columns.Count).End(xlToLeft).Column + 1 - target.Column).Select
Select Case Selection.Columns.Count
Case 18, 21 'Paste from Scrap report
Debug.Print "Paste from Scrap report" & Now
Call purgeCheckboxes
With Selection
.Copy
.PasteSpecial (xlValues)
End With
OnSelRow(4, 8).Select
Selection.Copy Destination:=OnSelRow(1)
'desc
OnSelRow(6) = OnSelRow(10)
OnSelRow(4) = OnSelRow(15)
With Range(Cells(Selection.Row, 10), Cells(Selection.Row + Selection.Rows.Count - 1, 10))
.FormulaR1C1 = _
"=RC[2]&"" ""&RC[3]&"" ""&RC[-3]&"" ""&RC[4]&"" ""&RC[7]&"" ""&RC[11]"
.Copy
.PasteSpecial (xlValues)
End With
Application.CutCopyMode = False
Range(Cells(Selection.Row, 7), Cells(Selection.Row + Selection.Rows.Count - 1, 7)).FormulaR1C1 = "TRUE"
Range(Cells(Selection.Row, 8), Cells(Selection.Row + Selection.Rows.Count - 1, 8)).FormulaR1C1 = "T D Q 9 A Wav DMR"
Range(Cells(Selection.Row, 9), Cells(Selection.Row + Selection.Rows.Count - 1, 9)).FormulaR1C1 = "2"
Range(Cells(Selection.Row, 11), Cells(Selection.Row + Selection.Rows.Count - 1, 11)).Select
Range(Selection, Cells(Selection.Row, UsedRange.Columns.Count)).Select
Selection.ClearContents
ActiveWindow.ScrollColumn = 1
End Select
Call RefreshCondFormats
End If
ExitSub:
On Error GoTo 0
Application.EnableEvents = True
End Sub
新进展: 我听从了其中一条评论中的建议。 "Long shot: do you have any conditional formatting that uses UDFs? – Rory yesterday" 它解决了我删除条件格式中的用户公式时出现的中断错误。现在编译器停止了,当我注释掉 "iRandomNonsense" 时,它在我的命令中中断了。当我把格式放回去时,它又搞砸了。
Rory,请将您的评论作为答案(并详细说明您是如何得出结论的),我会核实给您。
如果有人愿意,我真的很想知道解决 excel 中这个故障的方法。这似乎是我将来可能会使用的实用程序,这真的让我很困扰我不能以条件格式使用用户函数。此外,这段代码对我非常有用,如果没有条件格式中的用户公式或毛茸茸的自动更正代码,我看不到任何其他方法可以完成我所做的事情。
我建议使用 Rob Bovey 的代码清理器来清除可能导致此类错误的累积 p 代码。当 Excel VBA 开始表现异常时,清理代码通常可以解决问题。
http://www.appspro.com/Utilities/CodeCleaner.htm
如果您在条件格式中使用 UDF,则可能会出现此类问题。如果您没有错误处理,或者如果您尝试访问 .Value
或 .Formula
以外的任何属性,则很可能是这种情况。通常可以解决这个问题 - 例如使用替代计算,或将 UDF 放入单元格中 - 但有时您可能只是运气不好。