必需: 我尝试为所有零值创建一个过滤单元格I22 的宏,选择所有这些行,删除它们然后再次取消过滤。
我拥有的内容: 目前我在两个不同的步骤中执行此操作,因为一步完成此操作需要几个小时(因为它会删除每行的行数)
代码(1):自动过滤到“零”'和' N / A',选择所有内容并清除所有内容。接下来它清除过滤器并从最大到最小排序。这样,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
答案 0 :(得分:1)
如果您选择过滤数据的代码有效,您只需一次删除该步骤中的所有行。关键是使用SpecialCells
并仅选择可见单元格。然后,您可以获得EntireRow
和Delete
。
要添加的相关代码行是:
Selection.SpecialCells(xlCellTypeVisible).EntireRow.Delete
对代码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.SpecialCells(xlCellTypeVisible).EntireRow.Delete
ActiveSheet.ShowAllData
End Sub
作为旁注,您通常应该努力避免使用Select
Selection
以及与Excel UI交互的其他内容。我没有尝试解决这些问题,因为您的代码似乎通常正常工作。参考该问题:How to avoid using Select in Excel VBA macros