Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

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.
 
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
 
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

  • Question Question
XL 2021 listbox
Réponses
18
Affichages
284
Réponses
4
Affichages
177
Réponses
17
Affichages
1 K
Réponses
6
Affichages
1 K
Réponses
11
Affichages
780
Retour