Codigo para proteger e deproteger planilha

thiagoiath

17-09-2011 23:09:16

Caros amigos, estou fazendo um codigo em VBA, só que não estou conseguindo avançar... Se alguem puder dar uma ajuda, ficaria agradecido!
Ele faz o seguinte: verifica se a planilha esta protegida atraves de um teste lógico. Se estiver desprotegida, protege com uma senha, caso contrario
desprotege. Mas a condição lógica não está funcionando... alguem tem alguma ideia?

Abs



Sub Proteger()

If ActiveSheet.Protect = false Then
ActiveSheet.Protect Password:="213454", DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save

Else
ActiveSheet.Unprotect Password:="213454"
End If

End Sub

Mauro Coutinho

17-09-2011 23:30:29

thiago, recomendo usar a rotina indicada no link abaixo :

http://www.vbaexpress.com/kb/getarticle.php?kb_id=239

Código: Selecionar todos

Private Function SheetProtected(TargetSheet As Worksheet) As Boolean
     'Function purpose:  To evaluate if a worksheet is protected
     
    If TargetSheet.ProtectContents = True Then
        SheetProtected = True
    Else
        SheetProtected = False
    End If
     
End Function
 
Sub SimpleTest()
     'Macro purpose:  To demonstrate use of SheetProtected Function
     '*** THIS MACRO FOR ILLUSTRATIVE PURPOSES ONLY     ***
     '*** AND IS NOT REQUIRED TO USE THE ABOVE FUNTION! ***
     
     'Assign active worksheet to variable to be tested
    Dim ws As Worksheet
    Set ws = ActiveSheet
     
     'Testa as activesheet's proteção
    If SheetProtected(ws) Then
         'Se protegida
        MsgBox "Sorry, but " & ws.Name & " está Protegida!", _
        vbOKOnly + vbInformation, ws.Name & " Protegida!"
    Else
         'Se Desprotegida
        MsgBox "Hooray!  " & ws.Name & " Não está Protegida!", _
        vbOKOnly + vbInformation, ws.Name & " DesProtegida!"
    End If
End Sub
[]ss

thiagoiath

18-09-2011 02:21:58

Funcionou!!!
Cara te amo! huahuahuahu

obrigado mesmo! só tem fera aqui.

abs

mcdonald

24-09-2011 15:33:52

O usuário poderá fazer alguma alteração na planilha?