2014-09-25 32 views
0

我有一個過濾的電子表格,它沒有明顯的模式。我需要檢查是否有兩個連續的單元格用灰色填充(RGB:191,191,191)。當我連續說,我的意思是在可見細胞之外,如此連續不一定意味着行號將是連續的。不過,我不確定如何從for循環中訪問該範圍的下一行。我複製並粘貼了腳本的簡化版本以幫助解答。由於
從for循環(Excel VBA)中訪問範圍的下一個項目

Set Rng = Range("A2:A105").SpecialCells(xlCellTypeVisible) 
For Each rowcheck In Rng 
    If Cells(rowcheck.Row, "C").Interior.color = RGB(191, 191, 191) And _' 
    'The next visible cell also has an rgb value of 191 Then 
    blah blah 
    End If 
Next rowcheck 
+0

兩件事情:1 。是否可以使用「C」在'Cells()'中? (從來沒有見過)和2.在'和'後面的評論中,下一個可見單元格意味着什麼? – 2014-09-25 13:14:00

+0

@Goosebumbs yes可以在'Cells()中使用列名稱' – 2014-09-25 13:14:33

+1

我想使用另一個變量來存儲* previous *單元格,並在每次迭代中檢查它,而不是嘗試檢查下一個單元格。 – Rory 2014-09-25 13:19:30

回答

1

就做兩遍:

  1. 得到的信息
  2. 環比信息

例如:

Sub dural() 
    Dim boo() As Boolean, Rng As Range, i As Long, iMax As Long 
    Set Rng = Range("A2:A105").SpecialCells(xlCellTypeVisible) 
    ReDim boo(1 To Rng.Count) 

    i = 1 
    For Each rowcheck In Rng 
     If Cells(rowcheck.Row, "C").Interior.Color = RGB(191, 191, 191) Then 
      boo(i) = True 
     Else 
      boo(i) = False 
     End If 
     i = i + 1 
    Next rowcheck 
    iMax = i - 2 

    For i = 1 To iMax 
     If boo(i) And boo(i + 1) Then 
     'whatever 
     End If 
    Next i 

End Sub 
+0

謝謝你,不得不在第一個for循環中添加'rowNum(i)= rowCheck.Row',所以我有一個行號可以在「無論」階段引用,我猜這是最簡單的方法? 我真的會認爲會有一種簡單的方法可以說,範圍的第四個元素。你可以用For Each循環遍歷這些,所以它會讓我困惑,爲什麼你不能明確地引用它們。 – quantum285 2014-09-25 14:39:56

+0

@ quantum285很難通過* index循環一個不相交的範圍。*我通常需要**兩個**通行證,儘管它對我來說很不愉快。 – 2014-09-25 14:48:16