简体   繁体   English

如何删除范围内的所有行?

[英]How Can You Delete All the Rows In a Range?

I'm new to vba, and I've written the code below but can't figure out why it's not working.我是 vba 的新手,我写了下面的代码,但不知道为什么它不起作用。

Sub DataValidationDeleteWrongOrigin()
'
'Description: Goes through and deletes rows that are called out on the delete entry column.
'


'Dimension Worksheet
Dim DataValWs As Worksheet
Set DataValWs = Worksheets("Data Validation")

'Find the size of the table and store it as LastDataRow
Dim LastDataRow As Long
LastDataRow = DataValWs.Range("A5").End(xlDown).Row


'ThisRow will be the current row as the for loop goes through each row.
Dim ThisRow As Long
Dim DeleteRange As Range
Dim CurrentRow As Range
'Start the for loop from row 5
For ThisRow = 5 To LastDataRow

    'Check the Delete Entry row to see if it says Yes, delete the row. Use DeleteRange to add cells to it and delete them all at the end (much _
    faster than deleting them one at a time, and you don't have to re-find the size of the table after each row is deleted).

    If Cells(ThisRow, 16) = "Yes" Then
        If Not DeleteRange Is Nothing Then
            Set CurrentRow = DataValWs.Rows(ThisRow)
            Set DeleteRange = Union(CurrentRow, DeleteRange)
        Else
            Set DeleteRange = DataValWs.Cells(ThisRow, 16)
        End If


    End If

Next ThisRow

'DeleteRange.Select
DeleteRange.EntireRow.Delete



End Sub

Currently, the code gives me目前,代码给了我

Run-time error 1004 : Delete method of Range class failed.运行时错误 1004:Range 类的删除方法失败。

The "DeleteRange.Select" that is commented out near the end of the code selects the correct range, so I know the range is being built accurately.在代码末尾附近注释掉的“DeleteRange.Select”选择了正确的范围,所以我知道该范围正在准确构建。

I want to be able to just drop all of the rows to be deleted off of the sheet at once --the number of rows to be deleted could get pretty high in this application, and I'd prefer it not take forever to run.我希望能够一次从工作表中删除所有要删除的行——在这个应用程序中要删除的行数可能会非常高,我希望它不会永远运行。

I've looked around online, and found a couple of solutions that involve going through the DeleteRange iteratively and deleting each row out of it, but that gives me the same problem of deleting rows one at a time.我在网上环顾四周,找到了几个解决方案,这些解决方案涉及迭代地遍历 DeleteRange 并从中删除每一行,但这给了我一次删除一行的相同问题。 Is there a better way to handle this?有没有更好的方法来处理这个问题? Or, better yet, have I messed up in defining the DeleteRange?或者,更好的是,我是否在定义 DeleteRange 时搞砸了?

Thank you!谢谢!

Edit:编辑:

Did some testing with different sets of rows, and it turns out it has no problem deleting the rows if they're all adjacent to each other.对不同的行集进行了一些测试,结果发现如果它们彼此相邻,删除行没有问题。 Only results in the run time error if the rows have gaps between them...如果行之间有间隙,只会导致运行时错误......

将“EntireRow”属性添加到该行,如下所示:

Set DeleteRange = DataValWs.Cells(ThisRow, 16).EntireRow

I could replicate your problem only when there was an object at the side of the range (ie a ListObject )只有当范围一侧有对象(即ListObject )时,我才能复制您的问题

在此处输入图片说明

Check the data in your worksheet and if that is the case use rDelete.Delete Shift:=xlUp检查工作表中的数据,如果是这种情况,请使用rDelete.Delete Shift:=xlUp

Assuming your DATA is located in the range A5:P# (where # is the last row of data) use this code.假设您的 DATA 位于A5:P#范围内(其中#是最后一行数据),请使用此代码。

Sub Delete_Rows()
Dim ws As Worksheet
Dim rDelete As Range
Dim rData As Range, rRow As Range, lRw As Long

    Set ws = Worksheets("Data Validation")
    With ws
        lRw = .Range("A5").End(xlDown).Row
        Set rData = Range(.Range("A5"), Range("P" & lRw))    'adjust as required
    End With

    For Each rRow In rData.Rows
        If rRow.Cells(16) = "Yes" Then
            If rDelete Is Nothing Then
                Set rDelete = rRow

            Else
                Set rDelete = Union(rDelete, rRow)

    End If: End If: Next

    rDelete.Delete Shift:=xlUp

    End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM