有没有办法自动分组?

时间:2016-12-02 11:29:33

标签: excel excel-vba vba

我有一个像这样的Excel:

Name       DOC        STATUS
John      ID         OK
John      ADRESS     Incomplete
John      SURNAME    Missing
Lara      ID         OK
Lara      ADRESS     Missing
Lara      SURNAME    Missing
Rony      ID         OK
Rony      ADRESS     OK
Rony      SURNAME    OK

这个想法是,由于每个DOC,名称重复3次。有没有办法自动分组行?比如,每3行分组一次?

1 个答案:

答案 0 :(得分:1)

如果您将Group 3行,并且由于您的数据是连续的行,您将获得组合在一起的1个大范围的行。

如果你想以某种方式分组,只有第一个值是可见的,而另外两个是折叠的,那么你实际上只需要分组第2和第3行。

<强>代码

Option Explicit

Sub GroupEveryNRows()

Dim i As Long, LastRow As Long

' modify "Sheet1" to your sheet's name
With Sheets("Sheet1")
    ' find last row with data in Column A
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

    ' loop from 2nd row till last row with data (assuming the first row has headers)
    For i = 2 To LastRow Step 3
        Range("A" & i + 1 & ":A" & i + 2).Rows.Group
    Next i
End With

End Sub