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

我想创build一个macros来查找D列中的每一个空白单元格。例如:如果D4有一个空白单元格,那么单元格B4,C4,D4,E4应该被删除并向上移动,以便现在有更多空白单元格。

不知何故macros不会删除任何东西。

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 

find列D中的空白单元格,然后创build其他相邻列的联合。

 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