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 回答