根据特定列中的文本值删除行

根据特定列中的文本值删除行

我为我的工作簿的“预算”标签写了一个简短的宏,以删除第一列中所有值为“不适用”的行。

通过测试运行宏时,它似乎没有任何作用:

Sub Remove_NA_Macro_Round_2()
    With Sheets("Budget") 'Applying this macro to the "Budget" sheet/tab.

        'Establishing our macro range parameters
        Dim LastRow As Long
        Dim i As Long

        'Setting the last row as the ending range for this macro
        LastRow = .Range("I50").End(xlUp).Row

        'Looping throughout all rows until the "LastRow" ending range set above
        For i = LastRow To 1 Step -1
            If .Range("I" & i).Value = "Not Applicable" Then
                .Range("I" & i).EntireRow.Delete
            End If
        Next
    End With
End Sub

感谢您的帮助!

tangjw08 回答:根据特定列中的文本值删除行

您实际上不是在引用With Sheets("Budget")。在每个.实例之前添加句点Range,否则存在一个隐式ActiveSheet,不一定是“预算”选项卡。

With Sheets("Budget") 
    ...

    LastRow = .Range("I50").End(xlUp).Row

    ...
        If .Range("I" & i).Value = "Not Applicable" Then
            .Range("I" & i).EntireRow.Delete
        End If
    ...

End With

编辑:

根据评论和提供的屏幕截图,更改确定LastRow的方式(摆脱硬编码的I50):

LastRow = .Cells(.Rows.Count,"I").End(xlUp).Row
,

或者,根据条件删除行时,使用过滤器然后循环的速度更快。

_WIN32_WINNT
本文链接:https://www.f2er.com/2776785.html

大家都在问