Microsoft 365 tri automatique dates

GUERET

XLDnaute Occasionnel
Bonsoir,
Je suis en trains de créer cette feuille compta et je souhaiterai automatiser le tri en fonction de la date sur toutes les feuilles (de Janvier à Décembre).
J'ai essayé cette VBA mais elle ne fonctionne pas.
Cordialement

Sub macro_tri()

SortFields.Clear

SortFields.Add Key:=Range("C5"), SortOn:=xlSortOnValues, Order:= _

xlAscending, DataOption:=xlSortNormal

.Header = xlYes

.MatchCase = False

.Orientation = xlTopToBottom

.SortMethod = xlPinYin

.Apply

End With

End Sub
 

Pièces jointes

  • Test compta Chapitre.xlsm
    18.5 KB · Affichages: 12

TooFatBoy

XLDnaute Barbatruc
Bonjour,

Une proposition ci-dessous :
VB:
Sub macro_tri()
'
    Set Mafeuille = ActiveSheet     ' à adapter
    With Mafeuille.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("C5:C39"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SetRange Range("C4:C39")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

ps : des tableaux structurés seraient peut-être mieux.
 

ChTi160

XLDnaute Barbatruc
Bonsoir le Fil
Une adaptation de la proposition de @TooFatBoy
VB:
Sub macro_tri()
Dim DerLgn As Integer
    Set Mafeuille = ActiveSheet     ' à adapter
    DerLgn = Mafeuille.Cells(Mafeuille.Rows.Count, 3).End(xlUp).Row
    With Mafeuille.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("C5:C" & DerLgn), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        .SetRange Range("C4:F" & DerLgn)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
Jean marie
 

GUERET

XLDnaute Occasionnel
Bonsoir le Fil
Une adaptation de la proposition de @TooFatBoy
VB:
Sub macro_tri()
Dim DerLgn As Integer
    Set Mafeuille = ActiveSheet     ' à adapter
    DerLgn = Mafeuille.Cells(Mafeuille.Rows.Count, 3).End(xlUp).Row
    With Mafeuille.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("C5:C" & DerLgn), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        .SetRange Range("C4:F" & DerLgn)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
Jean marie
 

Discussions similaires

Statistiques des forums

Discussions
311 719
Messages
2 081 871
Membres
101 828
dernier inscrit
Did-Pan