2013-05-29 120 views
1

我試圖運行一個腳本,插入一個數字到基於單元格的顏色的單元格;如果顏色是紅色,然後將#1,試算表有380行,但該腳本將停止運行下面的行(插入1的)346腳本:VBA Excel; Rows.Count不包括所有行

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 
+0

設置colorD ...之後「colorD」的值是多少? –

+0

'設置顏色D =範圍(「F2」,範圍(「F」和行數)。結束(xlUp)。行)' – 2013-05-29 18:01:35

回答

2

試試下面的代碼:

一旦你計算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 
+0

Santosh =輝煌。謝謝! – matt