Excel VBA:保护我的工作表显着减慢我的vba代码

我对VBA非常陌生,在build立我现在的Excel'合同'的同时,基本上已经自学了。 我的目标是根据代表checkbox显示或隐藏合同选项列表。 我有4个工作表显示/删除范围,共有12个选项。

在组织方面,我已经根据每个行动利用模块。 我也命名了所有的范围

在保护我的工作表之前,当我select一个checkbox时,所有4个工作表中的所有4个范围立即显示。 当我不选时,他们立即清除他们的内容并隐藏起来。 好极了!

一旦我保护我的工作表,但是,事情要么放慢速度爬行,要么出现错误。 在下面的ProtectWorksheet模块中,注释掉的行可以工作,但是从阅读其他堆栈溢出的文章,它更好地使用我所拥有的代码。 不受保护的,它工作得很好。 受保护我得到“错误1004”:无法设置范围类的隐藏属性“。 如果我使用我的注释掉的代码,同时保护,它的工作原理,但速度超慢。

从技术上讲,我可以让一切工作…但从用户界面的立场是可怕的。

以下是我已经testing的第一份合约选项。 请和任何和所有的帮助谢谢你!

在Excel对象下 – sheet2(数据input)

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 

在模块:checkbox下

  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 

在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 

在Module 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 

编辑我能够通过更新下面的保护开启/closures代码来加速它,但是当我点击我的checkbox时,它仍然有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 

编辑 – 解决scheme? 所以我不认为这是最好的做法,也没有真正解决我的延误,但我find了一个解决方法。 我点击我的checkbox,打开保护,但允许行格式化,我消除了延迟。 从技术上讲,我的表不再受到用户修补的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 

它不应该那么慢,但我真的不知道你的电脑有多快,数据有多大。 不过,这里有一些你可以做得更好的东西:

 Sub ClearDomesticHotWater() For Each cell In [DataInput_DomesticHotWater] If cell.Interior.Color = RGB(226, 239, 218) Then cell.ClearContents End If Next End Sub 

并删除所有的select,他们放缓你。 像这样绕过它们: 如何避免在Excel VBAmacros中使用Select