所以在错误处理程序中如果有一些相关操作有可能触发错误的情况下可以先加上On Error Resume Next。 2>错误处理程序代码可以在程序中的任何地方写入。 推荐错误处理程序代码在Exit Sub语句之后,而在End Sub语句之前,从而与过程中的流程分开。 3>当处理在访问其它对象期间产...
可以直接退出sub了,不需要往下继续执行errorHandle标签"ExitSub"便签通常放在代码的最后,错误出现,从错误地方跳到这里执行代码."errorhandle:"这里错误号码Err.Number=0就是没有error,>0就是有error"IfErr.Number> 0 Then"有错误输出Err.Description&Err.Number跟OnErrorGoTo0的信息是一样的....
「已啟用」錯誤處理程式是由 On Error 語句開啟的錯誤處理程式;「作用中」錯誤處理程式是正在處理錯誤的已啟用處理程式。 如果在錯誤處理程式作用中時發生錯誤, (發生錯誤與 Resume、Exit Sub、Exit 函數或Exit 屬性語句) 之間,則目前程式的錯誤處理程式無法處理錯誤。 控件會回到呼叫程式。 如果呼叫程式已啟用錯誤處...
对应退出语句: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 ...
Exit Sub Else MsgBox "Thanks for the input." End If End Sub 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’ “...
在VBA中,使用Exit Sub语句的一般语法如下: 代码语言:txt 复制 Sub ProcedureName() ' 代码块 If condition Then ' 条件满足时执行的代码 Exit Sub ' 提前退出当前的子过程 End If ' 代码块 End Sub 在上述代码中,如果条件满足,则会执行Exit Sub语句,跳出当前的子过程,不再执行后续的代码。 Exit Sub语句的...
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...
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文件尚未打开,那么程序会在后台打开该工作簿。