[英]Excel VBA: protecting my worksheets slows down my vba code significantly
I am very new to VBA and have basically taught myself while building my current Excel 'contract'. 我对VBA很新,并且在构建我当前的Excel“合同”时基本上自学了。 My goal is have a list of contract options which are shown or hidden depending on their representative check boxes.
我的目标是有一个合同选项列表,根据其代表性复选框显示或隐藏。 There are 12 total options with ranges that I show/remove across 4 worksheets.
总共有12个选项,我在4个工作表中显示/删除范围。
In terms of organization, I have utilized modules based on each action. 在组织方面,我已经根据每个行动使用了模块。 I also named all my ranges
我还命名了所有范围
Prior to me protecting my worksheet, when I select a checkbox, all 4 ranges across all 4 worksheets immediately show. 在我保护工作表之前,当我选中一个复选框时,所有4个工作表中的所有4个范围都会立即显示。 When I unselect, they immediately clear their contents and hide.
当我取消选择时,他们立即清除其内容并隐藏。 Yay!
好极了!
Once I protect my worksheet, however, things either slow down to a crawl or I get an error. 但是,一旦我保护我的工作表,事情要么慢下来要么爬行,否则我会收到错误。 In my ProtectWorksheet module below, the commented out lines work, but from reading other stack overflow articles it seens better to use the code I have.
在下面的ProtectWorksheet模块中,注释掉的行可以工作,但是通过阅读其他堆栈溢出文章,它可以更好地使用我拥有的代码。 Unprotected, it works great.
不受保护,效果很好。 Protected I get the "Error 1004': Unable to set the Hidden property of the Range class".
受保护我得到“错误1004”:无法设置Range类的隐藏属性“。 If I instead use my commented out code while protected, it works but is super slow.
如果我在受保护的情况下使用我注释掉的代码,它可以工作,但速度非常慢。
Technically I can get everything to work...but from a user interface stance it's terrible. 从技术上讲,我可以让一切工作......但从用户界面的立场来看,这很糟糕。
Below is the 1st contract option I have been testing. 以下是我一直在测试的第一个合同选项。 Please and thank you for any and all help!
请感谢您的帮助!
under the Excel Objects - sheet2(Data Input) 在Excel对象下 - sheet2(数据输入)
Private Sub chkDomesticHotWater_Click()
ProtectOFF
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
If chkDomesticHotWater = True Then
AddDomesticHotWater
Else
'Remove the lines, clear the data, and move the mouse to the top
RemoveDomesticHotWater
ClearDomesticHotWater
Range("A1").Select
End If
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
ProtectON
End Sub
under the Module: Checkboxes 在模块下:复选框
Sub AddDomesticHotWater()
[DataInput_DomesticHotWater].EntireRow.Hidden = False
[Contract_DomesticHotWater].EntireRow.Hidden = False
[Invoice_DomesticHotWater].EntireRow.Hidden = False
[ExpectedCost_DomesticHotWater].EntireRow.Hidden = False
End Sub
Sub RemoveDomesticHotWater()
[DataInput_DomesticHotWater].EntireRow.Hidden = True
[Contract_DomesticHotWater].EntireRow.Hidden = True
[Invoice_DomesticHotWater].EntireRow.Hidden = True
[ExpectedCost_DomesticHotWater].EntireRow.Hidden = True
End Sub
Under the Module ClearData 在Module ClearData下
Sub ClearDomesticHotWater()
Range("DataInput_DomesticHotWater").Select
For Each cell In Selection
If cell.Interior.Color = RGB(226, 239, 218) Then
cell.ClearContents
End If
Next
Range("DomesticHotWaterStart").Select
End Sub
under the Module ProtectWorksheet 在模块ProtectWorksheet下
Sub ProtectON()
Dim ws As Worksheet
Dim pwd As String
pwd = "123" ' Put your password here
For Each ws In Worksheets
ws.Protect Password:=pwd, UserInterfaceOnly:=True
Next ws
'Worksheets("Data Input").Protect Password:="123"
'Worksheets("Contract").Protect Password:="123"
'Worksheets("Invoice").Protect Password:="123"
'Worksheets("Expected Cost").Protect Password:="123"
End Sub
Sub ProtectOFF()
Dim ws As Worksheet
Dim pwd As String
pwd = "123" ' Put your password here
For Each ws In Worksheets
ws.Unprotect Password:=pwd
Next ws
'Worksheets("Data Input").Unprotect Password:="123"
'Worksheets("Contract").Unprotect Password:="123"
'Worksheets("Invoice").Unprotect Password:="123"
'Worksheets("Expected Cost").Unprotect Password:="123"
End Sub
EDIT I was able to speed it up just a tiny bit by updating my Protect On/Off code below, but it's still a 3-5 second delay when I click on my check boxes: 编辑我能够通过更新下面的保护开/关代码来加快它的速度,但是当我点击我的复选框时,它仍然是3-5秒的延迟:
Sub ProtectON()
Dim ws As Worksheet
Set WSArray = Sheets(Array("Data Input", "Contract", "Invoice", "Expected Cost"))
For Each ws In WSArray
ws.Protect Password:="123"
Next
End Sub
Sub ProtectOFF()
Dim ws As Worksheet
Set WSArray = Sheets(Array("Data Input", "Contract", "Invoice", "Expected Cost"))
For Each ws In WSArray
ws.Unprotect Password:="123"
Next
End Sub
EDIT - SOLUTION? 编辑 - 解决方案? So I don't think this is best practice, nor have I really 'solved' my delay, but I found a workaround.
所以我认为这不是最好的做法,我也没有真正“解决”我的延迟,但我找到了一个解决方法。 I eliminated the delay when clicking my check boxes by turning on protection yet allowing row formatting.
通过启用保护但允许行格式化来单击我的复选框时,我消除了延迟。 Technically my sheet is no longer 100% protected from user tinkering, but I think that risk is worth removing such an annoying wait time after clicking.
从技术上讲,我的表单不再100%免受用户修补,但我认为风险值得在点击后消除这样烦人的等待时间。
Sub ProtectON()
Dim ws As Worksheet
Set WSArray = Sheets(Array("Data Input", "Contract", "Invoice", "Expected Cost"))
For Each ws In WSArray
ws.Protect Password:="123", AllowFormattingRows:=True
Next
End Sub
It should not be that slow, although I really have no clue how fast is your PC and how big is the data. 它应该不会那么慢,虽然我真的不知道你的PC有多快,数据有多大。 However, here is something you can make better:
但是,您可以做得更好:
Sub ClearDomesticHotWater()
For Each cell In [DataInput_DomesticHotWater]
If cell.Interior.Color = RGB(226, 239, 218) Then
cell.ClearContents
End If
Next
End Sub
and remove all selects, they are slowing you down. 并删除所有选择,他们正在减慢你的速度。 Go around them like this: How to avoid using Select in Excel VBA macros
像这样绕过它们: 如何避免在Excel VBA宏中使用Select
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.