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

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

  • tri_test.xlsm
    62 KB · Affichages: 7
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...

fanch55

XLDnaute Barbatruc
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 ...
 

Usine à gaz

XLDnaute Barbatruc
Supporter XLD
Re-Bonjour,

Je n'arrive pas à trouver où mettre ton code :mad:
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,
 

fanch55

XLDnaute Barbatruc
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 ...
 

Discussions similaires

Statistiques des forums

Discussions
312 176
Messages
2 085 967
Membres
103 072
dernier inscrit
Remithesix