简体   繁体   English

有没有一种方法可以提高在Excel VBA中激活工作表时取消保护大范围的效率?

[英]Is there a way to increase efficiency of unprotecting a large range upon worksheet activation in Excel VBA?

I currently have a worksheet that, upon activation, unlocks a portion of the worksheet for user entry, but protects the rest of the worksheet. 我目前有一个工作表,该工作表在激活后会解锁一部分工作表供用户输入,但会保护其余工作表。 When opening the worksheet, there is a noticeable delay before becoming active for the user. 打开工作表时,在被用户激活之前会有明显的延迟。 Is there a way to use an array for this? 有没有办法为此使用数组?

Dim ws As Worksheet
Dim rng As Range
Dim ce As Range

Set ws = ActiveSheet
Set rng = Union(ws.Range("B4:B9"), ws.Range("E4:E9"), ws.Range("I4:I10"), ws.Range("B10"), ws.Range("A14:O425"))

Application.ScreenUpdating = False
ws.Unprotect Password:="mediate"
For Each ce In rng
    ce.Select
    Selection.Locked = "False"
Next ce

ws.Protect Password:="mediate", UserInterfaceOnly:=True, AllowFormattingCells:=True
Application.ScreenUpdating = True

Using Range.Locked = False works, but there was an additional mistake that I made that caused the errors I described. 使用Range.Locked = False可以工作,但是我犯了一个额外的错误,导致了我描述的错误。 I split each range out to use .Locked = False and figured out which ranges were being problematic. 我将每个范围划分为使用.Locked = False并找出哪个范围存在问题。 Some of the ranges have merged cells, and I did not specify the entire 'length' of the range. 一些范围合并了单元格,我没有指定范围的整个“长度”。 IE Range("E4:E9") should have been Range("E4:F9"). IE Range(“ E4:E9”)应该是Range(“ E4:F9”)。

Here is the working code: 这是工作代码:

Dim ws As Worksheet
Dim rng As Range

Set ws = Worksheets("Initial Entry")
Set rng = Union(ws.Range("B4:B9"), ws.Range("E4:F9"), ws.Range("I4:K10"), ws.Range("B10:F10"), ws.Range("A14:O425"))

rng.Locked = False

ws.Protect Password:="mediate", UserInterfaceOnly:=True, AllowFormattingCells:=True

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

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