循环通过工作表中的区域/区域?

时间:2017-10-16 18:25:39

标签: excel vba excel-vba

我有一个工作表,其中包含许多我想要遍历的不同领域。我将进行一些计算,并认为它比在一列中逐个细胞更快/更有效。

我尝试了一些不同的东西,但无法弄清楚如何跳到下一个区域。我做了一些评论,最有希望的是最后一个(For each cel in rng...,但在我做了第一个cel.CurrentRegion.Select,然后做了一些事情,我怎么跳到下一个区域呢? / p>

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进行此操作,或者如果我必须这样做kludgy:

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

3 个答案:

答案 0 :(得分:3)

您可以尝试这样的事情......

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

答案 1 :(得分:2)

如果您要在电子表格中添加区域,可能需要使用region-Nregion-1region-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命名一个单元格,它就会被您的代码占用。

当然,您可以想象很多方法可以使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

答案 2 :(得分: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列中的随机空白单元可能会造成一些破坏。