代码之家  ›  专栏  ›  技术社区  ›  BruceWayne

删除行(向后),但使用范围变量?

  •  4
  • BruceWayne  · 技术社区  · 7 年前

    通常需要遍历一系列单元格,并根据某些条件删除整行。

    实际上,最好从最开始的时候开始 结束

    Dim i as Long
    For i = lastRow to 1 Step -1
        If Cells(i, 2).Value = "del" then Rows(i).EntireRow.Delete
    End if
    

    然而,大多数时候我都是和一个 Range

    有没有一种方法可以使用range对象而不需要使用 For i 类型循环?

    Dim rng as Range, cel as Range
    Set rng = Range("A1:A100")
    
    For each cel in rng step -1
       if cel.value = "del" then cel.EntireRow.Delete
    next cel
    

    此错误 Expected: End of Statement Step -1

    我的想法是,我基本上不必重新安排我的数据到 Cells() 射程 Cells([long],[long]) 如果这有道理的话。

    编辑:刚想到这个,但感觉还是很混乱:

    Dim k As Long, cel as Range
    Set cel = rng.cells(rng.cells.count)
    For k = cel.Row To rng.Cells(1).Row Step -1
        If rng.Cells(k).Value = "del" Then rng.Cells(k).EntireRow.Delete
    Next k
    
    3 回复  |  直到 7 年前