XL 2019 VBA Tri à n critères avec liste spéciale

Spitnolan08

XLDnaute Barbatruc
Bonjour à tous,

J'essaie de rationaliser l'usage de la fonction Tri à n niveaux via une routine type mais, j'ai un petit souci avec le passage de l'argument CustomOrder (A ne pas confondre avec orderCustom...) pour le passage d'une liste particulière

Je ne comprends pas pourquoi, ce code ne fonctionne pas
VB:
Sub triTest4b()
    Dim Liste4 As String
    Liste4 = "Marché,Avenant"
  
    Application.AddCustomList ListArray:=Array("Marché", "Avenant")
    ActiveWorkbook.Worksheets("BDTest").Sort.SortFields.Clear
    With ActiveWorkbook.Worksheets("BDTest").Sort.SortFields
       .Add2 Key:=Range("A:A"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
       .Add2 Key:=Range("C:C"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

       ''***Cas spécial avec liste particulière
       .Add2 Key:=Range("F:F"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:=Liste4, DataOption:=xlSortNormal
       .Add2 Key:=Range("V:V"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    End With
  
    With ActiveWorkbook.Worksheets("BDTest").Sort
        .SetRange ActiveSheet.UsedRange
        .Header = xlYes 'xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .Apply
    End With
End Sub

Le problème se situe sur la ligne .Add2 Key:=Range("F:F"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:=Liste4, DataOption:=xlSortNormal
alors que celui ci fonctionne parfaitement...

Code:
Sub triTest4b()
    Dim Liste4 As String
    Liste4 = "Marché,Avenant"
  
    Application.AddCustomList ListArray:=Array("Marché", "Avenant")
    ActiveWorkbook.Worksheets("BDTest").Sort.SortFields.Clear
    With ActiveWorkbook.Worksheets("BDTest").Sort.SortFields
       .Add2 Key:=Range("A:A"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
       .Add2 Key:=Range("C:C"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

       ''***Cas spécial avec liste particulière
       .Add2 Key:=Range("F:F"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:='"Marché,Avenant", DataOption:=xlSortNormal
       .Add2 Key:=Range("V:V"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    End With
  
    With ActiveWorkbook.Worksheets("BDTest").Sort
        .SetRange ActiveSheet.UsedRange
        .Header = xlYes 'xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .Apply
    End With
End Sub

Nota : ces codes nécessitent des tableaux avec en tête de ligne (Titres). Sinon, il faut changer xlYes en xlGuess ou xlNo
Avez vous une solution pour passer cet argument en variable ?

Edit : je viens de me rendre compte de ça :

SortField. CustomOrder, propriété (Excel) Spécifie un ordre personnalisé pour trier les champs. Variant en lecture/écriture.​


Mais même en passant Liste4 en Variant ça ne change rien
 
Dernière édition:

Discussions similaires

Statistiques des forums

Discussions
311 720
Messages
2 081 917
Membres
101 839
dernier inscrit
laurentEstrées