在VBA中,使用Exit Sub语句的一般语法如下: 代码语言:txt 复制 Sub ProcedureName() ' 代码块 If condition Then ' 条件满足时执行的代码 Exit Sub ' 提前退出当前的子过程 End If ' 代码块 End Sub 在上述代码中,如果条件满足,则会执行Exit Sub语句,跳出当前的子过程,不再执行后续的代码。 Exit Sub语句的...
Exit Sub MsgBox Application.WorksheetFunction.RoundUp(num, 1) End Sub 从上面的例子中,我在消息框之前添加了退出子代码,当我们运行这段代码时,没有出现任何消息框。 让我们再举一个使用if-else语句的示例。如果语句为true,它将执行代码直到结束。否则,它会破裂。 示例代码: # VBA Sub roundFunc() D...
# VBA Sub roundFunc() Dim num As Double num = 5.468 Exit Sub MsgBox Application.WorksheetFunction.RoundUp(num, 1) End Sub 從上面的例子中,我在訊息框之前新增了退出子程式碼,當我們執行這段程式碼時,沒有出現任何消息框。 讓我們再舉一個使用if-else語句的示例。如果語句為true,它將執行程式...
使用Exit Do语句退出。 三、退出Sub过程 使用Exit Sub语句退出。 四、退出函数Fuction 使用Exit Function语句退出。
VBA笔记退出循环、Sub、Fuction等的Exit语句 Exit语句,可以用来跳出、退出各种循环。以下是几种VBA循环及过程、函数等的退出语句代码。 一、For循环的退出 For Each myCell in Range("A1:H10") If myCell.Value = "" Then myCell.Value = "empty" Else Exit For End If Next myCell 以上是For Each的退出...
SubVBA_ExitSub1()DimAAs IntegerEnd Sub Step 4:Now open aDo WhileLoop as shown below. Code: SubVBA_ExitSub1()DimAAs IntegerDoWhileEnd Sub Step 5:In betweenDo Whilewe write a simple code of comparison. Let’s DO addition of variable A with +1. Which means DO Addition for A with its...
exit=TrueThenExitSubEndIfEndSubPrivateSub变量初始化()yn_exit=FalseEndSubPrivateSub子程序1()DimiAs...
Exit语句,可以用来跳出、退出各种循环。以下是几种VBA循环及过程、函数等的退出语句代码。 一、For循环的退出 For Each myCell in Range("A1:H10") If myCell.Value = "" Then myCell.Value = "empty" Else Exit For End If Next myCell 以上是For Each的退出方法。如果是For to 结构的循环,同样使用Exi...
'End Structure' 之前必須搭配相對應的 'Structure' 必須有 'End Sub' 'End Sub' 之前必須搭配相對應的 'Sub' End Sub' 必須是該行的第一個陳述式 'End SyncLock' 之前必須搭配相對應的 'SyncLock' 必須有結束標記 </tagName> 'End Try' 之前必須搭配相對應的 'Try' 'End Using' 之前必須搭配相對...
Sub vba_exit_sub_on_error() On Error GoTo iError Range("A1") = 10 / 0 iError: MsgBox "You can't divide with the zero." & _ "Change the code." Exit Sub End Sub In the above code, you have an error handler, “iError” with a message box and then the “Exit Sub” Stateme...