可以直接退出sub了,不需要往下继续执行errorHandle标签"ExitSub"便签通常放在代码的最后,错误出现,从错误地方跳到这里执行代码."errorhandle:"这里错误号码Err.Number=0就是没有error,>0就是有error"IfErr.Number> 0 Then"有错误输出Err.Description&Err.Number跟OnErrorGoTo0的信息是一
所以在错误处理程序中如果有一些相关操作有可能触发错误的情况下可以先加上On Error Resume Next。 2>错误处理程序代码可以在程序中的任何地方写入。 推荐错误处理程序代码在Exit Sub语句之后,而在End Sub语句之前,从而与过程中的流程分开。 3>当处理在访问其它对象期间产...
Cells(sfRow, slCol)) If slrg Is Nothing Then Exit Sub ' no data Dim dws As Worksheet: Set dws = wb.Worksheets(dName) Dim dlrg As Range: Set dlrg = RefColumn(dws.Cells(dfRow, dlCol)) If dlrg Is Nothing Then Exit Sub ' no data Dim slAddress As String: slAddress = slrg.Address...
On Error Exit Sub One of the best things about the “Exit Sub” you can use it to exit the procedure when an error occurs. Below is the code that divides a number with a zero that returns a “Run-time error ‘11’ “ and stops the execution. Here you can use the GoTo statement ...
Exit Property立即退出显示它的Property过程。 继续执行称为Property的过程之后的语句。 Exit Sub立即退出显示它的Sub过程。 继续执行称为Sub的过程之后的语句。 备注 请勿将Exit语句和End语句搞混。Exit不定义结构的末尾。 示例 此示例使用Exit语句退出For...Next循环、Do...Loop和Sub过程。
" MsgBox Msg, , "Deferred Error Test" Err.Clear ' Clear Err object fields End If Exit Sub ' Exit to avoid handler. ErrorHandler: ' Error-handling routine. Select Case Err.Number ' Evaluate error number. Case 55 ' "File already open" error. Close #1' Close open file.CaseElse' Handle...
对应退出语句:Exit For、 Exit Do、 Exit Sub、Exit Function。 如果要实现其他语言的 continue 的效果,可以使用利用goto语句进行模拟。比如要打印 100 以内 3 的倍数的和: Sub print3() Dim i As Integer, count As Long For i = 1 To 100 If i Mod 3 <> 0 Then GoTo CONTINUE ' 如果不能被3整除...
Sub t2() 17 Dim x As Integer 18 For x = 1 To 10 19 If Cells(x, 1) Mod 2 = 0 Then GoSub 100 20 Next x 21 Exit Sub 22 100: 23 Cells(x, 1) = "偶数" 24 Return '跳到gosub 100 这一句 25 End Sub 26 27 'on error resume next '遇到错误,跳过继续执行下一句 28 29 Sub ...
MsgBox"File not found or error occurred."Exit Sub End If ' 获取内容 content=wb.Sheets(1).Range("A1").Value2 MsgBox content wb.Close SaveChanges:=False MsgBox"Done!"End Sub 使用GetObject函数,获取对指定Excel文件的引用。如果Excel文件尚未打开,那么程序会在后台打开该工作簿。
“启用的”错误处理程序是指由On Error语句打开的错误处理程序;“活动”错误处理程序是指处理错误的过程中启用的处理程序。 如果在错误处理程序处于活动状态时发生错误, (错误发生与) 的 Resume、Exit Sub、Exit 函数或Exit Property语句之间发生错误,则当前过程的错误处理程序无法处理该错误。 控制权将返回给调用过程...