简体   繁体   English

如果整个范围为空白,则删除整行吗?

[英]Delete entire row if entire range is blank?

Haven't ever had to do this for an entire range, but only per cell for one column, so I need to figure out if this is even right. 从来不需要在整个范围内执行此操作,而只需要在一个列的每个单元格中执行此操作,因此我需要弄清楚这是否正确。 I want to loop through a column range (E2:S2) and if every cell is blank, then delete the whole row. 我想遍历列范围(E2:S2),如果每个单元格都为空白,则删除整行。 If there is at least one cell in that range with data, then keep the row. 如果该范围内至少有一个包含数据的单元格,则保留该行。

How could I edit this in order to create that For/Next loop? 我如何编辑它以创建该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

Would I need to add the for/next loop around the rng ? 我是否需要在rng周围添加for/next循环?

Have in mind Lastrow replace .Rows.Count . 请记住Lastrow替换.Rows.Count If needed, change the column for which Lastrow is calculated. 如果需要,请更改为其计算Lastrow的列。 For this example i use Column A . 对于此示例,我使用列A。

Try: 尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM