在VBA中,使用Exit Sub语句的一般语法如下: 代码语言:txt 复制 Sub ProcedureName() ' 代码块 If condition Then ' 条件满足时执行的代码 Exit Sub ' 提前退出当前的子过程 End If ' 代码块 End Sub 在上述代码中,如果条件满足,则会执行Exit Sub语句,跳出当前的子过程,不再执行后续的代码。 Exit Sub语句的...
Sub Example() Dim rangeValue As Integer rangeValue = 5 ' 假设范围值为5 If rangeValue = 0 Then Exit Sub ' 如果范围值为0,则结束Sub过程 End If ' 继续执行其他代码 End Sub 在上述示例中,如果rangeValue的值为0,则会执行Exit Sub语句,从而提前结束Sub过程。否则,会继续执行其他代码。
If 条件成立 Then 执行1 Else 执行2 End If '也可以写成一行 '但不推荐这样做,在复制代码时无法自动缩进 If 条件成立 Then 执行语句1 Else 执行语句2 嵌套使用 If条件1Then执行1ElseIf条件2Then执行2Else执行3EndIfEndIf 嵌套省略形式 If 条件1 Then 执行1 ElseIf 条件2 Then 执行2 ElseIf 条件3 Then 执...
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 结构的循环,同样使用Exit For语句来退出。 二、Do 或 Do While语句的退出 使用Exit Do语句退出。 三、退出Sub过程 使用Exit Sub语...
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的退出...
End If 或: If <条件> Then [指令] Else [指令] End If 当条件为真时,执行Then后面的语句并结束If…Then语句的执行,否则执行Else后面的语句或结束If…Then语句的执行。 ②当有两个或多个条件时,可使用嵌套的If … Then 结构: If <条件> Then ...
第一部分:END语句与Exit语句 一、END语句 作用:强制退出所有正在运行的程序。 二、Exit语句 作用:退出指定的语句 例:执行x从1到100,当x=5时停止: 1、Exit Sub(退出整个sub) Sub e1() Dim x As Integer For x = 1 To 100 Cells(1, 1) = x If x = 5 Then Exit Sub End If Next x End Sub ...
此示例使用Exit语句退出For...Next循环、Do...Loop和Sub过程。 VB复制 SubExitStatementDemo()DimI, MyNumDo' Set up infinite loop.ForI =1To1000' Loop 1000 times.MyNum = Int(Rnd *1000)' Generate random numbers.SelectCaseMyNum' Evaluate random number.Case7:ExitFor' If 7, exit For...Next....
If Length = 0 Or TheWidth = 0 Then ' If either argument = 0. Exit Sub ' Exit Sub immediately. End If Area = Length * TheWidth ' Calculate area of rectangle. Debug.Print Area ' Print Area to Debug window. End Sub 另请参阅 调用Sub 和 Function 过程 了解命名参数和可选参数 ...
If Not VBA.IsObject(dic) Then Exit Sub If dic Is Nothing Then Exit Sub If Me.ListBox1.ListIndex < 0 Then Exit Sub If Me.ListBox1.Value = Null Then Exit Sub If dic.exists(VBA.CInt(Me.ListBox1.Value)) Then '如果存在键 dStr = dic.Item(VBA.CInt(Me.ListBox1.Value)) dic.Remove...