Variable objet ou variable de bloc with non définie

Bonjour,

Sur une de mes feuilles j'ai le code suivant pour déverrouiller toutes les feuilles du classeur.

Cependant quand je clique sur mon bouton "déprotection totale", j'ai le message en objet ci dessus qui apparait.

Si quelqu'un veut bien m'apporter ses lumières svp.

Option Explicit

Public Sub Protect_Sheets()
Dim sh As Worksheet
'    For Each sh In ActiveWorkbook.Sheets
'        If Not sh.ProtectContents Then
'            sh.Protect Password:=PWD
'        End If
'    Next

    With ActiveSheet
        If Not sh.ProtectContents Then
            sh.Protect Password:=PWD
        End If
    End With

End Sub

Public Sub UnProtect_Sheets()
Dim sh As Worksheet, r

    Do
        r = Application.InputBox(prompt:="Saisisssez le mot de passe", Type:=2)
        If r = False Then Exit Sub
    Loop While r = vbNullString
    If r <> PWD Then
    MsgBox "Mot de passe invalide!..."
    Exit Sub
    End If
'    For Each sh In ActiveWorkbook.Sheets
'        If sh.ProtectContents Then sh.Unprotect Password:=PWD
'    Next

    With ActiveSheet
ligne en erreur  ==>  If sh.ProtectContents Then sh.Unprotect Password:=PWD

    End With

End Sub

Bonjour,

Dans

If sh.ProtectContents Then sh.Unprotect Password:=PWD

sh est une variable définie en "WorkSheet" mais qui n'est pas initialisée. Il faudrait utiliser :

soit directement :

If Worksheets("nomdelaFeuille").ProtectContents .../...

soit initialiser la variable sh:

Set sh = Worksheets("nomdelaFeuille")
If sh.ProtectContents .../...

soit, puisqu'on utilise With ActiveSheet :

If .ProtectContents .../...

A+

Rechercher des sujets similaires à "variable objet bloc definie"