简体   繁体   English

工作簿打开缓慢

[英]Slow Workbook_Open event

I have a workbook that takes more than 6 seconds to open due to a number of macros that run within the workbook_open event. 我有一个工作簿,由于在workbook_open事件中运行了许多宏,因此需要6秒钟以上的时间才能打开。

I want to speed this up so I have used a timer to test different parts of the code at startup vs being run while the workbook is open. 我想加快速度,所以我使用了一个计时器来测试在启动和运行时打开工作簿时代码的不同部分。 All of the parts take the same time to run in both situations, except this part: 在这两种情况下,所有部分都需要花费相同的时间才能运行,但以下部分除外:

Dim ATime As Double
Dim BTime As Double

ATime = timer

Dim b As Long
For b = 5 To 268
    If Sheets("Orders").Range("F" & b) = "Locked" Then
        Sheets("Orders").Range("C" & b).Locked = True
        Sheets("Orders").Range("D" & b).Locked = True
        Sheets("Orders").Range("E" & b).Locked = True
    End If
Next

BTime = timer
MsgBox "1. " & Format(BTime - ATime, "0.00 \s\ec")

When run at workbook_open: 2.78 seconds. 在workbook_open运行时:2.78秒。 When run manually within workbook: 0.01 seconds. 在工作簿中手动运行时:0.01秒。

What is the problem here? 这里有什么问题?

Try: 尝试:

With Sheets("Orders")
    For b = 5 To 268
        .Range("C" & b).Resize(1, 3).Locked = (.Range("F" & b) = "Locked") 
    Next
End With

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

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