Sub Decoupage()
Dim colref%, chemin$, LO As ListObject, tablo, nlig&, ncol%, d As Object, i&, a, resu(), k%, cle$, n&, j&
colref = 2 'colonne du tableau source contenant les clés
chemin = ThisWorkbook.Path & "\Fichiers clés\" 'sous-dossier
If Dir(chemin, vbDirectory) = "" Then MkDir chemin 'crée le sous-dossier s'il n'existe pas
Application.ScreenUpdating = False
Application.DisplayAlerts = False 'si les fichiers clés ont déjà été créés
Sheets("SynthesisWorkforceList").Copy '1er document auxiliaire
Set LO = ActiveSheet.ListObjects(1) 'tableau structuré
tablo = LO.Range.Formula 'tableau des formules
nlig = UBound(tablo)
ncol = UBound(tablo, 2)
LO.AutoFilter.ShowAllData 'désactive le filtre s'il existe
If Not LO.DataBodyRange Is Nothing Then LO.DataBodyRange.Delete xlUp 'RAZ
With LO.Range
.Columns(.Columns.Count + 1).Resize(, Columns.Count - .Columns.Count - .Column + 1).EntireColumn.Delete 'facultatif, vide à droite
.Parent.DrawingObjects.Delete 'supprime le bouton
With .Parent.UsedRange: End With 'actualise
End With
Set d = CreateObject("Scripting.Dictionary")
For i = 2 To nlig
d(UCase(tablo(i, colref))) = "" 'valeurs uniques en colonne B
Next i
a = d.keys
ReDim resu(1 To nlig, 1 To ncol)
For i = 0 To UBound(a)
cle = a(i)
n = 0
For j = 2 To nlig
If tablo(j, colref) = cle Then
n = n + 1
For k = 1 To ncol
resu(n, k) = tablo(j, k)
Next k
End If
Next j
LO.Parent.Copy '2ème document auxiliaire
With ActiveSheet.ListObjects(1).Range
.ListObject.Resize .Resize(n + 1) 'redimensionne le tableau structuré
.Cells(2, 1).Resize(n, ncol) = resu 'restitue le tableau des résultats
End With
ThisWorkbook.Sheets("Appendice").Copy After:=ActiveSheet 'ajoute la feuille Appendice
Sheets(1).Activate
If Trim(a(i)) = "" Then a(i) = "(vide)"
ActiveWorkbook.SaveAs chemin & a(i) & ".xlsx", 51 'enregistre le fichier
ActiveWorkbook.Close 'ferme le fichier créé
Next i
LO.Parent.Parent.Close False 'ferme le 1er document auxiliaire
MsgBox UBound(a) + 1 & " fichier" & IIf(UBound(a), "s .xlsx ont été créés...", " .xlsx a été créé...")
End Sub