Se connecter avec
S'enregistrer | Connectez-vous

Macro Excel - Tri en ligne

Dernière réponse : dans Programmation

Bonjour à tous,

Je sollicite votre aide car je me prends la tête sur une macro Excel (le dev c'est pas trop mon point fort). ;(
J'ai un tableau rempli de chiffres que j'aimerai trier du plus petit au plus grand en ligne (sachant que je peux avoir des milliers de lignes et des dizaines de colonnes).

Je me suis inspiré d'une macro que j'ai trouvé sur le net pour faire une macro qui trie mais en colonne (qui fonctionne très bien d’ailleurs) :

Sub Tri()
'
' Tri Macro
'

'
Dim maplage As Range, i As Byte
For i = 1 To 50
If ActiveCell = "" Then
i = i + 1
Else
Set maplage = Range(Cells(2, i), Cells(Cells(65536, i).End(xlUp).Row, i))
maplage.Sort Key1:=Cells(2, i), Order1:=xlAscending, _
Orientation:=xlTopToBottom
End If
Next i
End Sub

Le soucis est que je n'arrive pas à l'adapter pour un tri en ligne.
Je pensais modifier la "xlTopToBottom" en "xlLeftToRight" puis jouer sur les "Range" mais je n'y arrive pas...

Pouvez-vous m'aiguiller ou m'aider à la modifier svp?

Merci !!

Autres pages sur : macro excel tri ligne

Lassé par la pub ? Créez un compte


j'ai sur une ligne avec tri du plus petit au plus grand via l'enregistreur de macro



  1. Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
  2. OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight, _
  3. DataOption1:=xlSortNormal

Merci de ta réponse.

Voici ce que j'ai sur une ligne :

  1. Sub Macro2()
  2. '
  3. ' Macro2 Macro
  4. '
  5.  
  6. '
  7. ActiveWorkbook.Worksheets("Feuil1").Sort.SortFields.Clear
  8. ActiveWorkbook.Worksheets("Feuil1").Sort.SortFields.Add Key:=Range("A2:G2"), _
  9. SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
  10. With ActiveWorkbook.Worksheets("Feuil1").Sort
  11. .SetRange Range("A2:G2")
  12. .Header = xlGuess
  13. .MatchCase = False
  14. .Orientation = xlLeftToRight
  15. .SortMethod = xlPinYin
  16. .Apply
  17. End With
  18. End Sub


Il faudrait que j'arrive à trier ligne par ligne. Par exemple une incrémentation (i = i + 1) avec une boucle.
Je pense que c'est au niveau du "range" que tout ce passe...

En "farfouillant" sur le net, j'ai trouvé une macro qui fonctionne très bien (je me suis finalement laissé tenter par la facilité :| )

Je vous laisse le code si ça peut aider quelqu'un d'autre :

  1. Sub test()
  2. Dim DerLig As Long, DerCol As Integer
  3. Dim Rg As Range, R As Range
  4. With Feuil1
  5. If Not IsEmpty(.UsedRange) Then
  6. DerLig = .Cells.Find(What:="*", _
  7. LookIn:=xlFormulas, _
  8. SearchOrder:=xlByRows, _
  9. SearchDirection:=xlPrevious).Row
  10.  
  11. DerCol = .Cells.Find(What:="*", _
  12. LookIn:=xlFormulas, _
  13. SearchOrder:=xlByColumns, _
  14. SearchDirection:=xlPrevious).Column
  15. End If
  16. Set Rg = .Range("A2", .Cells(DerLig, DerCol))
  17. End With
  18.  
  19. Application.ScreenUpdating = False
  20. Application.EnableEvents = False
  21. For Each R In Rg.Rows
  22. LeTriHorizontal R
  23. Next
  24. Application.EnableEvents = True
  25. Application.ScreenUpdating = True
  26.  
  27. End Sub
  28. '----------------------------------------
  29. Sub LeTriHorizontal(Plg As Range)
  30. With Plg
  31. .Sort Key1:=.Range("A1"), Order1:=xlAscending, Header:=xlNo, _
  32. OrderCustom:=1, Orientation:=xlLeftToRight, DataOption1:=xlSortNormal
  33. End With
  34. End Sub
  35. '----------------------------------------


Merci à tous pour votre aide !
Lassé par la pub ? Créez un compte