trier 4 colonnes -help

rafmix

XLDnaute Nouveau
bonjour, je souhaite trier 4 colonnes
Pour 3 colonnes, pas de soucis :

Range("G1:J1000").Select
Selection.Sort _
Key1:=Range("G:G"), Order1:=xlAscending, _
Key2:=Range("H:H"), Order2:=xlAscending, _
Key3:=Range("I:I"), Order3:=xlAscending, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom



Mais impossible de rajouter un Key4, car j'obtiens une erreur

Si qq avait une solution, merci d'avance
 

roro69

XLDnaute Impliqué
Re : trier 4 colonnes -help

Bonsoir;
A ESSAYER
Sub trier()
Dim i As Long
For i = 7 To 10
Cells(1, i).Select
Dim deb As String
deb = Cells(1, i).Address
ActiveCell.End(xlDown).Select
While ActiveCell.Row < 65536
Dim der As String
der = ActiveCell.Address
Range("" & deb & ":" & der & "").Select
Selection.Sort key1:=Cells(1, i), order1:=xlAscending, header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
ActiveCell.End(xlDown).Select
ActiveCell.End(xlDown).Select
Wend
Next i
Range("G1").Select
End Sub

A+++
 
Dernière édition:

soenda

XLDnaute Accro
Re : trier 4 colonnes -help

Bonjour à tous

En suivant l'idée de rafmix,
On peut aussi ajouter les lignes suivantes,
à la fin de son code

Code:
...
Selection.Sort _
Key1:=Range("j:j"), Order1:=xlAscending, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
 

Gael

XLDnaute Barbatruc
Re : trier 4 colonnes -help

Bonsoir à tous,

Sauf erreur de ma part, il y a un problème car cette macro va trier colonne par colonne mais sans sélectionner l'ensemble de la table.

Les données ne seront donc plus du tout en phase.

@+

Gael
 

Gael

XLDnaute Barbatruc
Re : trier 4 colonnes -help

Re,

En macro, j'aurais fait le tri en partant du 4ème critère de la façon suivante:

Code:
Sub trier()
Dim i As Long
For i = 5 To 2 Step -1
Range("G1").Select
Selection.Sort Key1:=Cells(1, i), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Next i
Range("G1").Select
End Sub

@+

Gael
 

soenda

XLDnaute Accro
Re : trier 4 colonnes -help

Bonsoir à tous,

Sauf erreur de ma part, il y a un problème car cette macro va trier colonne par colonne mais sans sélectionner l'ensemble de la table.

Les données ne seront donc plus du tout en phase.

@+

Gael

Bonjour Gael

Le bout de code que j'ai proposé
s'ajoute à celui de rafmix

Ce qui donne:

Code:
Range("G1:J1000").Select
Selection.Sort _
Key1:=Range("G:G"), Order1:=xlAscending, _
Key2:=Range("H:H"), Order2:=xlAscending, _
Key3:=Range("I:I"), Order3:=xlAscending, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

Selection.Sort _
Key1:=Range("j:j"), Order1:=xlAscending, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
 

rafmix

XLDnaute Nouveau
Re : trier 4 colonnes -help

merci pour vos réponses, en effet, le soucis était de trier les colonnes en même temps, en enregistrant une macro, j'ai obtenu un code qui fonctionne :

Worksheets("CatData").Sort.SortFields.Clear
Worksheets("CatData").Sort.SortFields.Add Key:=Range("G:G") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
Worksheets("CatData").Sort.SortFields.Add Key:=Range("H:H") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
Worksheets("CatData").Sort.SortFields.Add Key:=Range("I:I") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
Worksheets("CatData").Sort.SortFields.Add Key:=Range("J:J") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With Worksheets("CatData").Sort
.SetRange Range("G:J")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
 

Discussions similaires