Deleting rows (working backwards), but use a range variable?

前端 未结 3 1408
情歌与酒
情歌与酒 2021-01-03 09:17

Often times it\'s required that you go through a range of cells, and based on some criteria, delete an entire row.

In practice, it\'s best to start at the end

3条回答
  •  时光说笑
    2021-01-03 09:39

    Yes, you can do it without a For i = statement. Just create a special range that you will delete once you finish your loop.

    Dim cel As Range, rng As Range
    Dim delRng As Range
    
    For Each cel In rng
        If cel.Value = "del" Then
            If delRng Is Nothing Then
                Set delRng = cel
            Else
                Set delRng = Union(delRng, cel)
            End If
        End If
    Next cel
    
    If Not delRng Is Nothing Then delRng.EntireRow.Delete
    

    And you don't even have to step backwards.

提交回复
热议问题