如何删除空行?

时间:2019-11-03 05:38:32

标签: excel vba

我正在尝试删除范围A2:A45中的空行。

我明白了

  

“编译错误-需要对象”

Sub delete_blank_rows()
Dim rng
Set rng = Nothing
For Each i In Range("A2:A45")
If Application.CountA(i.EntireRow) = 0 Then
    If rng Is Nothing Then
        Set rng = 1
        Else
            Set rng = Union(rng, i)
        End If
    End If
Next i
rng.EntireRow.Delete
End Sub

1 个答案:

答案 0 :(得分:0)

您键入的是1而不是i-将其更改为此:

Sub delete_blank_rows()
Dim rng
Set rng = Nothing
For Each i In Range("A2:A45")
    If Application.CountA(i.EntireRow) = 0 Then
        If rng Is Nothing Then
            Set rng = i ' <--- changed the 1 to an i
        Else
            Set rng = Union(rng, i)
        End If
    End If
Next i
rng.EntireRow.Delete
End Sub