1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| Public Sub GroupData()
Const levelColumn As Long = 1
Const firstRow As Long = 2
Dim lastRow As Long
Dim i As Long
Dim j As Long
Dim tabItems() As Variant
Dim objItem As Cls_Item
With ThisWorkbook.Worksheets("sans plan")
'charger les items
lastRow = .Cells(.Rows.Count, levelColumn).End(xlUp).Row
ReDim tabItems(1 To lastRow - firstRow + 1)
For i = firstRow To lastRow
Set objItem = New Cls_Item
objItem.RowNum = i
objItem.Level = .Cells(i, levelColumn)
j = j + 1
Set tabItems(j) = objItem
Next i
'chercher les parents/enfants
For i = UBound(tabItems) To 2 Step -1
If tabItems(i).Level > 1 Then
For j = i - 1 To 1 Step -1
If tabItems(j).Level = tabItems(i).Level - 1 Then
Set tabItems(i).Parent = tabItems(j)
tabItems(j).Childs.Add tabItems(i)
Exit For
End If
Next j
End If
Next i
'grouper les lignes
For i = UBound(tabItems) To 1 Step -1
If tabItems(i).FullChildsCount <> 0 Then
.Range(.Cells(tabItems(i).RowNum + 1, 1), .Cells(tabItems(i).RowNum + tabItems(i).FullChildsCount, 1)).EntireRow.Group
End If
Next i
End With
End Sub |
Partager