我有一堆工作表,我想循环浏览一堆不同的区域。我将会进行一些计算,并认为它会比列中的逐个单元更快更有效。在工作表中循环显示区域/区域?
我试过几个不同的东西,但无法弄清楚如何跳到下一个区域。我做了一些意见,最有前途的是最后一个(For each cel in rng...
,但我做的第一cel.CurrentRegion.Select
后,然后做的事情,我怎么跳转到下一个区域?
Here's a zany .gif of what that does as-is...
Sub loop_through_zones()
Dim rng As Range, area As Range, singleArea As Range, cel As Range
Set rng = Range("A2:D15")
For Each area In rng.Areas ' This just selects all the data.
area.Select
Next area
For Each area In rng.CurrentRegion ' this just loops through cells in an area.
area.Select
Next area
For Each cel In rng
cel.CurrentRegion.Select ' gets current region!
'do something with region here
' ...
' now, go to the next REGION, not cel in current area...?
Next cel
End Sub
所以我想获得A2:D4
,做的事情,然后移动到下一个区域,A6:D9
,然后转移到A11:D15
等
编辑:它看起来像我能做到这一点与一些For i
循环,但我很好奇,如果你能做到这一点的内置CurrentRegion
/Areas
,或者如果我要做到这一点缺憾:
For i = 2 To lastRow
Set CurrentRegion = .Range(.Cells(i, 1), .Cells(.Cells(i, 9).End(xlDown).row, 4))
CurrentRegion.Select
' Do things with the current region here...
i = CurrentRegion.Rows(CurrentRegion.Rows.Count).row + 1
Next i
哦,这太棒了!它使用内置的'Areas',这正是我尝试使用/学习的内容,并且非常直观,无需添加许多附加步骤。谢谢! :D – BruceWayne
@BruceWayne不客气!很高兴你发现它有帮助。 :) – sktneer