2016-06-13 59 views
0

我有这段代码删除所有只有在D列中具有单词“服务塔”的行。我想要的是能够删除更多的行,这些行也具有“流媒体播放“,”数据集“等等,无论它们在哪里,都包含这些文本。无论他们在a栏,b栏还是c栏到z栏。我希望有人能够帮助我编辑我的代码,以便能够添加“服务塔”以外的其他单词。先谢谢你!基于值或文本删除行的VBA /宏

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 

回答

0
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 
+0

谢谢!如果我有一些问题,我会探索更多并回来 – proxyflow