XL 2019 Classer des chiffres dans des cellules horizontales.

album40

XLDnaute Nouveau
Bonjour,
Je souhaite classer par ordre croissant des chiffres dans des cellules horizontales.
J'ai tenté de le faire par la fonction trier par ordre croissant je n'obtient pas le résultat voulu
c'est la première ligne seule qui me donne le bon résultat. je n'arrive pas a faire tout le tableau simultanément.
Merci de me donner l'astuce
 

Pièces jointes

  • ORDRE CROISSANT.xlsx
    9.5 KB · Affichages: 17
Solution
Bonsoir, Patricktoulon, Staple1600
il faut juste faire un Tri d’un tableau (Array) à 1 dimension
Le coeur du programme est simple pour le reste c'est le principe du tri quickSort

VB:
Sub MiseEnForme()
Dim t() As Variant, i As Integer, j As Integer
ReDim t(1)
For i = 7 To ActiveSheet.Cells(ActiveSheet.Cells(65535, 3).End(xlUp).Row, 3).Row
    t(i - 6) = Range(Cells(i, 3), Cells(i, 7))
    tri t, i - 6, LBound(t(1), 2), UBound(t(1), 2)
    ReDim Preserve t(UBound(t) + 1)
Next i
' Restitution de se tableau multidimension
ReDim Preserve t(UBound(t) - 1)
For i = LBound(t, 1) To UBound(t, 1)
Cells(i + 6, 9).Resize(UBound(t(i), 1), UBound(t(i), 2)) = t(i)
Next i
End Sub
La fonction de tri
VB:
Sub tri(a() As Variant, i, gauc...

Staple1600

XLDnaute Barbatruc
Bonsoir le fil

NB: Le confinement, c'est bien pour faire du tri dans ses archives ;)
Ici un macro paramétrable
(j'ai mis deux exemples: croissant et décroissant)
Dans le test, les données commencent en A1
(sans ligne vide et sans trou)
VB:
Sub tTest_D()
Application.ScreenUpdating = False
Tri_Horizontal Range("A1"), xlDescending
End Sub
Sub tTest_C()
Application.ScreenUpdating = False
Tri_Horizontal Range("A1"), xlAscending
End Sub
Private Sub Tri_Horizontal(Plage As Range, Sens As XlSortOrder)
Dim Rng As Range
Set Rng = Plage.CurrentRegion
For Each c In Rng.Rows
c.Sort Key1:=c.Cells(1, 1), Order1:=Sens, Header:=xlNo, Orientation:=xlSortRows
Next c
End Sub
EDITION:
Bonsoir patricktoulon
Bonsoir sousou

NB:Version corrigée après observation de patricktoulon
 
Dernière édition:

patricktoulon

XLDnaute Barbatruc
re
Staple1600 ben oui il veut un tri horizontal ligne par ligne
Capture.JPG
 

Staple1600

XLDnaute Barbatruc
Re

C'est ce que fait mon code.
Et j'ai précisé comment je l'ai testé.
Etonnant que tu n'es pas adapté le code avant de tester ;)
Ci-dessous la version corrigée.
VB:
Sub Tri_Horizontal(Plage As Range, Sens As XlSortOrder)
Dim Rng As Range
Set Rng = Plage.CurrentRegion
For Each c In Rng.Rows
c.Sort Key1:=c.Cells(1, 1), Order1:=Sens, Header:=xlNo, Orientation:=xlSortRows
Next c
End Sub
 
Dernière édition:

Staple1600

XLDnaute Barbatruc
Re

Je dois manquer de sucre... :rolleyes:
J'aurai du écrire ceci dés le départ ;)
VB:
Sub Tri_Horizontal(Plage As Range, Sens As XlSortOrder)
Dim c As Range
For Each c In Plage.CurrentRegion.Rows
c.Sort Key1:=c.Cells(1, 1), Order1:=Sens, Header:=xlNo, Orientation:=xlSortRows
Next c
End Sub
 

patricktoulon

XLDnaute Barbatruc
re
mon adaptation
a mon avis c'est plutôt ca
VB:
Sub tTest_C()
Application.ScreenUpdating = False
Tri_Horizontal Range("c7:g32"), xlAscending
End Sub
Private Sub Tri_Horizontal(Plage As Range, Sens As XlSortOrder)
For Each c In Plage.Rows
'MsgBox c.Address
c.Sort Key1:=c.Cells(1), Order1:=Sens, Header:=xlNo, Orientation:=xlLeftToRight
Next c
End Sub

ca peut pas être xlSortRows mais xlsortcolumns peut être non ?
;)
 

patricktoulon

XLDnaute Barbatruc
ok testé
je l'ai un peu simplifier et j'ai accessoiriser le currentregion
version Staple1600
VB:
Sub tTest_Staple1600()
Application.ScreenUpdating = False
Tri_Horizontal_Staple1600 Range("c7:g32"), xlAscending
End Sub

Sub Tri_Horizontal_Staple1600(Plage As Range, Sens As XlSortOrder)
If Plage.Cells.Count = 1 Then Set Plage = Plage.CurrentRegion
For Each c In Plage.Rows
c.Sort Key1:=c.Cells(1, 1), Order1:=Sens, Header:=xlNo, Orientation:=xlSortRows
Next c
End Sub

mais c'est pas très logique le " Orientation:=xlSortRows" pour trier une seule ligne

version patricktoulon
VB:
Sub tTest_patricktoulon()
Application.ScreenUpdating = False
Tri_Horizontal_patricktoulon Range("c7:g32"), xlAscending
End Sub

Private Sub Tri_Horizontal_patricktoulon(Plage As Range, Sens As XlSortOrder)
If Plage.Cells.Count = 1 Then Set Plage = Plage.CurrentRegion
For Each c In Plage.Rows
c.Sort Key1:=c.Cells(1), Order1:=Sens, Header:=xlNo, Orientation:=xlLeftToRight
Next c
End Sub
 
Dernière édition:

Staple1600

XLDnaute Barbatruc
Re

Je t'avais préparé ce petit test
(alors je livre)
VB:
Sub Pour_patricktoulon()
Randomize
[G7:P32] = "=1600-(COLUMN()+ROW())": [G7:P32] = [G7:P32].Value
End Sub
Sub tTest_D()
Application.ScreenUpdating = False
Tri_Horizontal Range("G7"), xlDescending
End Sub
Sub tTest_C()
Application.ScreenUpdating = False
Tri_Horizontal Range("G7"), xlAscending
End Sub
Private Sub Tri_Horizontal(Plage As Range, Sens As XlSortOrder)
Dim c As Range
For Each c In Plage.CurrentRegion.Rows
c.Sort Key1:=c.Cells(1, 1), Order1:=Sens, Header:=xlNo, Orientation:=xlSortRows
Next c
End Sub

PS: Tu peux donc supprimer le message#12 qui n'a pas lieu d'être ;)
 

Staple1600

XLDnaute Barbatruc
Re

Et dernier test, qui explique pourquoi j'ai pris xlSortRows
VB:
Sub tTest_AA()
Application.ScreenUpdating = False
Tri_Horizontal Range("G7:P32"), xlAscending
End Sub
Sub tTest_BB()
Application.ScreenUpdating = False
Tri_Horizontal Range("G7:P32"), xlDescending
End Sub
Sub tTest_CC_KO()
Application.ScreenUpdating = False
Tri_Horizontal Range("G7:P32"), xlDescending, 1
End Sub
Sub tTest_DD_KO()
Application.ScreenUpdating = False
Tri_Horizontal Range("G7:P32"), xlAscending, 1
End Sub

Private Sub Tri_Horizontal(Plage As Range, Sens As XlSortOrder, Optional Ori As XlSortOrientation = 2)
Dim c As Range
For Each c In Plage.Rows
c.Sort c.Cells(1, 1), Order1:=Sens, Header:=xlNo, Orientation:=Ori
Next c
End Sub
 

Statistiques des forums

Discussions
312 163
Messages
2 085 860
Membres
103 006
dernier inscrit
blkevin