Microsoft 365 ne pas avoir à modifier mes codes tris qd ajout/supprime une/des colonnes ds feuille

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 !

Usine à gaz

XLDnaute Barbatruc
Supporter XLD
Bonjour à tous et à tous,
Je vous souhaite une belle journée 🙂

Comme dit dans le titre, je cherche un moyen pour ne pas avoir à modifier tous mes codes de tris quand j'ajoute ou supprime une ou des colonnes à ma feuille.
Voilà longtemps que je cherche sans trouver de solution.
Z'auriez la solution ?

En cas, je joins un fichier test.
Je vous remercie,
Amicalement,
lionel 🙂
 

Pièces jointes

Solution
Exemple de code qui pourrait être commun à tous tes boutons sauf l'avant-dernier qui est hors colonne
VB:
Sub tri_Col()
Application.EnableEvents = False
    Application.ScreenUpdating = False
    RowMin = 3
    With ActiveSheet
'        .Unprotect Password:=""
        Col = .Shapes(Application.Caller).TopLeftCell.Column
        If .FilterMode Then .ShowAllData 'si la feuille est filtrée
        With .Rows(RowMin & ":" & .Cells(.Rows.Count, Col).End(xlUp).Row)
            If .Row < RowMin Then Exit Sub 'sécurité
            .Sort .Columns(Col), xlAscending, Header:=xlNo
        End With
'       .Protect Password:="", DrawingObjects:=True, Contents:=True, Scenarios:=True
    End With
Application.EnableEvents = True
End Sub
Tu modifies...
Salut Lionel,
dans les sub appelées par tes boutons, tu met en début (par exemple) :

C = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Column

C est alors le n° de la colonne où réside ton bouton.
Tu n'as plus qu'à trier sur celle-ci.
Par ailleurs si c'est toujours le même scénario, tu peux ainsi n'en faire qu'une ...
 
Re-Bonjour,

Je n'arrive pas à trouver où mettre ton code 😡
Par exemple :
VB:
Sub tri_N°suivi()
Application.EnableEvents = False
Application.ScreenUpdating = False
'ActiveSheet.Unprotect Password:=""
    'With ActiveSheet 'Feuille active
    With Sheets("Suivi")
    If .FilterMode Then .ShowAllData 'si la feuille est filtrée
    With .Rows("3:" & .Range("d65536").End(xlUp).Row)
        If .Row < 3 Then Exit Sub 'sécurité
        .Sort .Columns(4), xlAscending, Header:=xlNo
    End With
    End With
[a1].Select
'ActiveSheet.Protect Password:="", DrawingObjects:=True, Contents:=True, Scenarios:=True
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Tu veux bien me dire où je le mets ? 🙂
lionel,
 
Exemple de code qui pourrait être commun à tous tes boutons sauf l'avant-dernier qui est hors colonne
VB:
Sub tri_Col()
Application.EnableEvents = False
    Application.ScreenUpdating = False
    RowMin = 3
    With ActiveSheet
'        .Unprotect Password:=""
        Col = .Shapes(Application.Caller).TopLeftCell.Column
        If .FilterMode Then .ShowAllData 'si la feuille est filtrée
        With .Rows(RowMin & ":" & .Cells(.Rows.Count, Col).End(xlUp).Row)
            If .Row < RowMin Then Exit Sub 'sécurité
            .Sort .Columns(Col), xlAscending, Header:=xlNo
        End With
'       .Protect Password:="", DrawingObjects:=True, Contents:=True, Scenarios:=True
    End With
Application.EnableEvents = True
End Sub
Tu modifies la macro associée au bouton ...
 
- 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

Retour