[英]Delete Specific rows in Excel
I want to create a for loop to check all of the rows in a sheet that I have and want this code to be able to delete rows if they contain a specified content in certain columns (ie if column K contains "June" delete the row. Is there a way to code this? 我想创建一个for循环来检查工作表中的所有行,并希望此代码能够删除行(如果它们在某些列中包含指定内容)(即,如果列K包含“ June”,则删除该行)有没有办法对此进行编码?
*Edit I have the code working to search for criteria in one column, but now I need it to search and delete rows based on the data in two columns. *编辑我有用于在一列中搜索条件的代码,但是现在我需要它来基于两列中的数据搜索和删除行。 ie If the data in column K matches cell AJ1 (already have) and the data in column J matches AK1, then delete these rows.
即,如果K列中的数据与单元格AJ1相匹配(已经有)并且J列中的数据与AK1相匹配,则删除这些行。
The code I have is this: 我的代码是这样的:
Sub DeleteRows() 子DeleteRows()
Sheets("Sheet1").Select
Dim rFind As Range
Dim rDelete As Range
Dim strSearch As String
Dim iLookAt As Long
Dim bMatchCase As Boolean
strSearch = Range("AJ1")
iLookAt = xlWhole
bMatchCase = False
Set rDelete = Nothing
Application.ScreenUpdating = False
With Sheet1.Columns("K:K")
Set rFind = .Find(strSearch, LookIn:=xlValues, LookAt:=iLookAt, SearchDirection:=xlPrevious, MatchCase:=bMatchCase)
If Not rFind Is Nothing Then
Do
Set rDelete = rFind
Set rFind = .FindPrevious(rFind)
If rFind.Address = rDelete.Address Then Set rFind = Nothing
rDelete.EntireRow.Delete
Loop While Not rFind Is Nothing
End If
End With
Application.ScreenUpdating = True
End Sub 结束子
As a general rule of thumb, you should avoid looping over cells if possible in Excel VBA. 作为一般经验法则,应尽可能避免在Excel VBA中循环单元格。 Looping over cells is slow and inefficient .
遍历单元格缓慢且效率低下 。 It may not matter given the scope of your program, but it is something to be considered.
鉴于程序的范围,这可能并不重要,但这是需要考虑的事情。 If you are new to VBA programming, it's especially important to pick up good habits early on.
如果您不熟悉VBA编程,则尽早养成良好习惯特别重要。
Here is a solution using the Range.Find
method ( MSDN reference ) to gather the range of rows to delete, and then delete them all in one statement. 这是一种使用
Range.Find
方法( MSDN参考 )来收集要删除的行范围,然后在一条语句中将它们全部删除的解决方案。
Sub DeleteRows()
Dim rngResults As Range, rngToDelete As Range
Dim strFirstAddress As String
With Worksheets("Sheet1").UsedRange 'Adjust to your particular worksheet
Set rngResults = .Cells.Find(What:="June") 'Adjust what you want it to find
If Not rngResults Is Nothing Then
Set rngToDelete = rngResults
strFirstAddress = rngResults.Address
Set rngResults = .FindNext(After:=rngResults)
Do Until rngResults.Address = strFirstAddress
Set rngToDelete = Application.Union(rngToDelete, rngResults)
Set rngResults = .FindNext(After:=rngResults)
Loop
End If
End With
If Not rngToDelete Is Nothing Then rngToDelete.EntireRow.Delete
Set rngResults = Nothing
End Sub
This will process rows 2 thru 4000, adjust to suit your needs: 这将处理第2到4000行,以适应您的需求:
Sub RowKiller()
Dim K As Range, rKill As Range
Set K = Range("K2:K4000")
Set rKill = Nothing
For Each r In K
v = r.Text
If InStr(1, v, "June") > 0 Then
If rKill Is Nothing Then
Set rKill = r
Else
Set rKill = Union(r, rKill)
End If
End If
Next r
If Not rKill Is Nothing Then
rKill.EntireRow.Delete
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.