【问题标题】:Delete entire row if entire range is blank?如果整个范围为空白,则删除整行?
【发布时间】:2019-07-05 01:21:24
【问题描述】:

从来没有对整个范围执行此操作,但只针对一列的每个单元格执行此操作,因此我需要确定这是否正确。我想遍历一个列范围(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

我需要在rng 周围添加for/next 循环吗?

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    记住 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-27
      • 1970-01-01
      • 1970-01-01
      • 2019-09-07
      • 1970-01-01
      相关资源
      最近更新 更多