Sub 繰り返しForNext()
Dim i As Long, j As Long
For i = 1 To 5
For j = 1 To 5
Cells(i, j) = (i - 1) * 5 + j
Next j
Next i
End Sub
Sub 繰り返しForEach()
Dim WS As Worksheet
For Each WS In Worksheets
Debug.Print WS.Name
Next WS
End Sub
Sub 繰り返しForEachをForNextで()
Dim i As Long
For i = 1 To Worksheets.Count
Debug.Print Worksheets(i).Name
Next i
End Sub
Sub 繰り返しForEachRange()
Dim rng As Range
For Each rng In Range("A1:B3")
Debug.Print rng.Value
Next rng
End Sub
Sub 繰り返しForEachSelect()
Dim rng As Range
Range("A1:B3").Select
For Each rng In Selection
Debug.Print rng.Value
Next rng
End Sub
Sub 繰り返しForEachAry()
Dim Caramel(4) As String
Dim Taste As Variant
Caramel(0) = "りんご"
Caramel(1) = "バナナ"
Caramel(2) = "いちご"
Caramel(3) = "レモン"
Caramel(4) = "ぶどう"
For Each Taste In Caramel
Debug.Print Taste
Next Taste
End Sub
Sub 繰り返しDoLoopBefore()
Dim Tensu As String 'テストの点数
Dim Flg As Boolean '数値が入力されたかどうかのフラグ
Flg = True
Do Until Flg = True
Tensu = InputBox("点数は?")
Flg = IsNumeric(Tensu)
Loop
End Sub
Sub 繰り返しDoLoopAfter()
Dim Tensu As String 'テストの点数
Dim Flg As Boolean '数値が入力されたかどうかのフラグ
Flg = True
Do
Tensu = InputBox("点数は?")
Flg = IsNumeric(Tensu)
Loop While Flg = False
End Sub
Sub 繰り返しExitFor()
Dim i As Long, Goukei As Long
For i = 1 To 100
Goukei = Goukei + i * i
Debug.Print Goukei
If Goukei > 100 Then Exit For
Next i
End Sub
Sub 繰り返しExitDo()
Dim SaikoronoMe As Long, Kaisu As Long
Kaisu = 0
Do While Kaisu < 10
SaikoronoMe = Int((6 * Rnd) + 1)
Debug.Print SaikoronoMe
Kaisu = Kaisu + 1
If SaikoronoMe = 3 Then Exit Do
Loop
If SaikoronoMe = 3 Then
Debug.Print Kaisu & "回目"
Else
Debug.Print "出ませんでした!"
End If
End Sub
Sub 入れ子ExitFor()
Dim i As Long, j As Long
For i = 1 To 5
For j = 1 To 5
If i = j Then Exit For
Cells(i, j) = (i - 1) * 5 + j
Next j
Next i
End Sub
Sub DoLoopForEachExitDo()
Dim WS As Worksheet
Do
For Each WS In Worksheets
Debug.Print WS.Name
If WS.Name = "For Each" Then Exit Do
Next WS
Debug.Print "For Each 抜けました!"
Loop
Debug.Print "Exit Doしました!"
End Sub
コメント