Trier par ordre alphabétique

bonjour au forum

J'ai créé un code VBA pour trier par ordre alphabétique les données de mon fichier constitué de deux plages.

Mais a chaque fois que je saisis une donnée , au moment de l'exécution, il me revient "ERREUR DE COMPILATION" 'Sub ou Function non définie '

un membre peut- il m'aider à corriger mon erreur.

voici mon code VBA et le fichier joint

19exemple11.xlsm (20.01 Ko)
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 5 Then
        rang("B6:E15").Sort key1:=Range("B6")
    If Target.Column = 21 Then
        rang("S6:T15").Sort key1:=Range("S6")
    End If

End Sub

Bonjour,

Sans regarder le classeur, je peut te dire qu'en VBA la fonction RANG() c'est plutôt Application.WorksheetFunction.RANK()

https://docs.microsoft.com/fr-fr/office/vba/api/excel.worksheetfunction.rank

Bonjour,

A mon avis, ce n'est pas "rang" qui est une fonction comme dit par M12, mais "Range" qui fait référence à une plage de cellules.

Bonjour,
Pour le principe (pas optimisé) .
Cdlt.

21exemple11.xlsm (41.12 Ko)
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
Dim lo As ListObject, lo2 As ListObject

    Set lo = Me.Range("t_infos_1").ListObject
    Set lo2 = Me.Range("t_infos_2").ListObject

    If Not Intersect(Target, lo.ListColumns(5).DataBodyRange) Is Nothing Then
        With lo
            .Sort.SortFields.Add Key:=.ListColumns(2).DataBodyRange, SortOn:=xlSortOnValues
            .Sort.Header = xlYes
            .Sort.Apply
            .Sort.SortFields.Clear
        End With
    End If

    If Not Intersect(Target, lo2.ListColumns(5).DataBodyRange) Is Nothing Then
        With lo2
            .Sort.SortFields.Add Key:=.ListColumns(3).DataBodyRange, SortOn:=xlSortOnValues
            .Sort.Header = xlYes
            .Sort.Apply
            .Sort.SortFields.Clear
        End With
    End If

End Sub

@Jean-Eric

Il me semble que ce code serait plus optimisé:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lo As ListObject, lo2 As ListObject

    Set lo = Me.Range("t_infos_1").ListObject
    Set lo2 = Me.Range("t_infos_2").ListObject

    If Not Intersect(Target, lo.ListColumns(5).DataBodyRange) Is Nothing Then
        With lo
            .Range.Sort Key1:=.ListColumns(2), Order1:=xlAscending, Header:=xlYes
        End With
    End If

    If Not Intersect(Target, lo2.ListColumns(5).DataBodyRange) Is Nothing Then
        With lo2
            .Range.Sort Key1:=.ListColumns(3), Order1:=xlAscending, Header:=xlYes
        End With
    End If

End Sub

bonjour, c'était range pour définier la plage, avec un "e" supplémentair, mais le problème était surtout qu'un tableau doit être sorté en entier (avec entêtes), pas une partie.

Private Sub Worksheet_Change(ByVal Target As Range)

     If Target.Column = 5 Then     '= colonne E
          Range("B5").ListObject.Range.Sort Range("B5"), Header:=xlYes

     ElseIf Target.Column = 21 Then     '= colonne U
          Range("S5").ListObject.Range.Sort Range("S5"), Header:=xlYes
     End If

End Sub
Rechercher des sujets similaires à "trier ordre alphabetique"