2017-10-16 73 views
0

我有一堆工作表,我想循环浏览一堆不同的区域。我将会进行一些计算,并认为它会比列中的逐个单元更快更有效。在工作表中循环显示区域/区域?

我试过几个不同的东西,但无法弄清楚如何跳到下一个区域。我做了一些意见,最有前途的是最后一个(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 

enter image description here

所以我想获得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 

回答

2

你可以试试这样的事情...

Sub LoopThroughZones() 
Dim lr As Long, iRow As Object 
Dim Area As Range, Rng As Range, Cell As Range 

lr = Cells(Rows.Count, 1).End(xlUp).Row 

'Loopting through each block 
For Each Area In Range("A2:A" & lr).SpecialCells(xlCellTypeConstants, 2).Areas 
    Area.Resize(, 4).Select 
Next Area 

'Looping through each cell in each block 
For Each Area In Range("A2:A" & lr).SpecialCells(xlCellTypeConstants, 2).Areas 
    Set Rng = Area.Resize(, 4) 
    For Each Cell In Rng 
     Cell.Select 
    Next Cell 
Next Area 
End Sub 
+0

哦,这太棒了!它使用内置的'Areas',这正是我尝试使用/学习的内容,并且非常直观,无需添加许多附加步骤。谢谢! :D – BruceWayne

+0

@BruceWayne不客气!很高兴你发现它有帮助。 :) – sktneer

2

如果要将区域添加到电子表格中,一个想法可能是使用名称region-Nregion-1,region-2等)来命名每个区域的第一个单元格。

然后,写你的代码是这样的:

For j = 1 To n 'where n is the number of regions 
    Set currentRegion = Range("region-" & j).CurrentRegion 
    For Each cell In currentRegion 
     'do your things 
    Next cell 
Next j 

这样,你可以在每次添加一个区域,一旦你的名字将细胞与region-N,它会被你的代码作出。当然,你可以想象很多方法使For Loop动态变化(如果你对它进行硬编码,每次添加一个新区域都必须改变它)。例如:

For Each namedRange In ActiveWorkbook.Names 
    If Left(namedRange,7) = "region-" Then 
     Set currentRegion = Range(namedRange).CurrentRegion 
     For Each cell In currentRegion 
      'do your things 
     Next cell 
    End If 
Next namedRange 
+0

感谢您 - 我喜欢它,并可能将其用于其他一些文件。现在,它会为我目前的宏添加更多的代码,但非常感谢您的聪明建议! – BruceWayne

+0

不客气@brucewayne,你选择的答案确实是最符合你的情况的答案 –

0

我会在数组定义每个不同的区域,然后遍历数组,使您的修改/计算。事情是这样的:

Option Explicit 

    Sub Regions() 

    Dim rng As Range 
    Dim wks As Worksheet 
    Dim Region() As Range 
    Dim i As Integer, j As Integer 
    Dim LastRow As Integer, LastColumn As Integer 

    'Your worksheet name 

    Set wks = Worksheets("Sheet1") 

    'Find last row and column of data 

    LastRow = wks.Cells.SpecialCells(xlCellTypeLastCell).Row 
    LastColumn = wks.Cells.SpecialCells(xlCellTypeLastCell).Column 

    'Put distinct range areas into an array 

    j = 1 

    For i = 2 To LastRow 

     If Not Cells(i, 1) = "" And (Cells(i, 1).Offset(-1, 0).Value = "" _ 
     Or wks.Cells(i, 1).Offset(-1, 0).Row = 1) Then 

      ReDim Preserve Region(1 To j) 
      Set rng = wks.Range(Cells(i, 1), Cells(LastRow, LastColumn)) 
      Set Region(j) = _ 
      wks.Range(Cells(i, 1), Cells(rng.End(xlDown).Row, rng.End(xlToRight).Column)) 
      j = j + 1 

     End If 

    Next i 

    Dim rngCell As Range 

    For i = 1 To UBound(Region) 
     Region(i).Select 

      'My test 
      For Each rngCell In Region(i) 
        rngCell.Interior.ColorIndex = 1 + i 
      Next 

    Next i 

    End Sub 

这是确定下一个区域开始的地方的相当粗糙的方法,因此,如果您的电子表格没有其间的每个区域空白单元格,你可能需要调整它。或者,A列中的随机空白单元格可能会造成一些破坏。