Macro Excel - Tri en ligne

  • Auteur de la discussion lavjau
  • Date de début

lavjau

Nouveau membre
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 !!
 

Thore

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



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

lavjau

Nouveau membre
Merci de ta réponse.

Voici ce que j'ai sur une ligne :

[cpp]Sub Macro2()
'
' Macro2 Macro
'

'
ActiveWorkbook.Worksheets("Feuil1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Feuil1").Sort.SortFields.Add Key:=Range("A2:G2"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Feuil1").Sort
.SetRange Range("A2:G2")
.Header = xlGuess
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
End With
End Sub
[/cpp]

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...
 

lavjau

Nouveau membre
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 :

[cpp]Sub test()
Dim DerLig As Long, DerCol As Integer
Dim Rg As Range, R As Range
With Feuil1
If Not IsEmpty(.UsedRange) Then
DerLig = .Cells.Find(What:="*", _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row

DerCol = .Cells.Find(What:="*", _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
End If
Set Rg = .Range("A2", .Cells(DerLig, DerCol))
End With

Application.ScreenUpdating = False
Application.EnableEvents = False
For Each R In Rg.Rows
LeTriHorizontal R
Next
Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub
'----------------------------------------
Sub LeTriHorizontal(Plg As Range)
With Plg
.Sort Key1:=.Range("A1"), Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=1, Orientation:=xlLeftToRight, DataOption1:=xlSortNormal
End With
End Sub
'----------------------------------------[/cpp]

Merci à tous pour votre aide !
 
Vous devez vous inscrire ou vous connecter pour répondre ici.
Derniers messages publiés
Statistiques globales
Discussions
730 128
Messages
6 717 848
Membres
1 586 373
Dernier membre
https://forum.tomshardwar
Partager cette page
Haut