简体   繁体   English

由 ThisWorkbook.Close 调用时,BeforeClose 脚本中的 Excel 取消保护工作表不起作用

[英]Excel unprotect sheet in a BeforeClose script not working when called by ThisWorkbook.Close

I have an excel workbook that protects and hides all worksheets but one before the workbook is closed.我有一个 excel 工作簿,可以保护和隐藏所有工作表,但在工作簿关闭之前。 On that one worksheet it is supposed to unprotect the sheet, clear out some cell values, then reprotect the sheet.在那个工作表上,它应该取消保护工作表,清除一些单元格值,然后重新保护工作表。

When I close the workbook with the X in the top corner the sub runs just fine.当我用顶角的 X 关闭工作簿时,子程序运行得很好。 When I try to use ThisWorkbook.Close the sub does not actually unprotect the sheet so I get a Run-time error '1004' when it tries to clear the cell values.当我尝试使用 ThisWorkbook.Close 时,sub 实际上并未取消对工作表的保护,因此当它尝试清除单元格值时,我会收到运行时错误“1004”。

I need the .Close method to work for another script which will close the workbook after x amount of time.我需要 .Close 方法为另一个脚本工作,该脚本将在 x 时间后关闭工作簿。

Before Close Script关闭脚本前

Private Sub Workbook_BeforeClose(Cancel As Boolean)

ThisWorkbook.Unprotect Password:=pw
For Each Worksheet In ThisWorkbook.Worksheets
    Worksheet.Protect Password:=pw
    If Worksheet.Name = "Control Tab" Then
        With Worksheet
            .Unprotect Password:=pw
            .Cells(24, 4).Value = ""
            .Cells(24, 5).Value = ""
            .Cells(24, 6).Value = ""
            .Cells(24, 7).Value = ""
            .Cells(24, 8).Value = ""
            .Cells(24, 9).Value = ""
            .Cells(24, 10).Value = ""
            .Cells(24, 11).Value = ""
            .Cells(24, 12).Value = ""
            .Cells(24, 13).Value = ""
            .Cells(24, 14).Value = ""
            .Cells(24, 15).Value = ""
            .Cells(24, 16).Value = ""
            .Protect Password:=pw
        End With
    Else
        Worksheet.Visible = 0
    End If
Next
ThisWorkbook.Protect Password:=pw

ThisWorkbook.Save
End Sub

Button to close workbook (for testing)关闭工作簿的按钮(用于测试)

Sub Button1_Click()
    ThisWorkbook.Close
End Sub

I've tried to search for a reason why there might be a difference between problematically and manually closing a workbook on google and stackoverflow but haven't found any insight as to why the script won't unprotect the sheet.我试图寻找一个原因,为什么在 google 和 stackoverflow 上有问题地关闭工作簿和手动关闭工作簿之间可能存在差异,但没有找到关于脚本为什么不会取消保护工作表的任何见解。 Any help would be greatly appreciated.任何帮助将不胜感激。

First off, I wouldn't put that code in Workbook_BeforeClose;首先,我不会将该代码放在 Workbook_BeforeClose 中; I would put it in a Workbook_BeforeSave instead and use,我会把它放在 Workbook_BeforeSave 中并使用,

Private Sub Workbook_BeforeClose(Cancel As Boolean)

    if not thisworkbook.saved then _
        ThisWorkbook.Save

End Sub

... as the method of closing out the workbook. ...作为关闭工作簿的方法。 I'm not going to write paragraphs of white-paper jargon as to why;我不会写几段白皮书行话来说明原因; it just seems like a better method to me and still allows you to force the workbook to a certain condition before it is closed.这对我来说似乎是一种更好的方法,并且仍然允许您在关闭工作簿之前将其强制为特定条件。 With your method, it is theoretically possible to manually save the workbook in another condition then make a copy of the freshly saved workbook through file explorer without closing the workbook.使用您的方法,理论上可以在另一种情况下手动保存工作簿,然后在不关闭工作簿的情况下通过文件资源管理器制作新保存的工作簿的副本。

Finally, why isn't the Control Tab worksheet protected with the UserInterfaceOnly:=True argument?最后,为什么 Control Tab 工作表不受UserInterfaceOnly:=True参数保护? This would allow you to make any modification you want through VBA without unprotecting/modifying/reprotecting that particular worksheet while still restricting the user.这将允许您通过 VBA 进行任何您想要的修改,而无需取消保护/修改/重新保护该特定工作表,同时仍然限制用户。

Run this once then remove all VBA references to unprotect/protect the "Control Tab" worksheet (unless you specifically want to open it up to the user).运行一次,然后删除所有 VBA 引用以取消保护/保护“控制选项卡”工作表(除非您特别想向用户打开它)。

sub RunOnce()
    with thisworkbook.worksheets("Control Tab")
        .Unprotect Password:=pw
        .Protect Password:=pw, UserInterfaceOnly:=True
    end with
end sub

Any and all VBA coding that affects the Control Tab worksheet will run.任何和所有影响控制选项卡工作表的 VBA 编码都将运行。 Caveat: there may be some coded modification that require Application.DisplayAlerts = False if you want to avoid distracting message dialogs altogether.警告:如果您想完全避免分散消息对话框的注意力,可能会有一些需要 Application.DisplayAlerts = False 的编码修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM