1

セルの色に基づいてセルに数値を挿入するスクリプトを実行しようとしています。色が赤の場合は、# 1 を挿入します。スプレッド シートには 380 行ありますが、スクリプトは行 346 で実行を停止します (1 を挿入します)。以下のスクリプト:

Sub InsertOne()

Dim endRow As Long
Dim colorD As Range
Dim Cell As Range


endRow = Sheets(1).Cells(Sheets(1).Rows.Count, "C").End(xlUp).Row

'Ensure ending row is at least Row 2 

If endRow < 2 Then
  endRow = 2
End If

Set colorD = Range("F2", Range("F" & Rows.Count).End(xlUp))

'Loop through each cell in Column D

For Each Cell In colorD

    If Cell.Interior.ColorIndex = 3 Then 

        Cell.Value = 1

    End If

Next Cell

End Sub 
4

1 に答える 1

3

以下のコードを試してください:

endRow を計算したら、それを使用してSet colorD範囲を設定できます。

Sub InsertOne()

    Dim endRow As Long
    Dim colorD As Range
    Dim Cell As Range


    endRow = Sheets(1).Cells(Sheets(1).Rows.Count, "C").End(xlUp).Row

    'Ensure ending row is at least Row 2

    If endRow < 2 Then
        endRow = 2
    End If

    Set colorD = Range("F2:F" & endRow)

    'Loop through each cell in Column D

    For Each Cell In colorD

        If Cell.Interior.ColorIndex = 3 Then

            Cell.Value = 1

        End If

    Next Cell

End Sub
于 2013-05-29T17:50:32.140 に答える