XL 2016 Trier un tableau

  • Initiateur de la discussion Initiateur de la discussion thehou
  • Date de début Date de début

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

thehou

XLDnaute Nouveau
Bonjour à tous,

je souhaite faire une petite modification dans du vba mais comme je suis novice je n'arrive pas à tout modifier.

Voici le code original de tri pour un tableau, nommé Tableau1, dans une feuille Sheet1

VB:
ActiveWorkbook.Worksheets("Sheet1").ListObjects("Table1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").ListObjects("Table1").Sort.SortFields.Add _
 Key:=Range("Table1[[#All],[Column1]]"), _
 SortOn:=xlSortOnValues, _
 Order:=xlAscending, _
 DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").ListObjects("Table1").Sort
 .Header = xlYes
 .MatchCase = False
 .Orientation = xlTopToBottom
 .SortMethod = xlPinYin
 .Apply
End With

Je souhaite que mon code fonctionne à chaque fois que je rajoute une feuille, mais je bloque dans
VB:
Range("Table1[[#All],[Column1]]")
, car je ne sais pas comment la modifier

VB:
ActiveWorkbook.ActiveSheet.ListObjects(1).Sort.SortFields.Clear
ActiveWorkbook.ActiveSheet.ListObjects(1).Sort.SortFields.Add _
 Key:=Range("Table1[[#All],[Column1]]"), _
 SortOn:=xlSortOnValues, _
 Order:=xlAscending, _
 DataOption:=xlSortNormal
With ActiveWorkbook.ActiveSheet.ListObjects(1).Sort
 .Header = xlYes
 .MatchCase = False
 .Orientation = xlTopToBottom
 .SortMethod = xlPinYin
 .Apply
End With

Un grand merci pour votre aide et bonne journée.
 
Bonjour,

Voyez :
VB:
Key:= .ListColumns(1).range

Dans :
Code:
Dim lo As ListObject
    Set lo = ActiveWorkbook.ActiveSheet.ListObjects(1)
    lo.Sort.SortFields.Clear
    lo.Sort.SortFields.Add _
            Key:=lo.ListColumns(1).Range, _
            SortOn:=xlSortOnValues, _
            Order:=xlAscending, _
            DataOption:=xlSortNormal
    With lo.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

Qui revient à :
Code:
    With ActiveWorkbook.ActiveSheet.ListObjects(1)
        With .Sort
            With .SortFields
                .Clear
                .Add _
                        Key:=.Parent.Parent.ListColumns(1).Range, _
                        SortOn:=xlSortOnValues, _
                        Order:=xlAscending, _
                        DataOption:=xlSortNormal
            End With
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End With

Cordialement
 
Dernière édition:
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

Réponses
17
Affichages
932
Réponses
6
Affichages
949
Réponses
11
Affichages
726
Réponses
1
Affichages
792
Retour