将保护从'Full'切换到'UserInterfaceOnly'

时间:2014-01-09 12:34:40

标签: excel excel-vba vba

这是一个简单的问题,但有点狡猾地描述......

当我的团队打开工作簿时,必须保护工作表 - 即他们无法更改公式。因此,在Workbook_Save例程中,我将所有工作表都保护起来。

到目前为止,非常好。

Workbook_Open例程(以及一个或两个其他地方)中,我想将保护更改为UserInterFaceOnly,以便我的代码能够在工作表上进行更改。我用的是:

Sheet1.Unprotect Password≔”MyWord”
Sheet1.Protect Password≔”MyWord”, UserInterfaceOnly≔True

在我看来这一切似乎都会起作用,但麻烦的是,它没有。在运行上面的行后,工作表仍然受到完全保护 - 即不是UserInterfaceOnly

我正在使用2013年,但它需要在2007年开始工作。 2010年也是。

任何帮助,提示或指示都将被感激地收到并热切地申请! 非常感谢


回答答案......

    Sub ReProtTheSheets() 'This one turns full protection on
        shtHidden.Range("iProt") = False
        Call SetProtection
        shtHidden.Range("iProt") = True
        Call FullProtection
    End Sub
    '-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#
    Sub ResetTheSheets() 'This one turns User-Interface protection on
        shtHidden.Range("iProt") = False
        Call SetProtection
        shtHidden.Range("iProt") = True
        Call SetProtection
    End Sub
    '-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#
    Sub SetProtection()
    Dim xSheet As Worksheet

    Application.ScreenUpdating = False
    Application.EnableEvents = False
    On Error GoTo BackOn

        If shtHidden.[iProt] Then

            For Each xSheet In ThisWorkbook.Worksheets
                If IsOriginalSheet(xSheet) Then 'This UDF ensures that sheets added by the user are not affected
                    If xSheet.Name = ExecSumm.Name Then
    '                   Protect ExecSumm differently - Allow Insert/Delete Columns
                        xSheet.DisplayPageBreaks = False
                        xSheet.Protect UserInterfaceOnly:=True, DrawingObjects:=True, AllowInsertingColumns:=True, _
                                            AllowDeletingColumns:=True, Password:=shtHidden.[iWord]
                    ElseIf xSheet.Name = Engine.Name Then
                        xSheet.DisplayPageBreaks = False
                        xSheet.Protect UserInterfaceOnly:=True, DrawingObjects:=True, Password:=shtHidden.[iWord]
                    Else
                        xSheet.DisplayPageBreaks = False
                        xSheet.Protect UserInterfaceOnly:=True, DrawingObjects:=True, Password:=shtHidden.[iWord]
                    End If
                End If
            Next xSheet
        Else
            For Each xSheet In ThisWorkbook.Worksheets
                If IsOriginalSheet(xSheet) Then 'This UDF ensures that sheets added by the user are not affected
                    xSheet.Unprotect Password:=shtHidden.[iWord]
                    xSheet.DisplayPageBreaks = False
                End If
            Next xSheet
        End If
    BackOn:
    Application.EnableEvents = True
    End Sub
    '-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#-#~#
    Sub FullProtection()
    Dim xSheet As Worksheet

    Application.ScreenUpdating = False
    Application.EnableEvents = False
    On Error GoTo BackOn

        If shtHidden.[iProt] Then

            For Each xSheet In ThisWorkbook.Worksheets
                If IsOriginalSheet(xSheet) Then 'This UDF ensures that sheets added by the user are not affected
                    If xSheet.Name = ExecSumm.Name Then
    '                   Protect ExecSumm differently - Allow Insert/Delete Columns
                        xSheet.DisplayPageBreaks = False
                        xSheet.Protect DrawingObjects:=True, AllowInsertingColumns:=True, _
                                            AllowDeletingColumns:=True, Password:=shtHidden.[iWord]
                    ElseIf xSheet.Name = Engine.Name Then
                        xSheet.DisplayPageBreaks = False
                        xSheet.Protect DrawingObjects:=True, Password:=shtHidden.[iWord]
                    Else
                        xSheet.DisplayPageBreaks = False
                        xSheet.Protect DrawingObjects:=True, Password:=shtHidden.[iWord]
                    End If
                End If
            Next xSheet
        Else
            For Each xSheet In ThisWorkbook.Worksheets
                If IsOriginalSheet(xSheet) Then 'This UDF ensures that sheets added by the user are not affected
                    xSheet.Unprotect Password:=shtHidden.[iWord]
                    xSheet.DisplayPageBreaks = False
                End If
            Next xSheet
        End If
    BackOn:
    Application.EnableEvents = True
    End Sub

1 个答案:

答案 0 :(得分:0)

您的密码带有引号,VBA不接受它们作为字符串分隔符。你需要直引号。

Sheet1.Unprotect Password≔"MyWord"
Sheet1.Protect Password≔"MyWord", UserInterfaceOnly≔True

而不是

Sheet1.Unprotect Password≔”MyWord”
Sheet1.Protect Password≔”MyWord”, UserInterfaceOnly≔True

这是从Word或类似文件复制到VBA编辑器的危险。