简体   繁体   English

根据值或文本删除行

[英]Delete rows based on values or texts

This code deletes all rows that has the word "Service Tower" only in column D. 此代码删除仅在D列中带有单词“ Service Tower”的所有行。

I want to delete more rows which also has "Stream play", "Data Set", and many more that contains those text whether they are in column a, b, or c up to z. 我想删除更多行,这些行也具有“流播放”,“数据集”,以及更多包含这些文本的行,无论它们是在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
If .Value = "Service Tower" Or .Value = "Stream Play" Or .Value = "Data Set" Then .EntireRow.Delete

This will work if you only have a few to work with. 如果您只有少数几个可以使用,这将起作用。 For a larger number of items it's best to work with an array: 对于大量项目,最好使用数组:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM