简体   繁体   中英

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.

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. When run manually within workbook: 0.01 seconds.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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