不必在整个范围内执行此操作,而只需在一个列的每个单元格中执行此操作,因此我需要弄清楚这是否正确。我想遍历列范围(E2:S2),如果每个单元格都为空白,则删除整行。如果该范围内至少有一个包含数据的单元格,则保留该行。
我该如何编辑它才能创建For / Next循环?
Sub DeleteRowsWithEmptyColumnDCell()
Dim rng As Range
Dim i As Long
Set rng = ThisWorkbook.ActiveSheet.Range("E2:S2") ' <- and then loop to next row, etc..
With rng
For i = .Rows.Count To 1 Step -1
If .Item(i) = "" Then
.Item(i).EntireRow.Delete
End If
Next i
End With
End Sub
我是否需要在for/next
周围添加rng
循环?
答案 0 :(得分:0)
请记住 Lastrow 替换 .Rows.Count 。如果需要,请更改为其计算Lastrow的列。对于此示例,我使用 A列。
尝试:
Option Explicit
Sub DeleteRowsWithEmptyColumnDCell()
Dim rng As Range, cell As Range
Dim i As Long, y As Long, DeleteRow As Long, Lastrow As Long
Dim cellEmpty As Boolean
With ThisWorkbook.ActiveSheet
Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
For y = Lastrow To 2 Step -1
Set rng = .Range("E" & y & ":S" & y)
cellEmpty = False
For Each cell In rng
If cell.Value <> "" Then
cellEmpty = False
Exit For
Else:
cellEmpty = True
DeleteRow = cell.Row
End If
Next
If cellEmpty = True Then
.Rows(DeleteRow).EntireRow.Delete
End If
Next y
End With
End Sub