无法处理运行时错误9错误转到

我有以下的代码。 但是错误没有得到处理

Sub Some_sub() Some code here On Error GoTo resumeCode If Workbooks("Some file.xlsm").ReadOnly Then GoTo readOnlyError End If resumeCode: On Error GoTo fileNotOpen Workbooks("Some file.xlsm").Activate some more code here Exit Sub fileNotOpen: MsgBox "Error: Claims followup.xlsm is not open." & Chr(10) & Chr(10) & "Open the file in read/write" Exit Sub End Sub 

当我运行debugging模式时,它显示我这一行: Workbooks("Some file.xlsm").Activate黄色。 而不是处理错误,并去标签。

在工具 – >选项 – >常规选项卡下的VBA中:打开未处理的错误处于活动状态。

当我打开文件时,它会像运行代码一样运行代码。 closures时不处理错误。

我究竟做错了什么?

在此先感谢您的帮助

而已。 正如我在评论中所说的,当发生错误,并且您处理它时,您将无法设置新的On Error机制,除非您以任何方式显式调用Resume关键字。

一种可能的方式来实现它,如果你不想改变你的例程stream,只是在你的新的On Error语句和Resume之前添加一个标签。 像这样,例如:

 Sub Some_sub() ' Some code ... On Error GoTo resumeCode If Workbooks("Some file.xlsm").ReadOnly Then GoTo readOnlyError End If ' Some more code ... resumeCode: Resume ResumeHere ' <---------------------------------------- Invoke Resume ResumeHere: ' <------------------------------------- Add some label On Error GoTo fileNotOpen Workbooks("Some file.xlsm").Activate Exit Sub fileNotOpen: msgBox "Error: Claims followup.xlsm is not open." & Chr(10) & Chr(10) & "Open the file in read/write" Exit Sub End Sub 

你应该小心,如果错误状态是空白的并且从正常stream程到达,那么关键字Resume本身会引发一个错误。在这种情况下,你应该把error handling部分放在你的例程的最后,然后继续在正常stream量范围内的任何标签。 这通常是通常的做法。