開始するループがある場合:
For each c in Range("A1:C8")
c (c.count, c.value, c.something,...)
これまでにループが繰り返された回数を識別するプレースホルダーのプロパティはありますか? 別の変数を含めるよりも、このようなものを使用したいと思います。
「範囲内の各 c に対して」を使用する代わりに、次のようなことができます。
Dim c as Long 'presumably you did this as a Range, just change it to Long.
Dim myRange as Range 'Use a range variable which will be easier to call on later
Set myRange = Range("A1:C8")
For c = 1 to myRange.Cells.Count
'Do something to the cell, identify it as myRange.Cells(c), for example:
myRange.Cells(c).Font.Bold = True '<--- replace with your code that affects the cell
Next
これにより、不要なカウンター変数を含めずに、まったく同じ For/Next ループを実行できます。この場合、c
はカウンターですが、コードによって影響を受けるセルを識別する目的にも役立ちます。
このように自分で数える必要があります
Dim i as integer
i = 0
For each c in Range("A1:C8")
i = i + 1
または
Dim i as integer
Dim c as Range
For i = 0 to Range("A1:C8").Count - 1
Set c = Range("A1:C8").Cells(i)
(改訂)
反復する方向に応じて、列または行のプロパティを使用して、その場で序数を計算できます。したがって
For Each c1 in myRange
myOrdinal = c1.row - myRange.row + 1 ' down contiguous cells in one column
myOrdinal = c1.Column - myRange.Column + 1 ' contiguous columns, L2R
Next