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



我有一个经过过滤的电子表格,它没有明显的模式。我需要检查是否有两个连续的单元格带有灰色填充(RGB:191191191191)。当我说连续时,我指的是在可见单元格中,所以连续并不一定意味着行号是连续的。但是,我不知道如何从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. 获取信息
  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

最新更新