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 41 42 43 44 45 46 47 48 49 50 51 52
| Sub etablir()
Dim A_trier As Range, ListeNoms As Object, Nom As String, Ingd As String
Nom = ""
Set ListeNoms = CreateObject("System.Collections.ArrayList") ' un "Arraylist pour trier plus facilement en bas du module
ReDim Tb(1 To 1) 'j'initialise une variable tableau
x = 0
Dcol = Cells(5, Columns.Count).End(xlToLeft).Column ' on cherche la dernière colonne
Ingd = Cells(5, Dcol)
Set Dcel = Cells(Rows.Count, Dcol).End(xlUp) 'et la dernière cellule dans cette colonne
For i = 5 To Dcel.Row 'on boucle sur cette colonne
If Cells(i, Dcol).Value = Ingd Then 'on vérifie la cellule
x = x + 1 'si vérifié, on incrémente x
ReDim Preserve Tb(1 To x) 'on redimensionne le tableau
Tb(x) = Cells(i, Dcol).Address 'et on donne l'adresse à l'élément du tableau
End If
Next i
For i = 1 To UBound(Tb) - 1 'on boucle sur le tableau
Set A_trier = Range(Range(Tb(i))(2, -Dcol + 2), Range(Tb(i + 1))(0, 1)) 'plage qui sera triée
tri A_trier, Range(Tb(i))(2, 1) 'on va sur la procédure tri
Nom = Split(Range(Tb(i))(1, -Dcol + 3), " ")(1) 'nom représente le mot après "Marque"
ActiveWorkbook.Names.Add Name:=Nom, RefersTo:= _
Range(Range(Tb(i))(1, -Dcol + 2), Range(Tb(i + 1))(0, 1)) 'on définit un nom pour la plage triée
ListeNoms.Add Nom 'on ajoute ce nom à une "ArrayList"
Next i
'ci-dessous pour le dernier groupe
Set A_trier = Range(Range(Tb(UBound(Tb)))(2, -Dcol + 2), Dcel)
tri A_trier, Range(Tb(UBound(Tb)))(2, 1)
Nom = Split(Range(Tb(UBound(Tb)))(1, -Dcol + 3), " ")(1)
ListeNoms.Add Nom
ActiveWorkbook.Names.Add Name:=Nom, RefersTo:= _
Range(Range(Tb(UBound(Tb)))(1, -Dcol + 2), Dcel)
'---------------------------------------------------------------
Range(ListeNoms(0)).Cut Destination:=Range("J5") 'on déplace la 1ère plage (1er bloc) à droite de la dernière colonne
'et les autres au travers d'une boucle
For i = 1 To ListeNoms.Count - 1
Set Dcel = Cells(Rows.Count, Dcol * 2).End(xlUp)(2, -Dcol + 2)
Range(ListeNoms(i)).Cut Destination:=Dcel
Next i
'-------------------------------------------------------------
ListeNoms.Sort 'on trie les noms des blocs
Range(ListeNoms(0)).Cut Destination:=Range("A5") 'on remet le 1er bloc au début
'et les autres au travers d'une boucle
For i = 1 To ListeNoms.Count - 1
Set Dcel = Cells(Rows.Count, Dcol).End(xlUp)(2, -Dcol + 2)
Range(ListeNoms(i)).Cut Destination:=Dcel
Next i
End Sub
Private Sub tri(laplage As Range, lacel As Range)
laplage.Sort Key1:=lacel, Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End Sub |