Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Macro de tri (diff. entre XL 2003 et 2007)

5 réponses
Avatar
Emile63
Bonjour =E0 tous,

J'ai une macro (issue d'une feuille faite avec XL 2003) qui trie automatiqu=
ement des donn=E9s sur 3 crit=E8res et qui fonctionne bien:
----------------------------------------
Feuil2.[D15].Select
Selection.CurrentRegion.Select
Selection.Sort _
Key1:=3DRange("L16"), Order1:=3DxlAscending, _
Key2:=3DRange("K16"), Order2:=3DxlAscending, _
Key3:=3DRange("I16"), Order3:=3DxlAscending, _
Header:=3DxlGuess, OrderCustom:=3D1, MatchCase:=3DFalse, Orientation:=
=3DxlTopToBottom, _
DataOption1:=3DxlSortTextAsNumbers, _
DataOption2:=3DxlSortNormal, _
DataOption3:=3DxlSortNormal

----------------------------------------
Aujourd'hui, avec XL 2007, je souhaiterais trier sur 5 crit=E8res, mais je =
trouve que l'enregistreur de macros me "pond" une proc=E9dure plut=F4t comp=
liqu=E9e et qui ne fonctionne pas =E0 tous les coups.
Par quel moyen est-ce que je peux simplifier au mieux et gagner en efficaci=
t=E9?
Car dans le second exemple les zones sont fig=E9s (ex:Range("L16:L25")) alo=
rs que pr=E9c=E9demment, il triait a partir de l'ent=EAte de colonne quelle=
que soit le NB de lignes (puisque mon bulletin de livraison change constam=
ment)
----------------------------------------

ActiveWorkbook.Worksheets("Bulletin de livraison").Sort.SortFields.Clea=
r
ActiveWorkbook.Worksheets("Bulletin de livraison").Sort.SortFields.Add =
Key:=3D _
Range("L16:L25"), SortOn:=3DxlSortOnValues, Order:=3DxlAscending, D=
ataOption:=3DxlSortNormal
ActiveWorkbook.Worksheets("Bulletin de livraison").Sort.SortFields.Add =
Key:=3D _
Range("K16:K25"), SortOn:=3DxlSortOnValues, Order:=3DxlAscending, D=
ataOption:=3DxlSortNormal
ActiveWorkbook.Worksheets("Bulletin de livraison").Sort.SortFields.Add =
Key:=3D _
Range("I16:I25"), SortOn:=3DxlSortOnValues, Order:=3DxlAscending, D=
ataOption:=3DxlSortNormal
ActiveWorkbook.Worksheets("Bulletin de livraison").Sort.SortFields.Add =
Key:=3D _
Range("E16:E25"), SortOn:=3DxlSortOnValues, Order:=3DxlAscending, D=
ataOption:=3DxlSortNormal
ActiveWorkbook.Worksheets("Bulletin de livraison").Sort.SortFields.Add =
Key:=3D _
Range("D16:D25"), SortOn:=3DxlSortOnValues, Order:=3DxlAscending, D=
ataOption:=3DxlSortNormal
With ActiveWorkbook.Worksheets("Bulletin de livraison").Sort
.SetRange Range("C15:L25")
.Header =3D xlYes
.MatchCase =3D False
.Orientation =3D xlTopToBottom
.SortMethod =3D xlPinYin
.Apply
End With
----------------------------------------
Je vous remercie d'avance pour vos conseils et suggestions.
cordialement,

Emile

5 réponses

Avatar
MichD
Bonjour,

Tu dis que ce type de code ne fonctionne pas. Est-cela?

Dans la procédure, remplace "ThisWorkbook.Name" dans With
Workbooks(ThisWorkbook.Name)
par le nom du classeur + sont extension si la macro n'est pas mise dans le
classeur dont la feuille doit être triée.

'---------------------------------------------
Sub test()
Dim Sh As Worksheet

With Workbooks(ThisWorkbook.Name)
Set Sh = .Worksheets("Bulletin de livraison")
With Feuil1.Sort
.SortFields.Clear
.SetRange Sh.Range("C15:L25")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom

.SortFields.Add Key:=Sh.Range("L16"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Sh.Range("K16"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Sh.Range("i16"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Sh.Range("E16"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Sh.Range("D16"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.Apply
End With
End With
End Sub
'---------------------------------------------
Avatar
MichD
Oups, un oubli!!!

Évidemment, il faudrait remplacer cette ligne de code :

With Feuil1.Sort

Par

With Sh.Sort

pour être conséquent!
Avatar
Emile63
Une fois encore MichD, c'est la classe :-)
Merci.

Mais comme les données a trier sur mon bulletin peuvent être +/- nombre uses,
Est-ce que la ligne: .SetRange Sh.Range("C15:L25")
ne va pas me créer de problèmes si je dépasse le L25?
Ou est-ce qu'il faudrait préalablement compter les enregistrements pour l ui indiquer la "fin" de la sélection "Range"?
Merci pour ton éclairage sur ce point.
cordialement,
Emile
Avatar
MichD
Comme ceci :

'---------------------------------------------------
Sub test()
Dim Sh As Worksheet, DerLig As Long

With Workbooks(ThisWorkbook.Name)
Set Sh = .Worksheets("Bulletin de livraison")
End With

With Sh
With .Range("C:L")
DerLig = .Find("*", LookIn:=xlValues, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
If DerLig < 16 Then DerLig = 16
End With

With .Sort
.SortFields.Clear
.SetRange Sh.Range("C15:L" & DerLig)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom

.SortFields.Add Key:=Sh.Range("L16"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Sh.Range("K16"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Sh.Range("i16"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Sh.Range("E16"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Sh.Range("D16"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.Apply
End With
End With

End Sub
'---------------------------------------------------

"Emile63" a écrit dans le message de groupe de discussion :


Une fois encore MichD, c'est la classe :-)
Merci.

Mais comme les données a trier sur mon bulletin peuvent être +/- nombreuses,
Est-ce que la ligne: .SetRange Sh.Range("C15:L25")
ne va pas me créer de problèmes si je dépasse le L25?
Ou est-ce qu'il faudrait préalablement compter les enregistrements pour lui
indiquer la "fin" de la sélection "Range"?
Merci pour ton éclairage sur ce point.
cordialement,
Emile
Avatar
Emile63
Merci MichD,

Ca fonctionne Super. ;-)
Bonne soirée
Emile