Excel VBA - 查找和删除行范围内的空白单元格

时间:2017-12-06 09:59:20

标签: excel vba

我想创建一个宏来查找D列中的每个空白单元格。例如:如果D4有一个空白单元格,则应删除单元格B4,C4,D4,E4并向上移动以便现在有更多空白细胞

不知何故,宏不会删除任何内容。

Dim delREASON As Variant
Dim findReason As Range
Dim DelRng As Range

    With ThisWorkbook.Sheets("getDATA")
        delREASON = Null
        For Each findReason In .Range(.Range("D8"), .Range("D8").End(xlDown))
            If Not (IsError(Application.Match(findReason.Value, delREASON, 0))) Then
                If Not DelRng Is Nothing Then
            Set DelRng = Application.Union(DelRng, .Range(.Cells(findReason.Row, "B"), .Cells(findReason.Row, "E")))
        Else
            Set DelRng = .Range(.Cells(findReason.Row, "B"), .Cells(findReason.Row, "E"))
        End If
    End If
Next
    End With

1 个答案:

答案 0 :(得分:1)

在D列中找到空白单元格,然后创建其他相邻列的并集。

Dim blnks As Range
With ThisWorkbook.Sheets("getDATA")
    On Error Resume Next
    Set blnks = .Columns("D").SpecialCells(xlCellTypeBlanks)
    On Error GoTo 0
    If Not blnks Is Nothing Then
        Set blnks = Union(blnks.Offset(0, -2), blnks.Offset(0, -1), _
                          blnks, blnks.Offset(0, 1))
        blnks.Delete shift:=xlUp
    End If
End With