从工作表中删除命令button和行很慢

这个子工具将删除在工作表上以编程方式创build的所有命令button及其关联的行。 删除60个命令button和行需要大约20秒的时间。 我已经通过了它,可以发现没有问题。

Sub ResetForm_Click() Dim Contr Dim controlname As String Dim WS As Worksheet Set WS = ActiveSheet For Each Contr In WS.OLEObjects controlname = Mid(Contr.Name, 1, 2) If controlname = "CB" Then Contr.TopLeftCell.Rows.EntireRow.Delete Contr.Delete End If Next End Sub 

而不是逐行删除行尝试像这样(未经testing):

 Sub ResetForm_Click() Dim Contr Dim controlname As String Dim WS As Worksheet, rDel As Range Set WS = ActiveSheet For Each Contr In WS.OLEObjects controlname = Mid(Contr.Name, 1, 2) If controlname = "CB" Then If rDel Is Nothing then Set rDel = Contr.TopLeftCell Else Set rDel = Application.Union(rDel, Contr.TopLeftCell) End If Contr.Delete End If Next If Not rDel Is Nothing then rDel.EntireRow.Delete End Sub 

没有太多的编辑你的macros,closures屏幕更新和自动计算,看看是否有帮助。

在你的macros的开始(我通常把它放在我的声明之后),添加

 Application.ScreenUpdating = False Application.Calculation = xlCalculationManual 

然后在结束( End Sub之前),把它们重新打开

 Application.ScreenUpdating = True Application.Calculation = xlCalculationAutomatic