繁体   English   中英

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

[英]Access next item of a range from within a for loop (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. 获取信息
  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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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