![](/img/trans.png)
[英]VBA Excel - Is there a way to delete rows based on criteria in two columns?
[英]How to Delete Rows Based on Criteria from 2 Columns in VBA?
我需要通過刪除與我們無關的特定位置的行來清理一些記錄。 我想弄清楚如何編寫VBA代碼,該代碼將搜索“目標城市”和“目標州”列(在當前情況下為L&M列),並刪除與特定城市和州匹配的記錄。
我已經弄清楚了如何在同一列而不是跨列中通過多個條件刪除記錄。
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
使用以下數據:
我們要刪除加利福尼亞州斯普林菲爾德的所有行,但保留佛羅里達州斯普林菲爾德。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.