快速的方式来反向循环过滤列表?

我有一个大量的自动过滤行(> 200,000)表。 我想通过一个列向上循环,直到find与当前单元格不同的第一个单元格为止。 我可以通过可见的单元格向下循环使用:

For Each cl In rng.SpecialCells(xlCellTypeVisible) 'check for different value Next cl 

我也可以循环'向上'跳过隐藏的行使用:

 For i = rng.Count To 1 Step -1 If rng.Cells(i).EntireRow.Hidden Then 'do nothing ElseIf 'check different value End If Next i 

但是,对于大量的隐藏行,即使只有几百个可见行,也可能需要一段时间才能跳过所有这些行。 我尝试使用rng.SpecialCells(xlCellTypeVisible) ,并通过他们后退,但它似乎也通过隐藏的单元格。

  1. 有没有办法扭转一个For Each循环的顺序?
  2. 有没有更快的方法来做到这一点?

谢谢

 Sub Tester() Dim x As Long, n As Long Dim a() As Long Dim rng As Range, c As Range, vis As Range Dim sht As Worksheet Set sht = ActiveSheet Set rng = sht.Range("A1:A1000") Set vis = rng.SpecialCells(xlCellTypeVisible) n = vis.Cells.Count ReDim a(1 To n) x = 1 For Each c In vis.Cells a(x) = c.Row x = x + 1 Next c For x = n To 1 Step -1 Debug.Print a(x), sht.Cells(a(x), 1) Next x End Sub 

你可以build立一个可见单元的集合,然后把它们反过来提取出来:

 Sub Backwards() Dim N As Long, col As Collection, RR As Range, r As Range Dim i As Long Set RR = Intersect(ActiveSheet.UsedRange, Range("A:A").Cells.SpecialCells(xlCellTypeVisible)) Set col = New Collection For Each r In RR col.Add (r.Address) Next r N = col.Count For i = N To 1 Step -1 Set r = Range(col(i)) MsgBox r.Address Next i End Sub