Consulting

Results 1 to 7 of 7

Thread: Solved: Need to know how to Unprotect all sheets at once with a macro

  1. #1
    VBAX Regular Erays's Avatar
    Joined
    Mar 2005
    Posts
    73
    Location

    Solved: Need to know how to Unprotect all sheets at once with a macro

    Need to know how to Unprotect all sheets at once with a macro and then later go back and protect all sheets at once.

  2. #2
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    [vba]Sub ProtectAllSheets()
    Dim s As Worksheet

    For Each s In ThisWorkbook.Sheets
    s.Select
    Cells.Select
    Selection.Locked = True
    Selection.FormulaHidden = False
    s.Protect Password:="test", DrawingObjects:=True, Contents:=True, Scenarios:=True
    Next

    End Sub

    Sub UnProtectAllSheets()
    Dim s As Worksheet

    For Each s In ThisWorkbook.Sheets
    s.Select
    s.UnProtect Password:="test"
    Next

    End Sub [/vba]

    OH! There's a KB entry here:
    http://www.vbaexpress.com/kb/getarticle.php?kb_id=142

    Just search the KB first, Erays.
    ~Anne Troy

  3. #3
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    No need to select anything.
    [vba]
    Sub Unprotect()

    Dim ws As Worksheet

    For Each ws In Worksheets
    ws.Unprotect Password:="MyPassword"
    Next

    End Sub

    Sub Protect()

    Dim ws As Worksheet

    For Each ws In Worksheets
    ws.Protect Password:="MyPassword", DrawingObjects:=True, Contents:=True, Scenarios:=True
    Next

    End Sub[/vba]

  4. #4
    VBAX Regular Erays's Avatar
    Joined
    Mar 2005
    Posts
    73
    Location
    How would i just unlock the sheet and then lock just the sheet back not the cells

  5. #5
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    My code does not change the cells from being locked or not.

  6. #6
    VBAX Regular Erays's Avatar
    Joined
    Mar 2005
    Posts
    73
    Location
    I had to make a change
    [VBA] Sub Unprotect()

    Dim ws As Worksheet

    For Each ws In Worksheets
    ' Changed Worksheet.Unprotect Password:="MyPassword"
    ws.Unprotect Password:="MyPassword"
    Next

    End Sub

    Sub Protect()

    Dim ws As Worksheet

    For Each ws In Worksheets
    ws.Protect Password:="MyPassword", DrawingObjects:=True, Contents:=True, Scenarios:=True
    Next

    End Sub[/VBA]
    Last edited by Erays; 03-20-2005 at 11:14 AM. Reason: spelling

  7. #7
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Ok, I mistyped that line in my other reply. Fixed now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •