XL 2016 Trier un tableau

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.
 

Hasco

XLDnaute Barbatruc
Repose en paix
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:

Discussions similaires

Réponses
3
Affichages
572
Réponses
11
Affichages
427

Statistiques des forums

Discussions
312 169
Messages
2 085 918
Membres
103 038
dernier inscrit
Herve7