繁体   English   中英

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

[英]Excel VBA: protecting my worksheets slows down my vba code significantly

我对VBA很新,并且在构建我当前的Excel“合同”时基本上自学了。 我的目标是有一个合同选项列表,根据其代表性复选框显示或隐藏。 总共有12个选项,我在4个工作表中显示/删除范围。

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

在我保护工作表之前,当我选中一个复选框时,所有4个工作表中的所有4个范围都会立即显示。 当我取消选择时,他们立即清除其内容并隐藏。 好极了!

但是,一旦我保护我的工作表,事情要么慢下来要么爬行,否则我会收到错误。 在下面的ProtectWorksheet模块中,注释掉的行可以工作,但是通过阅读其他堆栈溢出文章,它可以更好地使用我拥有的代码。 不受保护,效果很好。 受保护我得到“错误1004”:无法设置Range类的隐藏属性“。 如果我在受保护的情况下使用我注释掉的代码,它可以工作,但速度非常慢。

从技术上讲,我可以让一切工作......但从用户界面的立场来看,这很糟糕。

以下是我一直在测试的第一个合同选项。 请感谢您的帮助!

在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

在模块下:复选框

 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

在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

在模块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

编辑我能够通过更新下面的保护开/关代码来加快它的速度,但是当我点击我的复选框时,它仍然是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

编辑 - 解决方案? 所以我认为这不是最好的做法,我也没有真正“解决”我的延迟,但我找到了一个解决方法。 通过启用保护但允许行格式化来单击我的复选框时,我消除了延迟。 从技术上讲,我的表单不再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

它应该不会那么慢,虽然我真的不知道你的PC有多快,数据有多大。 但是,您可以做得更好:

Sub ClearDomesticHotWater()

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

End Sub

并删除所有选择,他们正在减慢你的速度。 像这样绕过它们: 如何避免在Excel VBA宏中使用Select

暂无
暂无

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

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