「Excel VBA」 GoTo文でループ処理をスキップするサンプル

書式
GoTo ラベル名
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Sub testFunc()
Dim n As Integer
n = 10
'Do Whileループ
Do While n < 30
'ラベルInfo
Info:
If (n = 19) Then
n = n + 3
'gotoラベル
GoTo Info
End If
'デバッグの値を出力
Debug.Print n
n = n + 3
Loop
End Sub
Sub testFunc() Dim n As Integer n = 10 'Do Whileループ Do While n < 30 'ラベルInfo Info: If (n = 19) Then n = n + 3 'gotoラベル GoTo Info End If 'デバッグの値を出力 Debug.Print n n = n + 3 Loop End Sub
Sub testFunc()

  Dim n As Integer
  n = 10
  'Do Whileループ
  Do While n < 30
  'ラベルInfo
Info:
      If (n = 19) Then
        n = n + 3
        'gotoラベル
        GoTo Info
      End If
      'デバッグの値を出力
      Debug.Print n
      n = n + 3
  Loop
End Sub

実行結果
10
13
16
22
25
28

Excel VBA

Posted by arkgame