简体   繁体   中英

Access next item of a range from within a for loop (Excel VBA)

I have a filtered spreadsheet that has no obvious pattern to it. I need to check whether there are two consecutive cells with grey fill (RGB : 191,191,191). When I say consecutive, I mean out of the visible cells, so consecutive doesn't necessarily mean the row numbers will be consecutive. However I'm not sure how to access the next row of the range from inside the for loop. I've copied and pasted a simplified version of my script to aid answers. Thanks

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

Do it in two passes:

  1. get the information
  2. loop over the information

For example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM