On Error Resume Next 会使程序从紧随产生错误的语句之后的语句继续执行,或是从紧随最近一次调用含有 On Error Resume Next 语句的过程的语句继续运行。这个语句可以置运行时错误于不顾,使程序得以继续执行。可以将错误处理程序放置在错误发生的地方,而不必将控件传输到过程中的其它位置。在调用另一个过程时,On Error Resume Next 语句成为非活动的,所以,如果希望在例程中进行嵌入错误处理,则应在每一个调用的例程中执行 On Error Resume Next 语句。
注意 当处理在访问其它对象期间产生的错误时,与其使用 On Error GoTo 指令,不如使用 On Error Resume Next。每次和对象打交道,在不知道用代码访问哪个对象时,检查一下 Err 都会打消这种疑虑。可以确定是哪个对象产生错误(Err.Source 中指定的对象),也可以确定是哪个对象将错误代码放在 Err.Number 中。
On Error GoTo 0 停止在当前过程中处理错误。即使过程中包含编号为 0 的行,它也不把行 0 指定为处理错误的代码的起点。如果没有 On Error GoTo 0 语句,在退出过程时,错误处理程序会自动关闭。
在错误未发生的时候,为了防止错误处理程序代码运行,请像在下段程序中那样,在紧靠着错误处理程序的前面写入 Exit Sub、Exit Function 或 Exit Property 语句。
Sub InitializeMatrix(Var1, Var2, Var3, Var4)
On Error GoTo ErrorHandler
. . .Exit SubErrorHandler:. . .Resume NextEnd Sub
此处,错误处理程序代码在 Exit Sub 语句之后,而在 End Sub 语句之前,从而与过程中的流程分开。错误处理程序代码可以在程序中的任何地方写入。
The Resume statement instructs VBA to resume execution at a specified point in the code. You can use Resume only in an error handling block; any other use will cause an error. Moreover, Resume is the only way, aside from exiting the procedure, to get out of an error handling block. Do not use the Goto statement to direct code execution out of an error handling block. Doing so will cause strange problems with the error handlers.
The Resume statement takes three syntactic form:
Resume Resume Next Resume <label>
Used alone, Resume causes execution to resume at the line of code that caused the error. In this cause you must ensure that your error handling block fixed the problem that caused the initial error. Otherwise, your code will enter an endless loop, jumping between the line of code that caused the error and the error handling block. The following code attempts to activate a worksheet that does not exist. This causes an error (9 - Subscript Out Of Range), and the code jumps to the error handling block which creates the sheet, correcting the problem, and resumes execution at the line of code that caused the error.
On Error GoTo ErrHandler: Worksheets("NewSheet").Activate Exit Sub
ErrHandler: If Err.Number = 9 Then ' sheet does not exist, so create it Worksheets.Add.Name = "NewSheet" ' go back to the line of code that caused the problem Resume End If
The second form of Resume is Resume Next . This causes code execution to resume at the line immediately following the line which caused the error. The following code causes an error (11 - Division By Zero) when attempting to set the value of N. The error handling block assigns 1 to the variable N, and then causes execution to resume at the statement after the statement that caused the error.
On Error GoTo ErrHandler: N = 1 / 0 Debug.Print N Exit Sub
ErrHandler: N = 1 ' go back to the line following the error Resume Next
The third form of Resume is Resume <label>: . This causes code execution to resume at a line label. This allows you to skip a section of code if an error occurs. For example,
On Error GoTo ErrHandler: N = 1 / 0 ' ' code that is skipped if an error occurs ' Label1: ' ' more code to execute ' Exit Sub
ErrHandler: ' go back to the line at Label1: Resume Label1:
All forms of the Resume clear or reset the Err object.