Automatically Protect/UnProtect All Worksheets

Posted on the September 26th, 2008 under Workbook and Worksheet by Poer @ Excel VBA/Macro

Tonight I want to automatically protect and unprotect all Microsoft Excel Worksheets in my Workbook with password, instead of doing it manually, I put these following Excel VBA macro code in my Workbook.

Public Sub ProtectAllSheets()

    Dim objSheet As Worksheet

    'Protecting all worksheets with password
    For Each objSheet In Worksheets
        If objSheet.ProtectContents = False Then objSheet.Protect "a@#&ladfl&^"
    Next objSheet

End Sub

Public Sub UnProtectAllSheets()

    Dim objSheet As Worksheet

    'UnProtecting all worksheets with password
    For Each objSheet In Worksheets
        If objSheet.ProtectContents = True Then objSheet.Unprotect "a@#&ladfl&^"
    Next objSheet

End Sub

We only need to call the procedure ProtectAllSheets to protect with password all Excel Worksheets in our Workbook, and calling UnProtectAllSheets will reversed the effect.

Related Entries

External Resources

Tagged with:

Leave a Reply




XHTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
SYNTAX HIGHLIGHTER:
Place your VBA code between <pre> tags like this <pre class="brush:vb"> sub vba() ... end sub </pre>.