Private Sub Worksheet_Activate()
Dim saut As Byte, t, ncol%, d As Object, i&, x$, ntab&, rest(), n&, j%, a As Range
'---préparation et numérotation des lignes---
saut = 1 'nombre de lignes à sauter, à adapter
With Sheets("Base").[A3].CurrentRegion
t = .Resize(, .Columns.Count + 1) '1 colonne de plus
End With
ncol = UBound(t, 2)
Set d = CreateObject("Scripting.Dictionary")
For i = 2 To UBound(t)
x = CStr(t(i, 2)) 'matricule
If x <> "" Then
If IsNumeric(x) Then t(i, 2) = CDbl(x) 'conversion
d(x) = d(x) + 1 'rang des doublons
t(i, ncol) = d(x) 'numérotation en dernière colonne
End If
Next i
If d.Count = 0 Then GoTo 1 'si le tableau est vide
ntab = Application.Max(d.items) 'nombre de tableaux à créer
ReDim rest(1 To UBound(t) + saut * ntab, 1 To ncol - 1)
'---création des tableaux---
For ntab = 1 To ntab
For i = 2 To UBound(t)
If t(i, ncol) = ntab Then
n = n + 1
For j = 1 To ncol - 1
rest(n, j) = t(i, j)
Next j
End If
Next i
n = n + saut 'sauts de lignes
Next ntab
'---restitution et tri---
Application.ScreenUpdating = False
With [A4].Resize(n, ncol - 1)
.Value = rest
Set a = .Columns(2).SpecialCells(xlCellTypeConstants)
For Each a In a.Areas
a.EntireRow.Sort a, xlAscending, Header:=xlNo 'tri
Next
End With
1 Rows(n + 4 & ":" & Rows.Count).Delete
End Sub