【问题标题】:Delete rows based on values or texts根据值或文本删除行
【发布时间】:2019-07-11 20:41:18
【问题描述】:

此代码删除仅在 D 列中包含“服务塔”一词的所有行。

我想删除更多包含“流播放”、“数据集”的行,以及更多包含这些文本的行,无论它们是在列 a、b 还是 c 到 z。

Sheets("database").Select

Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long
With Application
    CalcMode = .Calculation
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
End With

With ActiveSheet    
    .Select
    ViewMode = ActiveWindow.View
    ActiveWindow.View = xlNormalView
    .DisplayPageBreaks = False    

    Firstrow = .UsedRange.Rows(2).Row
    Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

    For Lrow = Lastrow To Firstrow Step -1
        With .Cells(Lrow, "D")
            If Not IsError(.Value) Then
                If .Value = "Service Tower" Then .EntireRow.Delete
            End If
        End With

    Next Lrow
End With

ActiveWindow.View = ViewMode
With Application
    .ScreenUpdating = True
    .Calculation = CalcMode
End With

MsgBox "Report Completed!"
End Sub

【问题讨论】:

    标签: excel vba


    【解决方案1】:
    If .Value = "Service Tower" Or .Value = "Stream Play" Or .Value = "Data Set" Then .EntireRow.Delete
    

    如果您只有几个可以使用,这将起作用。对于大量项目,最好使用数组:

    delArray = Array("Service Tower", "Stream Play", "Data Set") ' add all the values you want in here
    For Lrow = Lastrow To Firstrow Step -1
        With .Cells(Lrow, "D")
            For i = 0 to UBound(delArray)  ' arrays are indexed from zero
                If Not IsError(.Value) Then
                    If .Value = delArray(i) Then 
                        ' if the value matches an array value we'll enter this condition
                        .EntireRow.Delete 
                        Exit For ' if you deleted the row, don't bother to check it against the next item...
                    End If
                End If
            Next
        End With
    Next Lrow
    

    【讨论】:

    • 谢谢你!如果我有一些问题,我会探索更多并回来
    猜你喜欢
    • 2012-07-04
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    相关资源
    最近更新 更多