2015-11-19 113 views
-2
Sub Chadsrebate() 

Application.ScreenUpdating = False 
Application.Calculation = xlCalculationManual 

If sheet2.Cells(Rows.Count, 2).Value <>"A","B","C","D" then 
entirerow.delete 

Application.ScreenUpdating = True 
Application.Calculation = xlCalculationAutomatic 
End Sub 
+0

那么......这是什么问题?你期望看到什么?你真正看到了什么? – grimstoner

+0

看看你最后的[问题](http://stackoverflow.com/questions/33656858/scroll-through-column-from-top-to-bottom-and-replace-0s-with-value-from-cell- ab),你需要用循环做几乎完全相同的事情,唯一的区别是你正在循环的列和if语句。使用该信息并查看IF..AND语句然后返回。 –

+0

因此,我有一个包含全部客户名称的数据。我需要删除所有行,但包含所述A,B,C,D等的行。在那之后我得到一个错误。 – ichoi

回答

2

的方式,你如何能做到这一个:如果你想删除它们包含名称中的所有行

Option Compare Text 
Sub Chadsrebate() 
    Dim i&, z& 
    With Application 
     .ScreenUpdating = False 
     .Calculation = xlCalculationManual 

     With Sheets("Sheet2") 
      i = .Cells(Rows.Count, "B").End(xlUp).Row 
      For z = i To 2 Step -1 
       If .Cells(z, "B").Value2 Like "*A*" _ 
        Or .Cells(z, "B").Value2 Like "*F*" _ 
         Or .Cells(z, "B").Value2 Like "*Z*" _ 
          Or .Cells(z, "B").Value2 Like "*Q*" Then 
        .Rows(z).Delete 
       End If 
      Next z 
      z = .Cells(Rows.Count, "B").End(xlUp).Row 
     End With 

     .ScreenUpdating = True 
     .Calculation = xlCalculationAutomatic 
    End With 
    MsgBox i - z & " Rows has been deleted!" 
End Sub 

,如果你想要删除所有不包含名称的行:

Option Compare Text 
Sub Chadsrebate() 
    Dim i&, z& 
    With Application 
     .ScreenUpdating = False 
     .Calculation = xlCalculationManual 

     With Sheets("Sheet2") 
      i = .Cells(Rows.Count, "B").End(xlUp).Row 
      For z = i To 2 Step -1 
       If Not (.Cells(z, "B").Value2 Like "*A*" _ 
        Or .Cells(z, "B").Value2 Like "*F*" _ 
         Or .Cells(z, "B").Value2 Like "*Z*" _ 
          Or .Cells(z, "B").Value2 Like "*Q*") Then 
        .Rows(z).Delete 
       End If 
      Next z 
      z = .Cells(Rows.Count, "B").End(xlUp).Row 
     End With 

     .ScreenUpdating = True 
     .Calculation = xlCalculationAutomatic 
    End With 
    MsgBox i - z & " Rows has been deleted!" 
End Sub 
+0

谢谢,但此代码删除值A,F,Z和问:我试图删除除A,F,Z和Q之外的所有其他值。我尝试用<>替换“like”,但这不起作用,它只是删除了所有内容。 – ichoi

+0

查看更新后的帖子,'If(.....)Then'已被替换为'If Not(.....)Then'' – Vasily