XL 2019 Trier efficacement un tableau

JMP56

XLDnaute Nouveau
Bonjour,

Mon tableau de comptes contient 8 colonnes de A à H. Il commence à la cellule A5 (première opération rentrée), est évolutif (contient actuellement 3410 opérations). La ligne 4 contient les titres de colonnes.
A l'aide d'un bouton lié à une macro, je cherche à le trier par dates (colonne A) et, à la fin de l'opération, revenir à son début, soit afficher depuis A1 sans rien de sélectionné.
J'ai enregistré un code dont je ne suis pas satisfait, car il se finit par la colonne A sélectionnée et j'ai dû définir une butée d'opérations à 10000. Or, je voudrais qu'il ne trie que les opérations rentrées, soit jusqu'à la dernière ligne non vide:
Sub Tri_date()
'
' Tri_date Macro
'
' Touche de raccourci du clavier: Ctrl+t
'
Columns("A:A").Select
ActiveWorkbook.Worksheets("Compte courant").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Compte courant").Sort.SortFields.Add2 Key:=Range( _
"A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Compte courant").Sort
.SetRange Range("A4:H10000")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub

Comment pourrais-je, certainement plus simplement, obtenir un résultat plus propre?
Merci de votre aide.
 

gbinforme

XLDnaute Impliqué
Bonjour,
En fonction de ce que j'ai compris de tes souhaits je te propose ceci :
VB:
Sub Tri_date()
'
' Tri_date Macro
'
' Touche de raccourci du clavier: Ctrl+t
'
Dim der As Long
    With ActiveWorkbook.Worksheets("Compte courant")
        der = .Cells(Rows.Count, 1).End(xlUp).Row
        .Activate
        With .Sort
            With .SortFields
                .Clear
                .Add Key:=Range("A4"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            End With
            .SetRange Range("A4:H" & der)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End With
    ActiveWindow.ScrollRow = 1
    ActiveWindow.ScrollColumn = 1
End Sub
 

sylvanu

XLDnaute Barbatruc
Supporter XLD
Bonjour Jmp,
Donnez nous un fichier test. Ce sera plus simple et évitera de nous le retaper.
Et utilisez les balises </> ( à droite de l'icone GIF ) là aussi c'est plus lisible.
En PJ un essai Tri ascendant Descendant sur Dates en colonne A avec :
VB:
Sub TriAscendant()
    Application.ScreenUpdating = False
    DL = [A65500].End(xlUp).Row
    Range("A4:H" & DL).Resize(DL).Sort key1:=[A4], order1:=xlAscending, Header:=xlYes
    [A1].Select
End Sub
Code:
Sub TriDescendant()
    Application.ScreenUpdating = False
    DL = [A65500].End(xlUp).Row
    Range("A4:H" & DL).Resize(DL).Sort key1:=[A4], order1:=xlDescending, Header:=xlYes
    [A1].Select
End Sub
 

Pièces jointes

  • Tri.xlsm
    15.5 KB · Affichages: 4

Statistiques des forums

Discussions
312 209
Messages
2 086 274
Membres
103 168
dernier inscrit
isidore33