如果列H包含以下任何值,我想删除一行:
1) %
2) Resistor
3) Capacitor
4) MCKT
5) Connector
6) anything else I may want to add to this list...
在谷歌上发现并进行了编辑 - 效果很好 - 不确定是否有更高效的"这样做的方法。
Sub DeleteRows()
lastrow = Cells.SpecialCells(xlCellTypeLastCell).Row
readrow = 1
For n = 1 to lastrow
If Range("H" & ReadRow).Value = "%" Or _
Range("H" & ReadRow).Value = "Resistor" Or _
Range("H" & ReadRow).Value = "Capacitor" Or _
Range("H" & ReadRow).Value = "MCKT" Or _
Range("H" & ReadRow).Value = "Connector" Then
Range("H" & ReadRow).EntireRow.Delete
Else
readrow = readrow + 1
End If
Next
End Sub
答案 0 :(得分:5)
有更有效的方法,但有这个:
代码:
Sub DeleteRows()
Dim Lastrow as long, n as long
lastrow = Cells.SpecialCells(xlCellTypeLastCell).Row
For n = lastrow to 1 Step -1
If Range("H" & n).Value = "%" Or _
Range("H" & n).Value = "Resistor" Or _
Range("H" & n).Value = "Capacitor" Or _
Range("H" & n).Value = "MCKT" Or _
Range("H" & n).Value = "Connector" Then
Rows(n).Delete
End If
Next
End Sub
另一种方法是使用Select Case
Sub DeleteRows()
Dim Lastrow as long, n as long
lastrow = Cells.SpecialCells(xlCellTypeLastCell).Row
For n = lastrow to 1 Step -1
Select Case Range("H" & n).Value
Case "%","Resistor","Capacitor","MCKT","Connector"
Rows(n).Delete
End Select
Next
End Sub
这样可以更轻松地添加到列表中。
答案 1 :(得分:0)
这很有效。
Option Explicit
Option Compare Text
Sub Delete_Rows()
Dim LastRow As Long, ReadRow As Long, n As Long
With ThisWorkbook.Sheets("Sheet1")
LastRow = .Cells(.Rows.Count, "H").End(xlUp).row
End With
ReadRow = 1
For n = 1 To LastRow
If Range("H" & ReadRow).Value Like "*%*" = True Or _
Range("H" & ReadRow).Value Like "*Resistor*" = True Or _
Range("H" & ReadRow).Value Like "*Transistor*" = True Or _
Range("H" & ReadRow).Value Like "*Micro*" = True Then
Range("H" & ReadRow).EntireRow.Delete
Else
ReadRow = ReadRow + 1
End If
Next
End Sub