Excel 如何一次删除多行而不会出现关于';删除范围类的方法';?
在这张表中,我试图在一个范围内搜索空单元格,并删除它们各自的行Excel 如何一次删除多行而不会出现关于';删除范围类的方法';?,excel,vba,rows,Excel,Vba,Rows,在这张表中,我试图在一个范围内搜索空单元格,并删除它们各自的行 Sub Delete() 'Amass row numbers Dim B, Blank As Range Dim Deletion() As Long Dim D As Long Set Blank = Sheets("Quotation").Range("I17:I3816") D = 0 For Each B In Blank If IsEmpty(B) Then D = D + 1
Sub Delete()
'Amass row numbers
Dim B, Blank As Range
Dim Deletion() As Long
Dim D As Long
Set Blank = Sheets("Quotation").Range("I17:I3816")
D = 0
For Each B In Blank
If IsEmpty(B) Then
D = D + 1
ReDim Preserve Deletion(D)
Deletion(D) = B.Row
End If
Next B
Dim Amass As Range
'A starting point for the Amass range - should it need one pre-Union?
Set Amass = Sheets("Quotation").Range("10000:10000")
'Amass rows
For i = 1 To D
Set Amass = Union(Amass, Sheets("Quotation").Range(Deletion(i) & ":" & Deletion(i)))
Next i
'Delete rows
Amass.EntireRow.Delete
End Sub
最后一次操作失败,错误如下:
“删除范围类的方法失败”
我是否正确使用了阵列和“ReDim Preserve”功能?我想这正是您想要的:
Sub tgr()
Dim ws As Worksheet
Dim rCheck As Range
Dim rDel As Range
Set ws = ActiveWorkbook.Sheets("Quotation")
For Each rCheck In ws.Range("I17", ws.Cells(ws.Rows.Count, "I").End(xlUp)).Cells
If IsEmpty(rCheck) Then
If Not rDel Is Nothing Then Set rDel = Union(rDel, rCheck) Else Set rDel = rCheck
End If
Next rCheck
If Not rDel Is Nothing Then rDel.EntireRow.Delete
End Sub
查看此处的方法-->当在循环的范围中发现空白单元格而不是创建数组时,是否尝试删除行?您的代码在我的电脑上正常工作。您确定工作簿中有列表“引号”吗?谢谢,Tiger。我当然会考虑其他的选择,只是选择一些更快更容易的。我喜欢您将我的代码缩减为更紧凑的方式。一个问题:最后一行是“…rDel.EntireRow.Delete”,其中rDel是要删除的行的集合,对吗?那么,在我的原始代码中,为什么“Amass.EntireRow.Delete”不起作用?我的射程有问题吗?@Bengery我无法重现你的错误。您的代码在我的测试工作簿中运行良好。