Protection Feuilles

FoLKeN

XLDnaute Junior
Protection Feuilles VBA

Hello la communauté de l'excel :)

J'aurais besoin de savoir deux choses quant aux protections des feuilles:

1. J'ai une feuille contenant des cellules protégées qu'un utilisateur ne pourra pas remplir manuellement, et d'autres non-protégées que l'utilisateur peut modifier. Cependant, la feuille contient un bouton qui remplit les cellules protégées (et l'utilisateur ne peut toujours pas y toucher). Mais la protection, lorsqu'elle est active, m'empeche de remplir les cellules automatiquement par la macro. Y a-t-il un moyen simple d'éviter ça ? (optionnel: et ce quelque soit le password ?)

2. Peut-on avoir une option qui protège ou ôte la protection de toutes les feuilles d'un classeur d'un coup ? (avec un password identique par exemple) Soit avec les paramètres de protection en mémoire de chaque feuille ou soit avec les mêmes paramètres appliqués à toutes les feuilles.

Merci d'avance pour votre aide !
FoLKeN
 
Dernière édition:

FoLKeN

XLDnaute Junior
Re : Protection Feuilles

J'ai trouvé des réponses partielles à mes questions avec les fonctions .Protect et .Unprotect que je peux exécuter au début et à la fin de chaque macro.

En revanche j'aurais des problèmes avec les passwords. J'aimerais que ce soit transparent pour l'utilisateur.
 

FoLKeN

XLDnaute Junior
Re : Protection Feuilles

Bon bah pour une fois, mon sujet n'a pas l'air d'en faire bouger beaucoup. C'est pas assez intéressant ? :)

J'ai résolu partiellement mon problème avec un code pratique que je viens de faire qui pourra peut-être servir à certains (pour le cas n°1, mais le cas n°2 est similaire):

Dans un module:
Code:
Option Explicit

Dim ProtectArray(15) As Variant
Dim BoolProtect As Boolean
Const pw As String = "test"

Sub UnprotectSheet()
    
    Dim ws As Worksheet
    Set ws = ActiveSheet
    BoolProtect = False
    
    ' Checks if the sheet is protected
    If ws.ProtectContents = True Or ws.ProtectDrawingObjects = True Or ws.ProtectScenarios = True Then
        ' Save the protection properties
        ProtectArray(0) = ws.ProtectDrawingObjects
        ProtectArray(1) = ws.ProtectContents
        ProtectArray(2) = ws.ProtectScenarios
        ProtectArray(3) = ws.ProtectionMode
        ProtectArray(4) = ws.Protection.AllowFormattingCells
        ProtectArray(5) = ws.Protection.AllowFormattingColumns
        ProtectArray(6) = ws.Protection.AllowDeletingRows
        ProtectArray(7) = ws.Protection.AllowInsertingColumns
        ProtectArray(8) = ws.Protection.AllowInsertingRows
        ProtectArray(9) = ws.Protection.AllowInsertingHyperlinks
        ProtectArray(10) = ws.Protection.AllowDeletingColumns
        ProtectArray(11) = ws.Protection.AllowDeletingRows
        ProtectArray(12) = ws.Protection.AllowSorting
        ProtectArray(13) = ws.Protection.AllowFiltering
        ProtectArray(14) = ws.Protection.AllowUsingPivotTables
        BoolProtect = True
        ' Unprotect the worksheet
        ws.Unprotect (pw)
    End If

End Sub

Sub ProtectSheet()

    Dim ws As Worksheet
    Set ws = ActiveSheet
    ' Check if the sheet was protected
    If BoolProtect = True Then
        ws.Protect _
        Password:=pw, _
        DrawingObjects:=ProtectArray(0), _
        Contents:=ProtectArray(1), _
        Scenarios:=ProtectArray(2), _
        UserInterfaceOnly:=ProtectArray(3), _
        AllowFormattingCells:=ProtectArray(4), _
        AllowFormattingColumns:=ProtectArray(5), _
        AllowFormattingRows:=ProtectArray(6), _
        AllowInsertingColumns:=ProtectArray(7), _
        AllowInsertingRows:=ProtectArray(8), _
        AllowInsertingHyperlinks:=ProtectArray(9), _
        AllowDeletingColumns:=ProtectArray(10), _
        AllowDeletingRows:=ProtectArray(11), _
        AllowSorting:=ProtectArray(12), _
        AllowFiltering:=ProtectArray(13), _
        AllowUsingPivotTables:=ProtectArray(14)
        BoolProtect = False
    End If
    
End Sub

Dans le handler d'un bouton:
Code:
Private Sub Button_Click()
 
        Call UnprotectSheet
        Call MaMacro
        Call ProtectSheet
    
End Sub


Ceci dit, je ne gère pas les password différents. Et de plus je dois écrire le password en dur dans mon code VBA en tant que constante. Ce qui veut dire qu'il est visible dès qu'on va dans l'éditeur VB. Vous avez une idée pour résoudre tout ça ? Ou rendre mon code plus propre?
 

Discussions similaires

Statistiques des forums

Discussions
312 241
Messages
2 086 522
Membres
103 241
dernier inscrit
Peyo33