Listing Unprotecting Worksheet Assets with the Unprotect Method

Function UnprotectWorksheet(ws As Worksheet, sPassword As String) As Boolean On Error GoTo ErrHandler If ws.ProtectContents Then ws.Unprotect sPassword End If

UnprotectWorksheet = True Exit Function

ErrHandler:

UnprotectWorksheet = False

End Function

Sub TestProtection() Dim ws As Worksheet

Set ws = ThisWorkbook.Worksheets(1)

' Example of how you might use ProtectWorksheet If Not ProtectWorksheet(ws, "TestPassword") Then

Debug.Print "The worksheet could not be protected."

Else

Debug.Print "The worksheet has been protected." End If

' Example of how you might use Unprotect Worksheet If UnprotectWorksheet(ws, "TestPassword") Then ' Unprotected - safe to modify the worksheet ' contents programmatically now... Debug.Print "The worksheet has been unprotected."

Else

Debug.Print "The worksheet could not be unprotected." End If

Set ws = Nothing End Sub

0 0

Post a comment