XL 2016 Vba trier plusieurs colonnes d'une plage

KTM

XLDnaute Impliqué
Bonsoir chers tous
Je voudrais trier quatre colonnes par ordre croissant ( B,E,G,I) .
Dans mon fichier j'ai conçu un code mais quelque chose cloche.
Aidez moi à corriger Svp.
 

KTM

XLDnaute Impliqué
Bonsoir @dysorthographie ,@TooFatBoy

@dysorthographie
Ton code et tri_bis donne le même résultat, non ?
(C'est juste pour savoir)

Normalement, @KTM, si il utilise Excel 2016, peut utiliser la syntaxe Sort apparu avec Excel 2007.

PS: J'ai testé ton code sur Office 365 64 bits.
MERCI à tous

j'ai adapté la proposition de Staple1600 et j'ai eu ce qui suit qui fonctionne aussi bien.
VB:
Sub tri()
Dim dl As Long
dl = Range("B" & Rows.Count).End(xlUp).Row
    Range("B2:M" & dl).Select
    With Worksheets("Feuil1")
    .Sort.SortFields.Clear
    .Sort.SortFields.Add2 Key:=Range("B3:B" & dl) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .Sort.SortFields.Add2 Key:=Range("E3:E" & dl) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .Sort.SortFields.Add2 Key:=Range("G3:G" & dl) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .Sort.SortFields.Add2 Key:=Range("I3:I" & dl) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With .Sort
        .SetRange Range("B2:M" & dl)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    End With
End Sub
 

Pièces jointes

  • Classeur1.xlsm
    43.6 KB · Affichages: 1

Staple1600

XLDnaute Barbatruc
Re

@KTM
Une version allégée de mon premier code
Est-ce que le résultat du tri est toujours bon chez toi ?
Code:
Sub tri_ter()
Dim dl&, I%, col, F As Worksheet: Set F = Worksheets("Feuil1")
dl = Cells(Rows.Count, 2).End(xlUp).Row - 2: col = Array(2, 5, 7, 9)
F.Sort.SortFields.Clear
For I = LBound(col) To UBound(col)
F.Sort.SortFields.Add2 Key:=Cells(3, col(I)).Resize(dl), SortOn:=0, Order:=1, DataOption:=0
With F.Sort
        .SetRange Range("B2:M78")
        .Header = xlYes: .MatchCase = False
        .Orientation = xlTopToBottom: .SortMethod = xlPinYin
        .Apply
End With
Next
End Sub
 

dysorthographie

XLDnaute Accro
Re

@dysorthographie
Moi, je demandais juste si le résultat est le même
en mode SQL
ou en mode Tri Excel (version 2007 et plus)

PS: Je garde précieusement ton module ModuleRequeteurUniversel dans ma besace VBA ;)
a priori c'est pareilo_O

Personnellement je nommerai les colonnes par leur lettre plutôt que par leur position c'est beaucoup plus parlant.
Code:
Array("B","E","G","I")
PS: Je garde précieusement ton module ModuleRequeteurUniversel dans ma besace VBA ;)

À ton service.
 
Dernière édition:

RyuAutodidacte

XLDnaute Impliqué
Supporter XLD
Bonjour @KTM et le fil,
@dysorthographie , @Staple1600 👋
à vérifier car je peux me tromper, mais il semblerait que l'on peut trier sur 3 colonnes
pour avoir le même résultat : B, G, I
VB:
Sub Tri()
Dim Plage As Range, Col, C
    With ActiveSheet
    Set Plage = .Cells(2, 2).CurrentRegion
    Col = Array(1, 6, 8)
        With .Sort
            .SortFields.Clear
            For Each C In Col
                .SortFields.Add Key:=Plage.Columns.Item(C), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
            Next
            .SetRange Plage: .Header = xlYes: .MatchCase = False: .Orientation = xlTopToBottom: .SortMethod = xlPinYin: .Apply
        End With
    End With
    Set Plage = Nothing
End Sub
 

Discussions similaires

Membres actuellement en ligne

Statistiques des forums

Discussions
312 215
Messages
2 086 329
Membres
103 184
dernier inscrit
Di Martino