Sub Sample3()
Dim c As Range, i As Long
For Each c In Selection
i = i + 1
c.Value = i
Next c
End Sub
もちろん、For EachステートメントではなくFor Nextステートメントでも操作できます。
Sub Sample4()
Dim i As Long
For i = 1 To Selection.Count
Selection(i) = i
Next i
End Sub
もう少し実用的な例を示しましょう。
次のマクロは、選択した範囲を、1行おきに塗りつぶします。
Sub Sample5()
Dim i As Long, S As Long, E As Long
S = Selection(1).Column
E = Selection(Selection.Count).Column
For i = Selection(1).Row To Selection(Selection.Count).Row Step 2
Range(Cells(i, S), Cells(i, E)).Interior.ColorIndex = 15
Next i
End Sub