【发布时间】:2015-05-16 19:33:50
【问题描述】:
必填: 我正在尝试创建一个宏,为所有 零 值过滤 cell I22,选择所有这些行,删除它们,然后再次取消过滤。
我有什么: 目前我分两个不同的步骤执行此操作,因为一步执行此操作需要几个小时(因为它会删除每一行的行)
代码 (1):自动过滤到“零”和“不适用”,选择所有这些并清除所有内容。接下来它清除过滤器并从最大到最小排序。这样 excel 不必单独删除每一行,从而加快处理速度。
代码(2):删除所有空白行。
考虑到它需要完成的任务,我的印象是这段代码效率不高而且太长。是否可以将它们组合成一个代码?
代码 (1)
Sub clearalldemandzero()
clearalldemandzero Macro
ActiveWindow.SmallScroll Down:=15
Range("A26:EU26").Select
Selection.AutoFilter
ActiveWindow.SmallScroll ToRight:=3
ActiveSheet.Range("$A$26:$EU$5999").AutoFilter Field:=9, Criteria1:="=0.00" _
, Operator:=xlOr, Criteria2:="=#N/A"
Rows("27:27").Select
Range("D27").Activate
Range(Selection, Selection.End(xlDown)).Select
Selection.Clear
ActiveSheet.ShowAllData
Range("H28").Select
ActiveWorkbook.Worksheets("Solver 4").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Solver 4").AutoFilter.Sort.SortFields.Add Key:= _
Range("I26:I5999"), SortOn:=xlSortOnValues, Order:=xlDescending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Solver 4").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
代码 (2)
Sub DeleteBlankRows3()
'Deletes the entire row within the selection if the ENTIRE row contains no data.'
Dim Rw As Range
If WorksheetFunction.CountA(Selection) = 0 Then
MsgBox "No data found", vbOKOnly, "OzGrid.com"
Exit Sub
End If
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
Selection.SpecialCells(xlCellTypeBlanks).Select
For Each Rw In Selection.Rows
If WorksheetFunction.CountA(Selection.EntireRow) = 0 Then
Selection.EntireRow.Delete
End If
Next Rw
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub
【问题讨论】:
-
以相反的顺序删除行肯定会有所帮助。
-
如果可以接受,您也可以禁用屏幕更新。这将大大减少执行时间。在您的
clearalldemandzero子开头输入此文本Application.ScreenUpdating = False然后在子末尾输入Application.ScreenUpdating=true看看是否有区别。基本上excel会做所有的改变,当它完成时它会更新屏幕。所以所有的行看起来都被一次删除了。