使用For Next删除空白行

时间:2015-01-19 03:50:10

标签: excel vba excel-vba

我的代码有关删除空白行的问题。它只需删除一些行而不是所有空行和行值#34; 0"。我不想在{o}论坛上使用.SpecialCells(xlCellTypeBlanks)作为威胁。

Dim R As Integer
R = Range("CuoiNKC").Row - 1
Dim DelCell As Range
Dim DelRange As Range
Set DelRange = Range("J9:J" & R)
For Each DelCell In DelRange
    If DelCell.Value = "0" Or DelCell.Formula = Space(0) Then
        DelCell.EntireRow.Delete
    End If
Next DelCel

1 个答案:

答案 0 :(得分:2)

为什么不使用Range AutoFilter Method而不是循环 假设您的代码中的DelRange值正确,请尝试以下操作:

DelRange.AutoFilter 1, AutoFilter 1, "=0", xlOr, "=" 'filtering 0 and space
DelRange.SpecialCells(xlCellTypeVisible).EntireRow.Delete xlUp 'delete visible cells
ActiveSheet.AutoFilterMode = False 'remove auto filter mode

顺便说一句,如果你想坚持你的逻辑,你需要向后迭代行 你只能使用传统的For Next Loop来做到这一点。再假设R的值是正确的。

For i = R To 9 Step -1
    If Range("J" & i).Value = "0" Or Range("J" & i).Value = " " Then
        Range("J" & i).EntireRow.Delete xlUp
    End If
Next
相关问题