Search code examples
excelexcel-2003vba

Is there a constraint on the depth level of grouping in Excel?


I am trying to write a macro in Excel which will allow me to automatically do groupings based on the number located in the first column. Here is the code.

Sub Makro1()
Dim maxRow As Integer
Dim row As Integer
Dim groupRow As Integer
Dim depth As Integer
Dim currentDepth As Integer

maxRow = Range("A65536").End(xlUp).row

For row = 1 To maxRow
    depth = Cells(row, 1).Value
    groupRow = row + 1
    currentDepth = Cells(groupRow, 1).Value
    If depth >= currentDepth Then
       GoTo EndForLoop
    End If
    Do While currentDepth > depth And groupRow <= maxRow
        groupRow = groupRow + 1
        currentDepth = Cells(groupRow, 1).Value
    Loop
    Rows(row + 1 & ":" & groupRow - 1).Select
    Selection.Rows.Group
EndForLoop:
    Next row
End Sub

The first column in the Excel file looks like this:

1
2
2
3
3
4
4
4
4
5
5
5
6
6
6
6
5
6
6
6
7
8
8
9
10
9
10
10
8
7
7
8
6
5
4
3
2
1
2

When the macro reaches the depth 8 speaking of the groupings, I get error number 1004. It looks like the Excel does not allow me to create a depth greater than 8. Is there a workaround for this? I am using MS Excel 2003.


Solution

  • You are out of luck.

    There is an 8 level limit for grouping which

    • also exists in xl07
    • on my testing exists in xl2010 (gives "Group method of range class failed")