VBA/Excel/Access/Word/Excel/Workbook Protect

Материал из VB Эксперт
Перейти к: навигация, поиск

checks the UserName; if it is not Admin, this code protects each sheet from user changes

 
Private Sub Workbook_Open()
    Dim sht As Worksheet
    If Application.UserName <> "Admin" Then
        For Each sht In Worksheets
            sht.Protect UserInterfaceOnly:=True
        Next sht
    End If
End Sub



Protect workbook

 
Sub ProtectWorkbook()
    Dim wb As Workbook
    Set wb = ThisWorkbook
    wb.Protect "Excel2003", True, True
    "wb.Unprotect
End Sub



Show the workbook protection

 
Sub ShowWbProtection()
    Dim wb As Workbook
    Set wb = ThisWorkbook
    Debug.Print "Structure protected? " & wb.ProtectStructure, _
      "Windows protected? " & wb.ProtectWindows
End Sub



Unprotect Workbook

 
Sub UnprotectWorkbook()
    Dim wb As Workbook
    Set wb = ThisWorkbook
    wb.Unprotect "Excel2003"
End Sub