我写了一个宏来清除活动单元格行的内容,然后调用一个模块来移动剩余的行。我正在经历漫长的等待时间让宏完成运行。不确定这是否可以写得更好,以便更快地执行。当用户单击用户表单上的“删除客户端”时,将调用第一个模块。任何帮助,将不胜感激。谢谢!
'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
答案 0 :(得分:2)
为什么不改变这一行:
ws.Range("C" & ActiveCell.Row & ":BJ" & ActiveCell.Row).ClearContents
对此:
ws.Range("C" & ActiveCell.Row & "BJ" & ActiveCell.Row).EntireRow.Delete
通过这种方式,您可以避免将第二个子组合在一起(或者将其保留为偶尔使用的清洁器,而不是每次只需删除1行时运行它。)
如果你确实需要两个潜艇,效率的常见第一步是在使用Application.ScreenUpdating = False
进入循环之前禁用屏幕更新,然后在循环结束时通过将False
更改为重新激活它True
。
答案 1 :(得分:0)
这是urdearboy答案的后续行动......
问题在于您的第二个功能和使用的静态范围。您删除了最后的所有行,超过了您的数据(最多约380个额外的删除行调用)。要解决它,你应该做两件事
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
我在常用函数上使用2来保持代码清洁......
<强> 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 强>
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