2015-05-05 68 views
1

我需要通過刪除特定位置的行來清理一些記錄,這些位置與我們沒有交往。我想知道如何編寫VBA代碼來搜索目的地城市和目的地國家列(在我當前的情況下列L & M)並刪除匹配特定城市和州的記錄。如何根據VBA中的2列中的條件刪除行?

我已經想出瞭如何通過同一列內的多個條件刪除記錄,但不是跨列刪除記錄。

With Application 
    CalcMode = .Calculation 
    .Calculation = xlCalculationManual 
    .ScreenUpdating = False 
End With 
With Sheets("data_export") 
    .Select 
    ViewMode = ActiveWindow.View 
    ActiveWindow.View = xlNormalView 
    .DisplayPageBreaks = False 
    Firstrow = 2 
    Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row 
    For lRow = Lastrow To Firstrow Step -1 
     With .Cells(lRow, "D") 'Stops' 
      If Not IsError(.Value) Then 
       If .Value >= "1" Then .EntireRow.Delete 
      End If 
     End With 

     With .Cells(lRow, "P") 'HaZMat 
      If Not IsError(.Value) Then 
       If .Value <> "" Then .EntireRow.Delete 
      End If 
     End With 

     With .Cells(lRow, "T") 'Service 
      If Not IsError(.Value) Then 
       If .Value <> "Truck Load" Then .EntireRow.Delete 
      End If 
     End With 
    Next lRow 
End With 

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

回答

1

有了這樣的數據:

enter image description here

我們要刪除所有行的斯普林菲爾德,加利福尼亞但保持斯普林菲爾德,美國佛羅里達州。

Sub RowKiller() 
    Dim N As Long, i As Long 
    N = Cells(Rows.Count, "L").End(xlUp).Row 
    For i = N To 1 Step -1 
     If Cells(i, "L") = "Springfield" And Cells(i, "M") = "California" Then 
      Cells(i, "L").EntireRow.Delete 
     End If 
    Next i 
End Sub 
+0

是的,這正是我所需要的。非常感謝! – BarryMahnly

+0

@BryryMahnly感謝您的反饋。 –