|
For...Nextステートメントは指定した回数だけForとNext間を繰り返して実行します。 For i = a to b (Step c): (Exit For) : Next i
Exit Forは、カウンタ変数が最終値になる前にFor...Nextのループを抜け出したい場合に使います。たいていは、If...Thenステートメントとあわせて利用します。 1行目に列番号をふります Sub Sample1() Dim i As Integer For i = 1 To 256 Cells(1, i).Value = i Next i End Sub 1行目の奇数列のセルの背景を黄色にします Sub Sample2() Dim i As Integer For i = 1 To 255 Step 2 Cells(1, i).Interior.ColorIndex = 6 Next i End Sub A1からG7までに31という値があればセルの背景を黄色にします Sub Sample3() Dim i As Byte Dim j As Byte For i = 1 To 7 For j = 1 To 7 If Cells(i, j).Value = 31 Then Cells(i, j).Interior.ColorIndex = 6 Exit For End If Next j Next i End Sub |