2015-11-19 151 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