5

開始するループがある場合:

For each c in Range("A1:C8")

c (c.count, c.value, c.something,...)これまでにループが繰り返された回数を識別するプレースホルダーのプロパティはありますか? 別の変数を含めるよりも、このようなものを使用したいと思います。

4

3 に答える 3

4

「範囲内の各 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はカウンターですが、コードによって影響を受けるセルを識別する目的にも役立ちます。

于 2013-02-15T04:10:33.843 に答える
1

このように自分で数える必要があります

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)
于 2013-02-15T04:03:22.703 に答える
0

(改訂)

反復する方向に応じて、列または行のプロパティを使用して、その場で序数を計算できます。したがって

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
于 2013-08-07T04:23:05.243 に答える