Automatiser un tri

surgeon84fr

XLDnaute Junior
Bonjour le forum

J'ai un fichier excel avec beaucoup de colonnes.

J'ai tenté l'enregistrement d'une macro pour m'aider à trouver le moyen d'automatiser un tri. Mais je n'y arrive pas...

Voici mon code:
Option Explicit
Sub TRIPROG()

Dim derlig&, dercol%, i&

With Sheets("Feuil1")
derlig = .Cells(.Rows.Count, 1).End(xlUp).Row 'dernière ligne
dercol = .Cells(1, .Columns.Count).End(xlToLeft).Column 'dernière colonne
End With

With Worksheets("Feuil1").Sort
.SortFields.Clear
.SortFields.Add Key:=[AV:AV], SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=[AW:AW], SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.
.
.

.SortFields.Add Key:=[DG : DG], SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange Range(Cells(1, 1), Cells(derlig, dercol))
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

End Sub

En fait, je ne trouve pas le moyen de mettre en boucle le KEY:= qui va de la colonne AV à la colonne DG.

Il y en a 64...

Merci d'avance pour votre aide.
 

pierrejean

XLDnaute Barbatruc
Re : Automatiser un tri

Bonjour surgeon

a tester:

Code:
Option Explicit
Sub TRIPROG()
Dim derlig&, dercol%, i&
Dim n As Integer
Dim col As Object
With Sheets("Feuil1")
derlig = .Cells(.Rows.Count, 1).End(xlUp).Row 'dernière ligne
dercol = .Cells(1, .Columns.Count).End(xlToLeft).Column 'dernière colonne
End With
With Worksheets("Feuil1").Sort
.SortFields.Clear
For n = 48 To 69
Set col = Columns(n)
 .SortFields.Add Key:=col, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
Next
.SetRange Range(Cells(1, 1), Cells(derlig, dercol))
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
 

Discussions similaires

Réponses
3
Affichages
591
Réponses
11
Affichages
453

Statistiques des forums

Discussions
312 326
Messages
2 087 311
Membres
103 513
dernier inscrit
adel.01.01.80.19