[英]VBA taking too long to execute
I have a macro written that clears contents of the active cell row then calls a module to shift the remaining rows up. 我写了一个宏,它清除活动单元格行的内容,然后调用一个模块将剩余的行向上移动。 I am experiencing a long wait time for the macro to finish running. 我正在等待很长时间才能完成宏的运行。 Not sure if this could be written better to execute quicker. 不知道这是否可以更好地编写以更快地执行。 The first module is called when a user clicks "Remove Client" on a User Form. 当用户单击用户窗体上的“删除客户端”时,将调用第一个模块。 Any help would be appreciated. 任何帮助,将不胜感激。 Thank you! 谢谢!
'Called when user clicks Remove Client on User Form
Sub letsgo()
Dim ws As Worksheet
Dim wb As Workbook
Set wb = ThisWorkbook
Set ws = wb.Sheets("contactunder")
ws.Range("C" & ActiveCell.Row & ":BJ" & ActiveCell.Row).ClearContents
Call shiftmeup
End Sub
Sub shiftmeup()
Dim ws As Worksheet
Dim wb As Workbook
Set wb = ThisWorkbook
Set ws = wb.Sheets("contactunder") '/// The underhood of my contacts
With ws.Range("D11:BJ392")
For i = .Rows.Count To 1 Step -1
If IsEmpty(.Cells(i, 1)) Then .Rows(i).Delete Shift:=xlUp
Next
End With
End Sub
Why not change this line: 为什么不更改此行:
ws.Range("C" & ActiveCell.Row & ":BJ" & ActiveCell.Row).ClearContents
To this: 对此:
ws.Range("C" & ActiveCell.Row & "BJ" & ActiveCell.Row).EntireRow.Delete
This way you can avoid your second sub all together (or keep this as an occasional cleaner rather run it every time you simply need to delete 1 row.) 这样,您就可以避免一起使用第二个子项(或者将其保留为偶尔的清理器,而不是每次只需要删除1行就运行一次。)
If you really do need both subs, a common first step for efficiency is to disable screen updating before entering your loop with Application.ScreenUpdating = False
and then re-activate it when your loop ends by changing False
to True
. 如果确实确实需要两个子控件,那么提高效率的第一步通常是禁用屏幕更新,然后使用Application.ScreenUpdating = False
进入循环,然后在循环结束时通过将False
更改为True
来重新激活它。
This is the followup to urdearboy's answer... 这是urdearboy的回答的后续行动...
The issue was in your second function and the static range used. 问题出在您的第二个功能和使用的静态范围中。 You were deleting all the rows at the end, past your data (up to ~380 extra delete row calls). 您要删除过去的所有行 ,超过数据(最多要再增加380个删除行调用)。 To fix it you should do two things 要修复它,您应该做两件事
Sub ShiftMeUp()
Dim wb As Workbook
Dim ws As Worksheet
Dim DeleteRowRange As Range
Set wb = ThisWorkbook
Set ws = wb.Sheets("contactunder") '/// The underhood of my contacts
For i = 1 To GetLastRow(1, ws)
If IsEmpty(ws.Cells(i, 1)) Then Set DeleteRowRange = MakeUnion(ws.Rows(i), DeleteRowRange)
Next
If Not DeleteRowRange Is Nothing Then DeleteRowRange.EntireRow.Delete Shift:=xlUp
End Sub
I used 2 on my commonly used functions to keep the code clean... 我在常用函数上使用了2,以保持代码的清洁。
MakeUnion MakeUnion
Public Function MakeUnion(Arg1 As Range, Arg2 As Range) As Range
If Arg1 Is Nothing Then
Set MakeUnion = Arg2
ElseIf Arg2 Is Nothing Then
Set MakeUnion = Arg1
Else
Set MakeUnion = Union(Arg1, Arg2)
End If
End Function
GetLastRow GetLastRow
Public Function GetLastRow(Optional Col As Integer = 1, Optional Sheet As Excel.Worksheet) As Long
If Sheet Is Nothing Then Set Sheet = Application.ActiveSheet
GetLastRow = Sheet.Cells(Sheet.Rows.Count, Col).End(xlUp).Row
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.