工作表不会取消保护-Excel VBA

时间:2018-07-31 18:36:17

标签: vba excel-vba

由于某种原因,当我运行下面的代码时,我收到一条错误消息,指出我要更改的工作表未受保护。如果我使用Excel保存选项进行保存,则工作正常,如果我在工作表上运行宏,则会收到错误消息,指出工作表已受到保护。为什么代码无法识别取消保护纸张的部分?

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)


Dim sSheetName As String
Dim logSheet As Worksheet
Dim logRow As Range
Set logSheet = Sheets("LogDetails")
sSheetName = "1107"

    Sheets("LogDetails").Unprotect
    Application.EnableEvents = False
    Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = "Narrative Box"  *****Error at this line**
    Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 1).Value = Sheets("1107").Shapes("TextBox 1").TextFrame.Characters.Text
    Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 2).Value = Environ("username")
    Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 3).Value = Now

 Sheets("LogDetails").Columns("A:D").AutoFit
 Application.EnableEvents = True




End Sub

1 个答案:

答案 0 :(得分:0)

我能够通过添加以下内容来纠正上述问题:

Worksheets.protect, UserInterFaceOnly:=True 

代码打开时