从for循环中访问范围的下一项(Excel VBA)

时间:2014-09-25 13:08:41

标签: excel vba excel-vba

我有一个过滤的电子表格,没有明显的模式。我需要检查是否有两个连续的灰色填充单元格(RGB:191,191,191)。当我说连续时,我的意思是在可见单元格之外,因此连续不一定意味着行数将是连续的。但是,我不确定如何从for循环内部访问该范围的下一行。我已经复制并粘贴了我的脚本的简化版本以帮助解答。谢谢你

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

1 个答案:

答案 0 :(得分:1)

分两次通过:

  1. 获取信息
  2. 循环信息
  3. 例如:

    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