【问题标题】:Removing rows without skipping a row how to?删除行而不跳过一行如何?
【发布时间】:2011-07-13 17:34:48
【问题描述】:

下面是我的代码。我试图通过一些行,如果那里有某些数据,然后删除这些行,但是我目前的编码方式,每当我删除一行时,我都会跳过它下面的行。我将范围更改为将高编号行变为低编号行,但我的宏仍然从顶部开始并向下移动。我想如果我让它向上移动,删除不会导致它跳过下一个项目。我怎样才能让它从列表的底部向上移动,或者有什么更好的方法呢? 我的代码如下:

Dim lLastRow As Long
Dim num As Integer
Dim name As String
Dim rCell As Range
Dim afCell As Range
Dim rRng As Range
Dim affectedRng As Range

Windows("Most Affected Customer Info.xls").Activate
Worksheets("Sheet 1").Activate
Cells(1, 1).Select
Selection.End(xlDown).Select
lLastRow = ActiveCell.Row

Set affectedRng = Range("A" & lLastRow & ":A2")
'First remove resolved entries
For Each afCell In affectedRng
    If (afCell.Offset(0, 4).Value = "resolved" Or afCell.Offset(0, 4).Value _ = "Resolved" Or afCell.Offset(0, 2).Value = "Resolved" Or afCell.Offset(0, 2).Value = _ "resolved") Then
            afCell.EntireRow.Delete
    End If
Next afCell

【问题讨论】:

    标签: vba excel foreach rows


    【解决方案1】:

    倒着看列表怎么样?

    编辑一些代码来尝试

    For row = lLastRow To 1 Step -1
        If Range("D" & row).Value = "resolved" Then Rows(row).EntireRow.Delete
    Next row
    

    我在 D 列中带有“已解决”的小案例上对此进行了测试,它的工作原理非常出色。您可能会发现代码既能解决问题又能很好地阅读。

    【讨论】:

    • 我在问题中解决了这个问题。
    • 要向后遍历,您要么需要使用 do while,要么需要在行号上使用带有 Step -1 的 for 循环。
    • 谢谢@Jon Egerton,就是这样。
    • 赞成我,因为从底部到顶部似乎是最优雅和有效的解决方案。
    • 我也点赞。问题中使用的方法(将范围写为 A100:A2 而不是 A2:A100)对检查行的顺序没有影响
    【解决方案2】:

    您需要一个 do 循环并使用行号,因为您需要操纵循环中的当前位置以及终点;

    例如

    Dim lRow as Long
    lRow = 1
    Do Until lRow > lLastRow
        Set afCell = Cells(lRow ,1)
    
        If (afCell.Offset(0, 4).Value = "resolved" Or afCell.Offset(0, 4).Value _ = "Resolved" Or afCell.Offset(0, 2).Value = "Resolved" Or afCell.Offset(0, 2).Value = _ "resolved") Then
            afCell.EntireRow.Delete
    
            'Decrement the last row as we've removed a row
            lLastRow = lLastRow - 1
        else
            'Increment the row number to move to the next one
            lRow = lRow + 1
        End IF
    Loop
    

    注意:这是完全未经测试的,因此您需要对其进行调试,但您应该了解要点。

    【讨论】:

    • 如果有人发现需要更正,请告诉我,我会更新答案。
    • 应该一直执行到(条件)..... ..... .. 循环但除此之外它工作得很好。非常感谢
    • 就像布赖恩说的那样,我会做一个直到循环,这样即使没有数据行它也能工作。
    【解决方案3】:

    您可以将 For Each 替换为 do while,如下所示:

    rowx = 2
    Do While rowx < llastrow
        If Range("B" & rowx).Value = "resolved" Then 'replace this with the columns you're checking
            Rows(rowx).EntireRow.Delete
        Else
            rowx = rowx + 1
        End If
    
    Loop
    

    【讨论】:

    • 您需要在删除行时处理 lLastRow 更改。
    • 只需在删除后添加一个 llastrow = llastrow - 1 就可以了。此外,根据 llastrow 的创建方式,您可能需要一个
    【解决方案4】:

    您可以尝试使用 While 循环更改 If 条件。

    For Each afCell In affectedRng
        r = afCell.Row
        c = afCell.Column
        While (afCell.Offset(0, 4).Value = "resolved" Or afCell.Offset(0, 4).Value _ = "Resolved" Or afCell.Offset(0, 2).Value = "Resolved" Or afCell.Offset(0, 2).Value = _ "resolved") 
           afCell.EntireRow.Delete
           Set afCell = Cells(r, c)
        Wend
    Next afCell
    

    【讨论】:

    • 当你点击一个不适合删除的行时,这个 While 循环不会进入无限循环吗?
    • 它不会进入无限循环。我已经更新了代码。检查更新的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多