根据行号VBA删除整行

时间:2017-07-11 14:38:58

标签: excel vba

我有一个18,806行的列表(名为“Reference”的工作表),需要从90,000多行excel表(名为“To Delete”的工作表)中删除。我正在尝试在“Reference”中创建一个包含行号的数组,遍历数组,并从“To Delete”中删除数组中的每一行。到目前为止,我有:

    Sub deleteRows()

        Dim rowArray As Variant
        ReDim rowArray(18085) As Integer
        rowArray = Sheets("Reference").Range("A1:A18086").Value

        Dim Arr As Variant
        Dim del As Integer
        Dim i As Integer
        i = 1
        For Each Arr In rowArray
            del = Arr
            Sheets("To Delete").Cells(del, 1).EntireRow.Clear
        Next

    End Sub

编辑:想出来!它只是清除内容并有一些内存溢出错误,但我正在解决这个问题。只是想发布在这里以供将来参考:)

2 个答案:

答案 0 :(得分:1)

根据我以前的评论,我建议不要改变你的行号:

For Each a In rowArray
    del = rowArray(a)
    Worksheets.Rows(del).ClearContents
Next a

Dim rowNum as Integer
rowNum = Worksheets.Rows.RowCount
While rowNum > 0
    If Worksheets.Cells(rowNum,1).Value = "" Then
        Worksheets.Rows(rowNum).Delete
    End If
    rowNum = rowNum - 1
Loop

以下是处理后的代码。这应该几乎可以使用:

Sub deleteRows()
    Dim rowArray(18086) As Integer
    Dim i As Integer, j As Integer, del As Integer, rowNum As Integer
    i = 1
    j = 18086
    While i <= j
        rowArray(i) = Sheets("Reference").Range(i, 1).Value
        i = i + 1
    Loop

    For Each a In rowArray
        del = rowArray(a)
        Sheets("Reference").Rows(del).ClearContents
    Next a

    rowNum = Sheets("Reference").Rows.RowCount
    While rowNum > 0
        If Sheets("Reference").Cells(rowNum, 1).Value = "" Then
              Sheets("Reference").Rows(rowNum).Delete
        End If
        rowNum = rowNum - 1
    Loop
End Sub

为了安全起见,请确保在调用变量之前定义变量。这是代码中的通用规则。

答案 1 :(得分:0)

试试这个:

Worksheets.Rows(i).Delete

将i作为行号。

Nb:这会导致你的行数