运行时错误438,excel VBAmacros – AutoFilter

所以在过去的几天里我一直在做这个工作,而且我似乎无法做到这一点。

Sub Button3_Click() Dim DeleteValue As String Dim rng As Range Dim calcmode As Long With Application calcmode = .Calculation .Calculation = xlCalculationManual .ScreenUpdating = False End With 'Fill in the value that you want to delete 'Tip: use DeleteValue = "<>ron" to delete rows without ron DeleteValue = "<>assap" 'Sheet with the data, you can also use Sheets("MySheet") With ActiveSheet 'Firstly, remove the AutoFilter ActiveSheet.AutoFilterMode = False 'Apply the filter 'The problem is this line according to the debugger ActiveSheet.Sheets("Sheet1").Range("A2:A" & .Rows.Count).AutoFilter Field:=1, Criteria1:=DeleteValue With .AutoFilter.Range On Error Resume Next Set rng = .Offset(1, 0).Resize(.Rows.Count - 1, 1) _ .SpecialCells(xlCellTypeVisible) On Error GoTo 0 If Not rng Is Nothing Then rng.EntireRow.Delete End With 'Remove the AutoFilter .AutoFilterMode = False End With With Application .ScreenUpdating = True .Calculation = calcmode End With End Sub 

这个应该做的是在列A中查找没有“assap”的单元格并删除它们,但是我不确定这段代码有什么问题,而且我从字面上知道它在线,大多数人似乎有它的工作,但我。 我也尝试添加和尝试其他的东西,但仍然会发生同样的问题,如果任何人都可以帮助我,我会很感激。

错误是438运行时错误

从该行的开头删除ActiveSheet.Sheet(“Sheet1”):

 ActiveSheet.Sheets("Sheet1").Range("A2:A" & .Rows.Count).AutoFilter Field:=1, Criteria1:=DeleteValue 

有几个问题; 主要来自您使用整个ActiveSheet.Rows.Count和With … End With语句的不正确使用。 使用Range.CurrentRegion属性将区域缩小到源于A1的“数据岛”。

 Sub Button3_Click() Dim DeleteValue As String Dim calcmode As Long With Application calcmode = .Calculation .Calculation = xlCalculationManual .ScreenUpdating = False End With 'Fill in the value that you want to delete 'Tip: use DeleteValue = "<>ron" to delete rows without ron DeleteValue = "<>assap" 'Sheet with the data, you can also use Sheets("MySheet") With ActiveSheet 'Firstly, remove the AutoFilter If .AutoFilterMode Then .AutoFilterMode = False 'Work with the range of cells containing data With .Cells(1, 1).CurrentRegion 'Apply the filter 'The problem is this line according to the debugger .Cells.AutoFilter Field:=1, Criteria1:=DeleteValue With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0) If CBool(Application.Subtotal(103, .Cells)) Then .Rows.EntireRow.Delete End If End With End With 'Remove the AutoFilter .AutoFilterMode = False End With With Application .ScreenUpdating = True .Calculation = calcmode End With End Sub 

在尝试使用Range.Offset属性向下推送范围时,使用.Rows.Count尝试将该范围从工作表的底部推出。 即使有一个较小的范围,不会从工作表的底部推行,Range.Resize属性应该预先设定一个Range.Offset属性 。

 'Use THIS With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0) 'Not this With .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count) 

你可以指定范围作为行例如我已经使用过滤条件的第一行

  ActiveSheet.Rows(2).AutoFilter Field:=1, Criteria1:=DeleteValue