【问题标题】:Excel VBA does not delete rows in some casesExcel VBA 在某些情况下不会删除行
【发布时间】:2019-11-07 12:17:19
【问题描述】:

我正在使用以下代码删除 excel 中的可见行(在过滤大于 30 的值之后)。我面临的问题是代码工作正常并在值(大于 30)按顺序时删除行,但它不会删除行并且当值不按顺序时会出错。我做错了什么?

'With rng
     .AutoFilter Field:=13, Criteria1:=">30"
     .SpecialCells(xlCellTypeVisible).EntireRow.Delete
     .AutoFilter Field:=13, Criteria1:=">0"
End With '

我收到错误:Delete method of Range class failed

这就是我要删除的数据的样子:

范围是如何设置的:

Dim rng As Range

LastRow = btsvoice.Range("M" & btsvoice.Rows.Count).End(xlUp).Row
Set rng = btsvoice.Range("M2:M" & LastRow)

下面是整个模块代码:

Sub Delete_Row_Final()

    Dim voice As Workbook
    Dim data As Workbook
    Dim btsvoice As Worksheet
    Dim nodebvoice As Worksheet
    Dim btsdata As Worksheet
    Dim enodebdata As Worksheet
    Dim rng As Range
    Dim LastRow As Long
    Dim MyRange As Range

    Set voice = Workbooks("Voice_Files.xlsx")
    Set btsvoice = voice.Sheets("2G Voice")
    Set nodebvoice = voice.Sheets("3G Voice")

    Set data = Workbooks("Data_Files.xlsx")
    Set btsdata = data.Sheets("2G Data")
    Set enodebdata = data.Sheets("4G Data")

    Application.Calculate
    If Not Application.CalculationState = xlDone Then
        DoEvents
    End If

    ' -------------------  2G Voice ------------------------
    'filter and delete all but header row which is in row 3
LastRow = btsvoice.Range("M" & btsvoice.Rows.Count).End(xlUp).Row
Set rng = btsvoice.Range("M1:M" & LastRow)

    ' filter and delete all but header row
    With rng
         .AutoFilter Field:=13, Criteria1:=">30"
    .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    .AutoFilter Field:=13, Criteria1:=">0"
    End With

    ' -------------------  3G Voice ------------------------
    'filter and delete all but header row which is in row 3
    LastRow = nodebvoice.Range("K" & nodebvoice.Rows.Count).End(xlUp).Row
    Set rng = nodebvoice.Range("K2:K" & LastRow)

    ' filter and delete all but header row
    With rng
         .AutoFilter Field:=11, Criteria1:=">30"
         .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
         .AutoFilter Field:=11, Criteria1:=">0"
    End With

    voice.Save

        ' -------------------  2G Data ------------------------
    'filter and delete all but header row which is in row 3
    LastRow = btsdata.Range("L" & btsdata.Rows.Count).End(xlUp).Row
    Set rng = btsdata.Range("L2:L" & LastRow)

    ' filter and delete all but header row
    With rng
         .AutoFilter Field:=12, Criteria1:=">30"
         .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
         .AutoFilter Field:=12, Criteria1:=">0"
    End With

            ' -------------------  4G Data ------------------------
    'filter and delete all but header row which is in row 3
    LastRow = enodebdata.Range("M" & enodebdata.Rows.Count).End(xlUp).Row
    Set rng = enodebdata.Range("M2:M" & LastRow)

    ' filter and delete all but header row
    With rng
         .AutoFilter Field:=13, Criteria1:=">30"
         .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
         .AutoFilter Field:=13, Criteria1:=">0"
    End With

    data.Save

End Sub

【问题讨论】:

  • 我假设您打算使用:.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete。除此之外,您还应该附上一张截图,说明您的数据在过滤后的样子,以及您在哪一行出现了哪些错误。
  • @JvdV 感谢您的建议。奇怪的是它曾经工作得很好。它今天只是随机开始给我错误。您对可能导致问题的原因有任何猜测吗?
  • 好吧,我想不出是什么导致了错误,因为我只复制了一次,而在第二次运行时它运行良好。你能包括你如何得到rng
  • 过滤后尝试移动删除整个
  • 正如@JvdV 指出的那样,我认为您的问题可能是您如何设置rng 变量。如果你能提供那段代码,那会有所帮助

标签: excel vba


【解决方案1】:

查看您当前的问题和解释,我认为您的代码中一定有一个额外的3

如果你设置范围:

LastRow = btsvoice.Range("M" & btsvoice.Rows.Count).End(xlUp).Row
Set rng = btsvoice.Range("M2:M" & LastRow)

您不能索引到 Field:=13,因为它根本不存在于 1 列范围内。请改用Field:=1

但是,需要指出的其他一些建议是,由于您有标头,使用Offset 可能会更好,使您的代码看起来像:

Dim rng As Range

LastRow = btsvoice.Range("M" & btsvoice.Rows.Count).End(xlUp).Row
Set rng = btsvoice.Range("M1:M" & LastRow)

With rng
    .AutoFilter Field:=1, Criteria1:=">30"
    .Offset(1,0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    .AutoFilter Field:=1, Criteria1:=">0"
End With

我可以想象的另一件事:当AutoFilter 已经应用于单个列,并且您想要AutoFilter 另一个单个列时,也会抛出所描述的错误。这意味着您有两种选择:

  • 在应用其他过滤器之前删除 AutoFilter
  • 将过滤器应用于整个范围而不是单个列

对于第一个选项,您可以尝试:

With Sheet1
    If .AutoFilterMode = True Then .AutoFilterMode = False
End with

对于第二个选项,您需要将范围设置为 A:M 列(甚至更远)

Set rng = btsvoice.Range("A2:M" & LastRow)

【讨论】:

  • Field:=1 过滤列A 为我,而Field:=13 过滤列M 按预期。错误发生在.Offset(1,0).SpecialCells(xlCellTypeVisible).EntireRow.Delete 行。我已将offset 添加到我的其余代码中。谢谢。
  • 我已经用完整的模块代码更新了我的问题。也许这可能会有所帮助
  • @aab,这种行为告诉我还有另一个AutoFilter 应用于A:A 范围。请注意,您的代码将使用相同的 AutoFilter 并尝试过滤字段 13,这是不可能的,因为您尚未将 rng 设置为多列。我已经相应地更新了我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多