从filter中删除'零'和'N / A'行 – VBA

必需:我试图创build一个macros,过滤所有值的单元格I22 ,select所有这些行,删除它们,然后不再filter。

我有什么:目前我在两个不同的步骤这样做,因为这一步需要几个小时(因为它删除每行的行)

代码(1) :自动filter为“零”和“不适用”,select全部并清除所有内容。 接下来清除filter并从最大到最小sorting。 这样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 

如果您select过滤数据的代码正在运行,您可以简单地删除该步骤中的所有行。 关键是使用SpecialCells并只select可见的单元格。 然后你可以得到EntireRowDelete它。

相关的代码行添加将是这样的:

 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交互的东西。 我没有尝试在这里解决这些问题,因为它似乎是你的代码通常工作。 引用该问题: 如何避免在Excel VBAmacros中使用select