【问题标题】:Run time error 438, excel VBA Macros - AutoFilter运行时错误 438,excel VBA 宏 - 自动筛选
【发布时间】:2015-10-26 21:48:04
【问题描述】:

所以过去几天我一直在研究这个问题,但我似乎无法让它发挥作用。

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运行时错误

【问题讨论】:

  • 你为什么在第二行开始你的过滤器?过滤器应(即必须)包含标题行。数据从哪一行开始?第 2 行还是第 3 行?
  • 因为数据从第二行开始

标签: vba excel


【解决方案1】:

从这一行的开头删除 ActiveSheet.Sheets("Sheet1"):

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

【讨论】:

  • 仍然存在同样的问题:(
  • 删除整个Activesheet.Sheets("Sheet1")
  • 已更新,请重试。
【解决方案2】:

有几个问题;主要是因为您对整个ActiveSheet 使用了.Rows.Count,并且不正确地使用了With ... End With statement。与Range.CurrentRegion property 合作,将区域缩小到源自 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 property 向下推范围时使用.Rows.Count 试图将范围推离工作表的底部。即使有一个较小的范围不会将行从工作表的底部推开,Range.Resize property 也应该先于Range.Offset property

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

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

【讨论】:

  • 当我尝试你的代码时,这一行得到一个运行错误代码 1004 .Cells.AutoFilter Field:=1, Criteria1:=DeleteValue not sure what's going on
  • 我已经在示例数据块上运行了代码,没有出现错误,它按预期运行。或许将With ActiveSheet 更改为With Worksheets("MySheet")
  • 还是同样的问题,我无法确定问题出在哪里
  • 可能是时候将问题工作簿的编辑副本发布到公共文件上传站点,并将上传文件的公开链接发布到您的原始问题中。
【解决方案3】:

您可以将范围指定为行,例如我使用第一行作为过滤条件

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

【讨论】:

  • 我不确定我是否能把你带到这里,所以我打算逐行但只过滤 A 列中的单元格,你的解决方案可以吗?
  • 你检查我的解决方案了吗?代码将在第一行放置一个过滤器,因为您的字段 =1,它将检查第一列中的 Crietrial
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多