VBA基础学习之1.5循环语句( 二 )


Private Sub Constant_demo_Click()Dim a As Integera = 10For i = 0 To a Step 2 'i is the counter variable and it is incremented by 2MsgBox ("The value is i is : " & i)If i = 4 Theni = i * 10 'This is executed only if i=4MsgBox ("The value is i is : " & i)Exit For 'Exited when i=4End IfNextEnd Sub
1.5.6 Exit Do语句
当想要根据特定标准退出Do循环时 , 可使用Exit Do语句 。它可以同时用于Do…While和Do…Until直到循环 。
当Exit Do被执行时 , 控制器在Do循环之后立即跳转到下一个语句 。
【VBA基础学习之1.5循环语句】Private Sub Constant_demo_Click()i = 0Do While i <= 100If i > 10 ThenExit Do' Loop Exits if i>10End IfMsgBox ("The Value of i is : " & i)i = i + 2LoopEnd Sub