Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

[HS] suite à question sur protection de feuilles avec userform

1 réponse
Avatar
LSteph
Bonsoir,

Pour protèger ou déprotèger toutes les feuilles
avec saisie d'un mot de passe

A part le plaisir de coder ce n'est pas forcément utile.
Voici ce que m'a inspiré un sujet, si ça intéresse,
cela peut sûrement être simplifié ou amélioré...
ou corrigé ..(merci)

http://cjoint.com/?jEvyqO7UjZ

Code plus bas

Cdlt.

--
lSteph


Il faut insérer un userform et un module standard , dans le userform
mettre un label un textbox deux boutons(valider , annuler)

'''''code de Module1
Option Explicit
Public testP As Boolean

Sub ProtectOrNotAllSh()
Dim sh As Worksheet, msg As Integer
Call verifProtect
If testP Then
msg = MsgBox("Une ou des feuille(s) sont protégée(s)" _
& vbCrLf & "voulez-vous déprotèger", vbOKCancel, _
"Protection des feuilles")
If msg = 1 Then UserForm1.Show
Else
UserForm1.Show
End If
End Sub

Function protectState(ws As Worksheet) As Boolean
If ws.ProtectContents Or _
ws.ProtectDrawingObjects Or _
ws.ProtectScenarios Then
protectState = True
End If
End Function
Sub verifProtect()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Sheets
If protectState(sh) Then testP = True: Exit For
Next
End Sub

'''''code Userform1
Private Sub UserForm_Initialize()
TextBox1.PasswordChar = "@"
If testP Then
Me.Caption = "Déprotection des feuilles"
Label1.Caption = "entrer le mot de passe "
Else
Me.Caption = "Protection des feuilles"
Label1.Caption = "saisir un mot de passe "
End If
End Sub

Private Sub Valider_Click()

If Len(myPass) = 0 Then
If Len(TextBox1) < 4 Then
Label1.Caption = "mot de passe 4 caractères minimum"
TextBox1.SetFocus: Exit Sub
Else
myPass = TextBox1
If Not testP Then
Label1.Caption = "confirmer mot de passe"
Valider.Caption = "Confirmer"
TextBox1.Text = ""
TextBox1.SetFocus
Else
Call Deprotege
End If
End If
Else
If TextBox1 <> myPass Then
myPass = ""
MsgBox "Confirmation erronée": Unload Me
Else
Call Protege
Unload Me
End If
End If
End Sub

Private Sub Protege()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Sheets
sh.Protect myPass
Next
myPass = ""
End Sub
Private Sub Deprotege()
Dim sh As Worksheet
testP = False
On Error Resume Next
For Each sh In ActiveWorkbook.Sheets
If protectState(sh) Then sh.Unprotect myPass
Next
Call verifProtect
If testP Then
MsgBox "feuilles toujours protégées vérifier le mot de passe"
End If
Unload Me
End If
End Sub
'''''''''''''''''''''

1 réponse

Avatar
Lou
J'avais justement un classeur contenant 38 feuilles protégées par un même mot
de passe. Votre solution m'a grandement aidé alors je voulais simplement
vous remercier de mettre cet outil à notre disponibilité.

Merci grandement.

Lou:-)


Bonsoir,

Pour protèger ou déprotèger toutes les feuilles
avec saisie d'un mot de passe

A part le plaisir de coder ce n'est pas forcément utile.
Voici ce que m'a inspiré un sujet, si ça intéresse,
cela peut sûrement être simplifié ou amélioré...
ou corrigé ..(merci)

http://cjoint.com/?jEvyqO7UjZ

Code plus bas

Cdlt.

--
lSteph


Il faut insérer un userform et un module standard , dans le userform
mettre un label un textbox deux boutons(valider , annuler)

'''''code de Module1
Option Explicit
Public testP As Boolean

Sub ProtectOrNotAllSh()
Dim sh As Worksheet, msg As Integer
Call verifProtect
If testP Then
msg = MsgBox("Une ou des feuille(s) sont protégée(s)" _
& vbCrLf & "voulez-vous déprotèger", vbOKCancel, _
"Protection des feuilles")
If msg = 1 Then UserForm1.Show
Else
UserForm1.Show
End If
End Sub

Function protectState(ws As Worksheet) As Boolean
If ws.ProtectContents Or _
ws.ProtectDrawingObjects Or _
ws.ProtectScenarios Then
protectState = True
End If
End Function
Sub verifProtect()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Sheets
If protectState(sh) Then testP = True: Exit For
Next
End Sub

'''''code Userform1
Private Sub UserForm_Initialize()
TextBox1.PasswordChar = "@"
If testP Then
Me.Caption = "Déprotection des feuilles"
Label1.Caption = "entrer le mot de passe "
Else
Me.Caption = "Protection des feuilles"
Label1.Caption = "saisir un mot de passe "
End If
End Sub

Private Sub Valider_Click()

If Len(myPass) = 0 Then
If Len(TextBox1) < 4 Then
Label1.Caption = "mot de passe 4 caractères minimum"
TextBox1.SetFocus: Exit Sub
Else
myPass = TextBox1
If Not testP Then
Label1.Caption = "confirmer mot de passe"
Valider.Caption = "Confirmer"
TextBox1.Text = ""
TextBox1.SetFocus
Else
Call Deprotege
End If
End If
Else
If TextBox1 <> myPass Then
myPass = ""
MsgBox "Confirmation erronée": Unload Me
Else
Call Protege
Unload Me
End If
End If
End Sub

Private Sub Protege()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Sheets
sh.Protect myPass
Next
myPass = ""
End Sub
Private Sub Deprotege()
Dim sh As Worksheet
testP = False
On Error Resume Next
For Each sh In ActiveWorkbook.Sheets
If protectState(sh) Then sh.Unprotect myPass
Next
Call verifProtect
If testP Then
MsgBox "feuilles toujours protégées vérifier le mot de passe"
End If
Unload Me
End If
End Sub
'''''''''''''''''''''