XL 2010 VBA Worksheet.Unprotect puis Worksheet.Protect en conservant les autorisations (proposition de macro)

Guy_M

XLDnaute Occasionnel
Bonjour,

J'ai appris à mes dépendants que Worksheet.Protect remet toutes les autorisations aux valeurs par défaut:eek:
J'ai donc cherché une solution pour conserver les autorisations telles qu'elles étaient au moment du Worksheet.Unprotect, je n'ai rien trouvé, j'ai donc écrit une macro pour gérer ça.
VB:
'Protège la feuille passée en paramètre en conservant les autorisations antérieures
'Worksheet.Protect met toutes les autorisation aux valeurs par défaut.
'Set Woksheet.Protection = ObjetProtection génère une erreur
'Worksheet.Unprotect met à False DrawingObjects, Contents, et Scenarios, si on veut resistituer l'état antérieur à Unprotect, ils devront être conservés et passés en paramètre
'Worksheet.Unprotect ne modifie pas les autres autorisations Worksheet.Protection
Sub ProtegeFeuille(Feuille As Worksheet, Optional MotDePasse As String = "", Optional LesObjetsDessines As Boolean = True, Optional LeContenu As Boolean = True, Optional LesScenarios As Boolean = True)
Feuille.Protect Password:=MotDePasse, _
    DrawingObjects:=LesObjetsDessines, _
    Contents:=LeContenu, _
    Scenarios:=LeScketch, _
    AllowFormattingCells:=Feuille.Protection.AllowFormattingCells, _
    AllowFormattingColumns:=Feuille.Protection.AllowFormattingColumns, _
    AllowFormattingRows:=Feuille.Protection.AllowFormattingRows, _
    AllowInsertingColumns:=Feuille.Protection.AllowInsertingColumns, _
    AllowInsertingRows:=Feuille.Protection.AllowInsertingRows, _
    AllowInsertingHyperlinks:=Feuille.Protection.AllowInsertingHyperlinks, _
    AllowDeletingColumns:=Feuille.Protection.AllowDeletingColumns, _
    AllowDeletingRows:=Feuille.Protection.AllowDeletingRows, _
    AllowSorting:=Feuille.Protection.AllowSorting, _
    AllowFiltering:=Feuille.Protection.AllowFiltering, _
    AllowUsingPivotTables:=Feuille.Protection.AllowUsingPivotTables
End Sub

Je suis dans l'attente des vos commentaires surtout si vous avez beaucoup plus simple, sinon en espérant que cela sera utile à quelqu'un.

A bientôt
GM
 

sylvanu

XLDnaute Barbatruc
Supporter XLD
Bonjour Guy,
Avec l'enregistreur de macro et en cochant toutes les autorisations possibles, on obtient ça :
VB:
Sub ProtectSheet()
    Sheets("Feuil1").Select
    ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
        False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
        AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows _
        :=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
        AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
        AllowUsingPivotTables:=True
End Sub
Donc en enregistrant avec vos choix spécifiques, vous obtiendrez plus simple.
 

Guy_M

XLDnaute Occasionnel
Merci @sylvanu pour ces éléments.
Mon objectif est d'avoir une méthode universelle, j'ai besoin de déprotéger pour travailler avec le contenu d'une feuille et je souhaite la restituter dans l'état de protection où elle était avant ça : au moment de déprotéger, je ne sais pas qu'elle sont les autorisations, par qui elles ont été mises ni pourquoi.
 
Dernière édition:

Statistiques des forums

Discussions
311 720
Messages
2 081 892
Membres
101 831
dernier inscrit
gillec