简体   繁体   English

Excel 365 vba -(取消)保护工作表会大大减慢宏的速度

[英]Excel 365 vba - (un)protect sheet slows down macro a lot

Unprotecting/protecting a sheet using VBA is much slower in Excel 365 compared to Excel 2007. Does anyone have the slightest idea as to why this is the case?与 Excel 2007 相比,使用 VBA 取消保护/保护工作表在 Excel 365 中要慢得多。有没有人知道为什么会这样? And is there anything that can be done to resolve this without just omitting the protect-functions?有什么办法可以在不忽略保护功能的情况下解决这个问题?

To make sure the issue is caused solely by the unprotect/protect statements, I tested with a blank workbook containing a single module with the following code:为确保问题由 unprotect/protect 语句引起,我使用包含具有以下代码的单个模块的空白工作簿进行了测试:

Dim secondsElapsed As Double

startTime = Timer
Sheets("test").Unprotect ("****")
ThisWorkbook.Sheets("Timers").Cells(1, 1) = Round(Timer - startTime, 2)

startTime = Timer
Sheets("test").Protect Password:="****", DrawingObjects:=False, Contents:=True, Scenarios:=True, AllowSorting:=True, AllowFiltering:=True
ThisWorkbook.Sheets("Timers").Cells(2, 1) = Round(Timer - startTime, 2)

` `

This yields the following averages:这产生以下平均值:

  • Excel 2007 Excel 2007
    • unprotect: 0.02 seconds取消保护:0.02 秒
    • protect: 0.01 seconds保护:0.01秒
  • Excel 365 Excel 365
    • unprotect: 0.52 seconds取消保护:0.52 秒
    • protect: 0.47 seconds保护:0.47 秒

After more research and asking on other forums, I have found a solution.经过更多研究并在其他论坛上询问,我找到了解决方案。 It is possible to protect only for the user, and thus not for VBA, by adding the UserInterfaceOnly:=True parameter in the protect statement.通过在保护语句中添加UserInterfaceOnly:=True参数,可以只保护用户,而不保护 VBA。 Therefore, it is merely necessary to protect once (ie the (un)protect statements in the rest of the code can be omitted).因此,只需要保护一次(即可以省略其余代码中的(un)protect 语句)。

Of course, this does not yet explain why the (un)protect statements take this long.当然,这还不能解释为什么 (un)protect 语句需要这么长时间。 To try to find an answer tot this problem, I will contact Microsoft directly.为了尝试找到这个问题的答案,我将直接与 Microsoft 联系。

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

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