【发布时间】: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变量。如果你能提供那段代码,那会有所帮助