我有一张包含大量自动过滤行(> 200,000)的工作表。我正在尝试通过一个列“向上”循环,直到找到与当前单元格不同的第一个单元格。我可以使用以下方法“向下”通过可见单元循环:
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)
并向后踩过它们,但它似乎也经历了隐藏的细胞。
For Each
循环的顺序? 由于
答案 0 :(得分:3)
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
答案 1 :(得分:2)
您可以构建可见单元格的集合,然后反向提取它们:
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