第八课 VBA分支与退出语句

2017-12-18  本文已影响79人  流星雨的梦
 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
   Range("b1") = 100
End Sub

2)Exit function

Function ff()
   Dim x As Integer
   For x = 1 To 100
      If x = 5 Then
          Exit Function
      End If
   Next x
   ff = 100
End Function

3)Exit for

Sub e2()
   Dim x As Integer
   For x = 1 To 100
         Cells(1, 1) = x
         If x = 5 Then
             Exit For
         End If
   Next x
   Range("b1") = 100
End Sub

4)Exit do

Sub e3()
    Dim x As Integer
    Do
         x = x + 1
         Cells(1, 1) = x
          If x = 5 Then
             Exit Do
          End If
    Loop Until x = 100
    Range("b1") = 100
End Sub
Sub t1()
   Dim x As Integer
   Dim sr
100:
   sr = Application.InputBox("请输入数字", "输入提示")
   If Len(sr) = 0 Or Len(sr) = 5 Then GoTo 100
End Sub
Sub t2()
   Dim x As Integer
   For x = 1 To 10
      If Cells(x, 1) Mod 2 = 0 Then GoSub 100
   Next x
   Exit Sub
100:
   Cells(x, 1) = "偶数"
   Return      '跳到gosub 100 这一句'
End Sub
Sub t3()
   On Error Resume Next
   Dim x As Integer
   For x = 1 To 10
       Cells(x, 3) = Cells(x, 2) * Cells(x, 1)
   Next x
End Sub
Sub t4()
   On Error GoTo 100
   Dim x As Integer
   For x = 1 To 10
      Cells(x, 3) = Cells(x, 2) * Cells(x, 1)
   Next x
   Exit Sub
100:
   MsgBox "在第" & x & "行出错了"
End Sub
Sub t5()
   On Error Resume Next
   Dim x As Integer
   For x = 1 To 10
       If x > 5 Then On Error GoTo 0
           Cells(x, 3) = Cells(x, 2) * Cells(x, 1)
   Next x
   Exit Sub
End Sub
上一篇下一篇

猜你喜欢

热点阅读